ListView獲取選中和長按菜單對應的item的_id值

首先,如果是用simpleCursorAdapter填充ListView的數據時,必須注意SQLite的主鍵命名,由於simpleCursorAdapter的方法只識別_id,所以,當你用到SQLite的simpleCursorAdapter時,必須把數據表的主鍵命名爲_id。否則就會出現java.lang.IllegalArgumentException: column '_id' does not exist錯誤


(1)言歸正傳,當我們點擊ListView時,會調用ListView的點擊事件,那麼點擊的這個item的對應的數據庫的_id的值,我們可以通過以下代碼獲取

        //添加點擊  
        listView.setOnItemClickListener(new OnItemClickListener() {  
  
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {  
            	
                //setTitle("點擊第"+arg2+"個項目"); 
                String mytitle = null;
                String mycontent = null;
                
                switch(arg0.getId())
                {
                case R.id.ListView01:
                	ListView templist = (ListView)arg0;
                	View mView = templist.getChildAt(arg2);
                	mytextview = (TextView) mView.findViewById(R.id.ItemTitle);
                	mytitle = mytextview.getText().toString();
                	
                	mysqlhelper.db = mysqlhelper.mOpenHelper.getReadableDatabase(); 
                	Cursor cur=mysqlhelper.db.rawQuery("select Content from Table_1 where Title = ?",new String[]{mytitle});
                	int count=cur.getCount();
                	cur.moveToFirst();
                	mycontent=cur.getString(0);
                	
                	cur.close();
                	mysqlhelper.db.close();
                	
                	Intent intent = new Intent(view_Activity.this, modify_Activity.class);  
                	intent.putExtra("Title",mytitle);
                	intent.putExtra("Content",mycontent);
                	startActivityForResult(intent, SUBACTIVITY2);
                	break;
                }
            }  
        });  
          
        //添加長按點擊  
        listView.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {  
              
            public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {  
                menu.setHeaderTitle("確定刪除?");     
                menu.add(0, 0, 0, "OK");  
                menu.add(0, 1, 0, "Cancel");     
            }  
        }); 

(2)另外,我們可能還會用到長按菜單(上下文菜單),當我們在ListView上長按時,想獲得長按的這個ListViewitem_id的值,參考下面這段代碼

        //長按菜單響應函數  
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		ContextMenuInfo menuInfo = (ContextMenuInfo) item.getMenuInfo();     
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo(); 
        int id = (int)info.id;//這裏的info.id對應的就是數據庫中_id的值
        
		switch(item.getItemId())
		{
		case 0:
			ListView templist = listView;
        	View mView = templist.getChildAt(id);
        	mytextview = (TextView) mView.findViewById(R.id.ItemTitle);
        	String title1 = mytextview.getText().toString();
        	//setTitle("點擊了長按菜單裏面的第"+title1+"個項目"); 
        	mysqlhelper.DeleteItem(title1); //刪除這條記錄
        	
			break;
		case 1:
			break;
		}
		return super.onContextItemSelected(item);
	}


menu.add方法的參數:
    
第一個int類型的group ID參數,代表的是組概念,你可以將幾個菜單項歸爲一組,以便更好的以組的方式管理你的菜單按鈕。
第二個int類型的item ID參數,代表的是項目編號。這個參數非常重要,一個item ID對應一個menu中的選項。在後面使用菜單的時候,就靠這個item ID來判斷你使用的是哪個選項。
第三個int類型的order ID參數,代表的是菜單項的顯示順序。默認是0,表示菜單的顯示順序就是按照add的顯示順序來顯示。

第四個String類型的title參數,表示選項中顯示的文字。



發佈了141 篇原創文章 · 獲贊 57 · 訪問量 39萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章