2012-06-06 21 views
1

我有一個使用兩個獨立數據庫的標籤主機活動。每個選項卡都有一個列表視圖。我正在嘗試使用上下文菜單通過長按單擊來刪除列表項。我擁有的將在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

你有單獨的fillData()和fillDatapremix()函數和2個獨立的函數來檢索你的2個數據庫中的遊標,但是它看起來像在你的onContextItemSelected函數中,你正在使用相同的DatabaseManger刪除函數和fillData()函數名單。

看起來您需要爲第二個數據庫使用不同的刪除函數,然後使用fillDatapremix()更新listView而不是fillData()。

+0

非常感謝!那就是訣竅。 –