In general, there is a ListView list that I fill out through the SimpleCursorAdapter . It is initialized like this:

 String[] from = new String[] {"client_tel","timeToWashStart"}; int[] to = new int[] {R.id.client_tel,R.id.timeToWashStart}; scAdapter = new SimpleCursorAdapter(MainActivity.this, R.layout.item, null, from, to, 0); lvData.setAdapter(scAdapter); getSupportLoaderManager().initLoader(0, null, MainActivity.this); 

I receive the data through CursorLoader with MySQL , the list is formed, everything is fine. But in each row of the list there are buttons to click on.

Here's the question: how do you know the _id or position of the line where the button was clicked?

and the method

 lvData.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(MainActivity.this, "position = " + position + " id= " + id, Toast.LENGTH_SHORT).show(); } }); 

for some reason it does not work ... and I just won’t understand how to set the getView() method for the adapter.

Tell me which way to think, I'm already tired of stomping around) Thank you in advance for your advice.

PS: all my code is implemented in one class, maybe this is important

 public class MainActivity extends FragmentActivity implements CompoundButton.OnCheckedChangeListener,LoaderManager.LoaderCallbacks<Cursor> { 

    2 answers 2

    The fact that setOnItemClickListener does not work can only be guessed at. Unload the code, then there will be an answer.

    To find out the position and id when you click on the button (cursorloader), you can use different options, the most obvious and simple in my opinion:


    We do custom SimpleCursorAdapter, we find our button, we hang up the listener. Next, we get its parent (an item in the list) for this item, we get the parent, this is our list, and relative to them we find the position in the list. Knowing the position we get the id:

    MyAdapter.class

     public class MyAdapter extends SimpleCursorAdapter { public MyAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) { super(context, layout, c, from, to, flags); } @Override public void bindView(View view, Context context, final Cursor cursor) { Button btn = (Button) view.findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { View parent_row = (View) v.getParent(); ListView lv = (ListView) parent_row.getParent(); final int position = lv.getPositionForView(parent_row); Log.d("...", "position = " + position); Log.d("...", "id = " + getItemId(position)); } }); super.bindView(view, context, cursor); } } 
    • one
      Thanks, really it was necessary to carry out in a separate class and to redefine bindView. What is interesting is that on each button I recorded onClick and they both work independently - Necro The Human

    To summarize, the working piece of code:

     public class MainActivity extends FragmentActivity { public String LOG_TAG = "LOG_TAG"; public ListView lvData; public MyAdapter scAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); lvData = (ListView) findViewById(R.id.lvData); ///попробую тут адаптер инициализировать String[] from = new String[] {"client_tel","timeToWashStart"}; int[] to = new int[] {R.id.client_tel,R.id.timeToWashStart}; Log.d(LOG_TAG,"формируем адаптер"); // создааем адаптер и настраиваем список /// scAdapter = new SimpleCursorAdapter(MainActivity.this, R.layout.item, null, from, to, 0); scAdapter = new MyAdapter(MainActivity.this, R.layout.item, null, from, to, 0); lvData.setAdapter(scAdapter); // создаем лоадер для чтения данных http://startandroid.ru/ru/uroki/vse-uroki-spiskom/278-urok-136-cursorloader.html // по этой ссылке описано что делает следующая строка, это получение данных getSupportLoaderManager().initLoader(0, null, MainActivity.this); } 

    And, as recommended by shwarz-andrei , we create a separate class for the adapter.

     public class MyAdapter extends SimpleCursorAdapter { public MyAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) { super(context, layout, c, from, to, flags); } @Override public void bindView(View view, Context context, final Cursor cursor) { final ImageView img = (ImageView) view.findViewById(R.id.ivImg); img.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { View parent_row = (View) v.getParent(); ListView lv = (ListView) parent_row.getParent(); final int position = lv.getPositionForView(parent_row); Log.d("...", "position = " + position); Log.d("...", "id = " + getItemId(position)); } }); super.bindView(view, context, cursor); } }