2011-03-11 77 views
0

我有一個列表視圖,我想獲取所選項目上的文本。獲取列表視圖文本信息

我想這

mContactList.setOnItemLongClickListener(new OnItemLongClickListener() { 

     @Override 
     public boolean onItemLongClick(AdapterView<?> arg0,View arg1,int arg2,long arg3) { 
      SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(Contacts.this); 
      SharedPreferences.Editor edit = pref.edit(); 

      n = mContactList.getItemAtPosition(arg2).toString();  
      Log.v("Contacts", "List Name: " + n); 
      edit.putString(Prefs.NAME, n); 
      showDialog(1); 
      return false; 
     } 
    }); 

但它給我這個日誌

List Name: [email protected] 

我需要做什麼?

回答

0

我認爲這個關鍵在於「查看arg1」參數。

你在listView中列出了哪些視圖?如果它是一個純文本視圖列表,那麼arg1.getText()應該做到這一點(就像Spidy所說的那樣)。

如果這是一個比較複雜的視圖包含一個TextView,那麼首先獲得與TextView的:

TextView tv = (TextView) arg1.findViewById(R.id.text_view_id); 

,然後調用gettext的()。

+0

是得到textview做的伎倆謝謝你 – tyczj

2

被點擊的項目應該是您的查看參數。 arg1在你的情況下。典型的ListView使用TextView來顯示項目。因此,將View arg1轉換爲TextView,然後使用getText()獲取文本;

String listItemSelected_TextValue = ((TextView) arg1).getText();