删除不同选项卡中的列表项

时间:2012-06-06 18:20:42

标签: android listview android-tabhost

我有一个使用两个独立数据库的标签主机活动。每个选项卡都有一个列表视图。我试图使用上下文菜单通过长按删除列表项。我所拥有的将在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;
}
}

我的问题是,如何让第二个标签从列表和数据库中删除列表项?

1 个答案:

答案 0 :(得分:1)

你有单独的fillData()和fillDatapremix()函数以及2个独立的函数来从你的2个数据库中检索游标,但是你的onContextItemSelected函数看起来就像你为每个列表使用相同的DatabaseManger删除函数和fillData()函数

看起来您需要为第二个数据库使用不同的删除功能,然后使用fillDatapremix()更新listView而不是fillData()。