Android, Custom ListAdapter gets TextView-Text

I encoded my own adapter and added it to my ListActivity via ListView. The reason I wrote my own adapter is because I had to make some changes to the list in the list entry. In each list entry, I have 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 implement the "onListItemClick-Methode". Subsequently, I implemented onListItemLongClick-Listener with the following code:

in onCreate of Activity I added:

  registerForContextMenu(getListView()); 

then I added the following method:

 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. Now I want to get texview text with id = "myNr of this ListItem. Is there any way to get this text?

+7
android listview adapter
source share
12 answers

Hi guys, I found another way to set ItemLongClickListener. So I also found a way to get the text that I am showing.

 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; }}); 
+6
source share

view is a list item, so you can do

 String s =(String) ((TextView) view.findViewById(R.id.myNr)).getText(); 
+18
source share

just take a link to the text view if you extend the BaseAdapter , for example

 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); 
+9
source share

I did not find any of the above solutions to work. Alternatively, 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; } }); 
+4
source share

The best solution I found was:

 @Override public void onCreateContextMenu(ContextMenu menu, View view,ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, view, menuInfo); AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo; ViewGroup vg = (ViewGroup) view; View children = vg.getChildAt(info.position); TextView child = (TextView) children.findViewById(R.id.text1); String s= child.getText().toString(); menu.setHeaderTitle(s); menu.add(0, view.getId(), 0, "Serve"); menu.add(0, view.getId(), 0, "View"); } 
+3
source share

If you are looking for a TextView in an element position, a person with a long press, you can get it using something like this:

 String s = ((TextView) view.getItemAtPosition(info.position)).getText(); 
+2
source share

Yes, I thought so. But when I implement this, I just get the text of the first TextView in the list. This means that I can get the text of the desired TextView, but it is always the text of the first entry in the list. Is it possible to add a longClickedList entry?

0
source share

Thank you for your help, but I am afraid that it still does not work. if I add the line that you suggested, it underlines "getItemAtPosition" and says: - The getItemAtPosition (int) method is undefined for the View type -

He suggests throwing a view into the AdapterView, so it looks like this:

 String s = ((TextView) ((AdapterView<ListAdapter>) view).getItemAtPosition(info.position)).getText(); 

When I run this and make a long click on the item, I get a "ClassCastException"

0
source share

In this method, you will get the item associated with your list item.

 @Override public boolean onContextItemSelected(MenuItem aItem) { AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) aItem.getMenuInfo(); Object entity = (Object) todoList.getAdapter().getItem(menuInfo.position); switch (aItem.getItemId()) {} } 
0
source share
 list.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view,int position, long id) { String title = (String) ((TextView)view.findViewById(R.id.title)).getText(); String artist=(String)((TextView)view.findViewById(R.id.artist)).getText(); //System.out.println(s); Toast.makeText(CustomizedListView.this,"Title = "+title+"\n"+"Artist = "+artist, Toast.LENGTH_SHORT).show(); } }); 
0
source share

I had an ArrayList from ArrayLists as the data source for the CustomAdapter, so I use

 menu.setHeaderTitle(Messages.get(info.position).getUserName()); 
0
source share

using HashMap

 getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { HashMap m = (HashMap) getListView().getItemAtPosition(position); System.out.println("itemClick: position = " + position + ", id = " + id + " o = " + m.get("YOUR_KEY")); } }); 
0
source share

All Articles