2
我想在ListView上的每一行添加一個按鈕。該按鈕然後將從表中複製已經被點擊的行,並將該行插入到另一個表中。我需要知道的是如何將點擊行rowId分配給我的按鈕方法,因爲我需要將它傳遞給copyData方法。這是我的主代碼,按鈕方法在底部。通過一個按鈕傳遞ListView的rowId
public class Favourites extends ListActivity {
private static final int ACTIVITY_CREATE=0;
private static final int ACTIVITY_EDIT=1;
private static final int INSERT_ID = Menu.FIRST;
private static final int DELETE_ID = Menu.FIRST + 1;
private FavouritesDbAdapter mDbHelper;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.favourites_list);
mDbHelper = new FavouritesDbAdapter(this);
mDbHelper.open();
fillData();
registerForContextMenu(getListView());
}
private void fillData() {
Cursor notesCursor = mDbHelper.fetchAllNotes();
startManagingCursor(notesCursor);
// Create an array to specify the fields we want to display in the list
String[] from = new String[]{FavouritesDbAdapter.KEY_TITLE,FavouritesDbAdapter.KEY_ROWID};
// and an array of the fields we want to bind those fields to
int[] to = new int[]{R.id.text1,R.id.text2};
// Now create a simple cursor adapter and set it to display
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.favourites_row, notesCursor, from, to);
setListAdapter(notes);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, INSERT_ID, 0, R.string.menu_insert);
return true;
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch(item.getItemId()) {
case INSERT_ID:
createNote();
return true;
}
return super.onMenuItemSelected(featureId, item);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(0, DELETE_ID, 0, R.string.menu_delete);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId()) {
case DELETE_ID:
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
mDbHelper.deleteNote(info.id);
fillData();
return true;
}
return super.onContextItemSelected(item);
}
private void createNote() {
Intent i = new Intent(this, FavouritesEdit.class);
startActivityForResult(i, ACTIVITY_CREATE);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Intent i = new Intent(this, Favourites.class);
Intent i1 = new Intent(this, FavouritesEdit.class);
i1.putExtra(FavouritesDbAdapter.KEY_ROWID, id);
i.putExtra(FavouritesDbAdapter.KEY_ROWID, id);
startActivityForResult(i1, ACTIVITY_EDIT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
fillData();
}
public void button(View view) {
mDbHelper.copyData(#ROWID of CLICKED BUTTON TO BE PASSED OVER#);
fillData();
}
}
使用選定的行ID設置局部變量會更容易,您不覺得嗎? – ykatchou 2010-11-10 13:40:51
我不知道,爲什麼我問:) – Sazzle 2010-11-10 14:02:53