1

I have developed a custom adapter for listview but its onclicklistner is not working, although I am not getting any error in the log. Following is the code

public class HospiRow extends AppCompatActivity {

private List<ContactData> hospiData = new ArrayList<ContactData>();
Intent intent;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_layout);

    hospiData.add(new ContactData("Shrotri Hospital", "Datta chowk, Yavatmal", "Phone Number 07232 232383", R.drawable.call_button3));
    hospiData.add(new ContactData("Tarak Hospital","Shani chowk, Yavatmal","Phone Number 07232 232383", R.drawable.call_button3));
    hospiData.add(new ContactData("Dattani Hospital","Shivagi nagar, Yavatmal","Phone Number 07232 232383", R.drawable.call_button3));
    hospiData.add(new ContactData("Chattani Hospital","Darda nagar, Yavatmal","Phone Number 07232 232383", R.drawable.call_button3));
    hospiData.add(new ContactData("Thakare Hospital","Anne Vidyalaya, Yavatmal","Phone Number 07232 232383", R.drawable.call_button3));
    hospiData.add(new ContactData("Bade Hospital","Laxminagar, Yavatmal","Phone Number 07232 232383", R.drawable.call_button3));
    hospiData.add(new ContactData("Rathode Hospital","Balaji Society, Yavatmal","Phone Number 07232 232383", R.drawable.call_button3));

    ArrayAdapter<ContactData> hospiAdapter = new DataAdapter(this, R.layout.hospi_row, hospiData);
    ListView myList = (ListView) findViewById(R.id.myFirstListView);
    myList.setAdapter(hospiAdapter);

    myList.setOnItemClickListener(
            new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    ContactData singleItem = (ContactData) parent.getItemAtPosition(position);
                    String number = singleItem.getTelephoneNumber();
                    Toast.makeText(HospiRow.this,number, Toast.LENGTH_LONG).show();
                    intent = new Intent(Intent.ACTION_DIAL);
                    intent.setData(Uri.parse("tel:" + number));
                    startActivity(intent);
                }
            }
    );

}

}

Please help

XML code for listview is

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/myFirstListView"
        android:focusable="false"
        android:focusableInTouchMode="false"/>
</LinearLayout>

The name of the xml file is my_layout.xml

Nikesh Patel
  • 521
  • 1
  • 5
  • 13

3 Answers3

1

If any row item of list contains focusable or clickable view then OnItemClickListener won't work. The row item must have a param like

android:descendantFocusability="blocksDescendants"

Your xml for list item must look like this.

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
  android:baselineAligned="false"
 android:descendantFocusability="blocksDescendants"
 android:gravity="center_vertical" >

 // your other widgets here

 </LinearLayout>
Vishesh
  • 308
  • 1
  • 9
0

buttons or any views ,make sure those are not clickable for that item.

android:focusable="false"
android:focusableInTouchMode="false"

if it is not working with SimpleAdapter,try working with CustomListViewAdapter and there for each view,make clickable false in xml.

Nithin
  • 503
  • 4
  • 19
0

setClickable as false to row items like this in your adapter's xml file.

<TextView 
.....
android:clickable="false"
....
/>

If any row item of list contains focusable or clickable view then OnItemClickListener won't work.

Reffer Here

Community
  • 1
  • 1
sivaBE35
  • 1,876
  • 18
  • 23