How does the cursor know which item to click in the list?

I have an activity displaying call logs in a ListView. The adapter used here to populate the list extends the CursorAdapter. Listview is set to onItemClickListener (..). My question is, whenever an element is clicked, how does the cursor get the data? how does the cursor know which position is pressed, and you only need to get data from the position just pressed? I have provided snippnet code.

public class CallLog extends Activity { ListView mListView; Cursor cursor; //other variables public void OnCreate() { setContentView(R.layout.calllog); //SQLiteDatabaseInstance db cursor = db.query(...parameters to get all call logs...); mListView.setOnItemClickListener(this); } public void OnItemClick( // all arguments... ) { //there is a column name 'NAME' in call log table in database String name = cursor.getString(cursor.getColumnIndex(Database.NAME)) //here name is obtained of the clicked item. } 

A cursor is a result set. how does the cursor know which element to click? What can be methods implicitly called a cursor that gives it the position of the clicked element?

If there are any links with a similar question, then pls provide.

I hope I can make you understand this question. thank you

+4
source share
4 answers

In particular, it is NOT a cursor that knows who clicked on what. This is actually handled by the adapter. The adapter is used to group elements together and allows for abstraction as such that they can be processed in a uniform way.

Any form of list always has an adapter, which is why the adapter works so well. If you look at Custom Listview with a custom adapter, you will definitely see how this is done.

Example:

http://android.vexedlogic.com/2011/04/02/android-lists-listactivity-and-listview-ii-%E2%80%93-custom-adapter-and-list-item-view/

+1
source

Try the following:

  @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //move cursor to clicked row cursor.moveToPosition(position); } 
+2
source

You must use the internal function cursor.moveToposition (position) to jump to the position of this object. After that, you apply this, and when you click on an element, the cursor will be placed on that element, and then you can use this specific element for your work.

+1
source
  mListView..setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View view, int position, long id) { // here position gives the which item is clicked.. } }); 

Additionally check this link for ListView ListView and ListActivity

It can help you.

0
source

Source: https://habr.com/ru/post/1394365/


All Articles