-2

I have created a Custom listView where i put some text and image but when i clicked in list view their is no response.I have created a custom Listview. if any one known then please tell me. my code is here

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final List<pdf> pdfList;

    //the listview
    final ListView listView;

        //initializing objects
      pdfList = new ArrayList<>();
        listView = (ListView) findViewById(R.id.list);

        //adding some values to our list
      pdfList.add(new pdf("Book1", "First Year"));
     pdfList.add(new pdf( "Book2", "First Year"));
        //creating the adapter
        ListAdptot adapter = new ListAdptot(this, R.layout.customlistview, pdfList);

        //attaching adapter to the listview
        listView.setAdapter(adapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                    @Override
                                    public void onItemClick (AdapterView < ? > adapter, View view, int position, long arg){

                                       if(position==0)
                                       {
                                           Toast.makeText(MainActivity.this, "Position 0", Toast.LENGTH_SHORT).show();
                                       }

                                      else if(position==1)
                                        {
                                            Toast.makeText(MainActivity.this, "Position 1", Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                }
    );
    }
}
Aakash Jain
  • 87
  • 10

2 Answers2

2

Get list item positon click as below

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                @Override
                                public void onItemClick (AdapterView < ? > adapter, View view, int position, long arg){

                                  String value = listView.getItemAtPosition(position).toString();
                                }
                            }
);
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
mehul chauhan
  • 1,792
  • 11
  • 26
0

If you have any clickable views inside your list item, then it will consume click event of your ListView item click.

Set android:focusable="false" on your Button or ImageButton if you have any.

Reference SO answer

Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212