Hi people. Please help me ... I'm new to this. There is a ListView which is based on the list_row template. In list_row there are id (hidden), title and info fields. ID contains a unique number from the database. If I know the position of the item I need in the listview, how do I get the ID from the list_row I need this value. I get the position by long clicking on the item at which the context menu pops up with the delete button.

<ListView android:id=" @android :id/list" android:layout_width="fill_parent" android:layout_height="wrap_content"/> 


 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/list_selector" android:orientation="horizontal" android:padding="5dip" > <TextView android:id="@+id/id_item" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="gone" /> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/thumbnail" android:layout_toRightOf="@+id/thumbnail" android:paddingBottom="2dp" android:paddingTop="6dp" android:text="Заголовок вопроса ... " android:textColor="#040404" android:textSize="13dip" android:textStyle="bold" android:typeface="sans" /> <TextView android:id="@+id/question" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below=" @id /title" android:layout_marginTop="1dip" android:layout_toLeftOf="@+id/date" android:text="Краткий вопрос ..." android:textColor="#343434" android:textSize="11dip" /> <TextView android:id="@+id/date" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignTop=" @id /title" android:layout_marginRight="5dip" android:gravity="right" android:text="00.00.0000 00:00" android:textColor="#464b50" android:textSize="10dip" android:textStyle="bold" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignTop=" @id /question" android:layout_centerVertical="true" android:layout_marginRight="5dip" android:contentDescription="@string/star" android:src="@drawable/arrow" /> </RelativeLayout> 

    2 answers 2

    Some HTML-style approach with hidden-fields. Correctly everything is done a little wrong.

    You have a ListView adapter in which you override the getItem method, and in it you already return by position what you like, id records, some more complex data structures. However, in your case, a simple redefinition of getItemId , which can only return an int .

    Well, then either in onCreateContextMenu , or onContextItemSelected , or in the set for ListView OnItemLongClickListener get the data you need. In the case of the listener, via listView.getAdapter().getItem(position) or listView.getAdapter().getItemId(position) , in the case of using one of the first two methods, via AdapterContextMenuInfo .

       onItemLongClick(AdapterView<?> parent, View view, int position, long id) { final TextView tv = (TextView)view.findViewById(R.id.id_item); final String id = tv.getText(); } 

      id contains the text you need


      then make it all much easier:

      idsFromDB - an array of ID-Schnik records from the database (initialize it when loading records in the ListView of BD )

      in onItemLongClick () you take the ID you need like this: idsFromDB [position]

      • This is the construction public boolean onContextItemSelected (MenuItem item) {AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item .getMenuInfo (); int menuItemIndex = item.getItemId (); // check for selected option if (menuItemIndex == 0) {// user selected delete // delete the dataBaseHelper dbh = new DataBaseHelper (getApplicationContext ()); // / dbh.deleteSite (info.position); // reloading same activity again Intent intent = getIntent (); finish (); startActivity (intent); } return true; } - Kirill Mamro
      • I didn’t understand your bike at all .... updated my answer - Roman Zakharov