我有一个使用两个独立数据库的选项卡主机活动。每个选项卡都有一个列表视图。我正在尝试使用上下文菜单通过长按来删除列表项。我所拥有的将在 list1 选项卡上工作,但不会删除 list2 选项卡中的列表项。这是我的代码:
public class TabListActivity extends TabActivity implementsOnTabChangeListener,OnClickListener{
private static final String LIST1_TAB_TAG = "Mixed Recipes";
private static final String LIST2_TAB_TAG = "Pre-Mixed Recipes";
// The two views in our tabbed example
private ListView listView1;
private ListView listView2;
private TabHost tabHost;
private DatabaseManager mDbHelper;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs);
mDbHelper = new DatabaseManager(this);
mDbHelper.open();
tabHost = getTabHost();
tabHost.setOnTabChangedListener(this);
// setup list view 1
listView1 = (ListView) findViewById(R.id.list1);
registerForContextMenu(listView1);
// setup list view 2
listView2 = (ListView) findViewById(R.id.list2);
registerForContextMenu(listView2);
// add views to tab host
tabHost.addTab(tabHost.newTabSpec(LIST1_TAB_TAG).setIndicator(LIST1_TAB_TAG).setContent(new TabContentFactory() {
public View createTabContent(String arg0) {
return listView1;
}
}));
tabHost.addTab(tabHost.newTabSpec(LIST2_TAB_TAG).setIndicator(LIST2_TAB_TAG).setContent(new TabContentFactory() {
public View createTabContent(String arg0) {
return listView2;
}
}));
tabHost.setCurrentTab(1);
tabHost.setCurrentTab(0);
}
/**
* Implement logic here when a tab is selected
*/
public void onTabChanged(String tabName) {
if(tabName.equals(LIST2_TAB_TAG)) {
//do something
fillDatapremix();
}
else if(tabName.equals(LIST1_TAB_TAG)) {
//do something
fillData();
}
}
private void fillData() {
Cursor notesCursor = mDbHelper.fetchAllNotes();
startManagingCursor(notesCursor);
// Create an array to specify the fields we want to display in the list (only TITLE)
String[] from = new String[]{DatabaseManager.KEY_NAME, DatabaseManager.KEY_ROWID};
//String1[] from = new String[]{DatabaseManager.KEY_ROWID};
// and an array of the fields we want to bind those fields to (in this case just text1)
int[] to = new int[]{android.R.id.text1};
//int[] to = new int[]{android.R.id.text2};
// Now create a simple cursor adapter and set it to display
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this,android.R.layout.simple_list_item_2, notesCursor, from, to);
listView1.setAdapter(notes);
listView1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent myIntent = new Intent(view.getContext(),
NoCalc.class);
myIntent.putExtra(DatabaseManager.KEY_ROWID, id);
startActivity(myIntent);
}
});
}
public void onClick(View v) {
// TODO Auto-generated method stub
}
private void fillDatapremix() {
Cursor notesCursor = mDbHelper.fetchAllNotespremix();
startManagingCursor(notesCursor);
// Create an array to specify the fields we want to display in the list (only TITLE)
String[] from = new String[]{DatabaseManager.KEY_NAME, DatabaseManager.KEY_ROWID};
//String1[] from = new String[]{DatabaseManager.KEY_ROWID};
// and an array of the fields we want to bind those fields to (in this case just text1)
int[] to = new int[]{android.R.id.text1};
//int[] to = new int[]{android.R.id.text2};
// Now create a simple cursor adapter and set it to display
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this,android.R.layout.simple_list_item_2, notesCursor, from, to);
listView2.setAdapter(notes);
listView2.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent myIntent = new Intent(view.getContext(),
Premix2.class);
myIntent.putExtra(DatabaseManager.KEY_ROWID, id);
startActivity(myIntent);
}
});
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
switch (v.getId()) {
}
menu.setHeaderTitle("Delete Recipe?");
menu.add(0, v.getId(), 0, "Delete");
}
public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.list1:
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
mDbHelper.deleteRow(info.id);
fillData();
return true;
case R.id.list2:
AdapterContextMenuInfo info1 = (AdapterContextMenuInfo) item.getMenuInfo();
mDbHelper.deleteRow(info1.id);
fillData();
return true;
}
return false;
}
}
我的问题是,如何获得第二个选项卡以从列表和数据库中删除列表项?