2つの別々のデータベースを使用するタブホストアクティビティがあります。各タブにはリストビューがあります。コンテキストメニューを使用して、ロングクリックでリストアイテムを削除しようとしています。私が持っているものは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;
}
}
私の質問は、リストとデータベースからリストアイテムを削除するための2番目のタブを取得するにはどうすればよいですか?