我为我的数据库中的每个名称创建一个按钮,然后我应该在此按钮添加一个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>
感谢
答案 0 :(得分:1)
所以按钮在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) {
...
}
});
}
}
这样你的'find'就会限定在列表中的一行。您无法在当前拥有代码的位置添加侦听器,因为将有多个具有相同ID的视图。
答案 1 :(得分:1)
要将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
}