Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android, Custom ListAdapter get TextView-Text

I coded an own Adapter and added it to my ListActivity via an ListView. The reason why I wrote an own Adapter is, that i had to make some layout changes to the list-entrys. In every entry of the list i've got 3 TextViews.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<TextView
    android:id="@+id/myNr"
    android:layout_width="40dip"
    android:layout_height="fill_parent"
    android:layout_marginRight="15dip"
    android:text="id" 
    android:textSize="25dip"
    android:background="#333333"
    android:gravity="center_horizontal"/>
<LinearLayout
    android:orientation="vertical"
    android:layout_width="0dip"
    android:layout_weight="1"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/editor"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:gravity="center_vertical"
        android:text="Editor: " />
    <TextView
        android:id="@+id/date"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1" 
        android:singleLine="true"
        android:ellipsize="marquee"
        android:text="Date: " />
</LinearLayout>

The next thing i did, was to implement a "onListItemClick-Methode". Afterwards i implemented a onListItemLongClick - Listener with the following code:

in onCreate of the Activity i added:

 registerForContextMenu(getListView());

then i added the following methode:

Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo){
         AdapterView.AdapterContextMenuInfo info;
         info = (AdapterView.AdapterContextMenuInfo) menuInfo;
            long id = getListAdapter().getItemId(info.position);
        }

where "id" is the index of the item in the list. I now want to get the Text of the texview with the id="myNr of this ListItem. Is there any way get this Text?

like image 681
Ripei Avatar asked Nov 12 '09 19:11

Ripei


4 Answers

view is the list item, so you can do

String s =(String) ((TextView) view.findViewById(R.id.myNr)).getText();
like image 83
David Hedlund Avatar answered Nov 20 '22 01:11

David Hedlund


just take reference of of the text view if you extending a BaseAdapter like

Listener = new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {

TextView tx =(TextView)view.findViewById(R.id.text);
            String s = tx.getText().toString();
            Log.d(TAG, "string : "+s);
like image 44
Subhajit banerjee Avatar answered Nov 20 '22 03:11

Subhajit banerjee


I found another way to set an ItemLongClickListener. Therefore I also found a way to get the Text I am displaying.

ListView lv = getListView();
lv.setOnItemLongClickListener(new OnItemLongClickListener(){
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,int row, long arg3) {
   String[] tmp = (String[]) arg0.getItemAtPosition(row);
   //tmp[0] ist the Text of the first TextView displayed by the  clicked ListItem 
   return true;
   }});
like image 34
Ripei Avatar answered Nov 20 '22 02:11

Ripei


I found none of the above solutions to work. As an alternative you can:

getListView().setOnItemLongClickListener(new OnItemLongClickListener() 
{

   public boolean onItemLongClick(AdapterView<?> arg0, View v,int position, long id) 
   {

    Cursor c = (Cursor) arg0.getItemAtPosition(position);
    String tableValue = c.getString(1);

        return true;
   }
});
like image 4
Nik NexAndrX Avatar answered Nov 20 '22 02:11

Nik NexAndrX