问题描述
|
我为数据库中的每个名称创建一个按钮,然后应在此按钮上添加onclick函数,但我不知道使用什么ID。我用 ???在代码中指出位置
public class Game extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
ListView playersList=(ListView)findViewById(R.id.playersList);
MyDb db=new MyDb(getApplicationContext());
db.open();
Cursor c=db.fetchAllUsers();
startManagingCursor(c);
SimpleCursorAdapter adapter=new SimpleCursorAdapter(
this,R.layout.user,c,new String[]{MyDb.UsersMetaData.USERS_NAME},new int[]{R.id.namePlayer}
);
playersList.setAdapter(adapter);
db.close();
Button newUser;
newUser = (Button)findViewById(R.id.buttonNew);
newUser.setonClickListener(new View.OnClickListener() {
public void onClick(View view) {
MyDb db=new MyDb(getApplicationContext());
db.open();
EditText inserted = (EditText)findViewById(R.id.editName);
String nome = inserted.getText().toString();
db.insertUser(nome);
db.close();
}
});
Button chosen;
chosen = (Button)findViewById(R.id.???);
chosen.setonClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(),Game2.class);
startActivityForResult(myIntent,0);
finish();
}
});
}
}
用户界面
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<FrameLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"
android:id=\"@+id/sfondo\"
android:layout_height=\"fill_parent\"
android:layout_width=\"fill_parent\"
android:background=\"@drawable/sfondo\">
<LinearLayout
android:orientation=\"vertical\"
android:layout_width=\"fill_parent\"
android:layout_height=\"fill_parent\">
<LinearLayout
android:orientation=\"horizontal\"
android:layout_width=\"fill_parent\"
android:layout_height=\"wrap_content\"
android:layout_gravity=\"center_horizontal\">
<EditText
android:id=\"@+id/editName\"
android:text=\"@string/editName\"
android:layout_height=\"wrap_content\"
android:layout_width=\"wrap_content\"
android:lines=\"1\"
android:width=\"180px\" />
<Button
android:text=\"@string/newPlayer\"
android:id=\"@+id/buttonNew\"
android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\"
android:minWidth=\"70px\" />
</LinearLayout>
<ListView
android:id=\"@+id/playersList\"
android:layout_width=\"fill_parent\"
android:layout_height=\"fill_parent\" />
</LinearLayout>
</FrameLayout>
db元素的xml
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<LinearLayout
xmlns:android=\"http://schemas.android.com/apk/res/android\"
android:layout_width=\"fill_parent\"
android:layout_height=\"fill_parent\"
android:orientation=\"vertical\"
android:layout_gravity=\"center_vertical\"
>
<Button
android:id=\"@+id/namePlayer\"
android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\"
android:layout_gravity=\"center_horizontal\"
android:minWidth=\"250px\" />
</LinearLayout>
谢谢
解决方法
那么按钮在
R.layout.user
中吗?如果是这样,我认为您可能需要扩展SimpleCursorAdapter
,并在getView()
方法中可以找到视图并添加侦听器。像这样:
public class MyCursorAdapter extends SimpleCursorAdapter {
public MyCursorAdapter(...) {
super(...);
}
public void getView(int position,View convertView,ViewGroup parent) {
View view = super.getView(position,convertView,parent);
Button button = (Button) view.findViewById(R.id.???);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
...
}
});
}
}
这样,您的\'查找\'便会在列表中限定为一行。您无法将侦听器添加到当前有代码的位置,因为将有多个具有相同ID的视图。
, 要为多个对象定义onClick,您的类应实现接口OnClickListener
并覆盖方法onClick(View v)
,如下所示:
public class MyActivity extends Activity implements OnClickListener{
@Override
public void onCreate(Bundle savedInstance){
....
//add listeners to your buttons
btn1.setOnClikListener(this);
btn2.setOnClickListener(this);
....
}
@Override
public void onClick(View v ) {
if(v==btn1)
// treatement 1
if(v== btn2)
//treatment 2
...etc
}