I create a button for each name in my database, then i should add at this button a onclick function, but i don’t know what id use. i use ??? in the code for indicate the position
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();
}
});
}
}
user interface
<?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>
xml for the db elements
<?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>
thanks
So the button is in
R.layout.user? If so I think you may need to extendSimpleCursorAdapterand in thegetView()method you can find the view and add the listener. Something like this:That way your ‘find’ is scoped to one row in the list. You can’t add the listener where you have the code currently because there will be multiple Views with the same ID.