开发者

getting listview text information

i have a list view and i want to get the text on the item that is selected.

I tried thi开发者_Python百科s

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;
        }
    });

but it gives me this in the log

List Name: android.database.sqlite.SQLiteCursor@40522900

what do i need to do?


The item clicked should be your View parameter. arg1 in your case. A typical ListView uses TextView to display items. So cast the View arg1 to TextView and then grab the text with getText();

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


I think the key to this lies in the "View arg1" argument.

What kind of Views are you listing in the listView? If it is a list of plain TextViews, then arg1.getText() should do the trick (just like Spidy said).

If it is a more complex View which contains a textView, then first get the textView with:

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

and then call getText() on that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜