我为listview开发了一个自定义适配器,但它的onclicklistner不能正常工作,尽管我没有在日志中收到任何错误。以下是代码
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);
}
}
);
}
}
请帮忙
listview的XML代码是
<?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>
xml文件的名称是my_layout.xml
答案 0 :(得分:1)
如果列表中的任何行项包含可聚焦或可点击的视图,则OnItemClickListener将无法正常工作。 行项必须有一个像
这样的参数android:descendantFocusability="blocksDescendants"
您的列表项xml必须如下所示。
<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>
答案 1 :(得分:0)
按钮或任何视图,请确保该项目无法点击。
android:focusable="false"
android:focusableInTouchMode="false"
如果它不能与SimpleAdapter一起使用,请尝试使用CustomListViewAdapter并在每个视图中使用xml中的clickable false。
答案 2 :(得分:0)
将setClickable设置为false,以便在适配器的xml文件中对此项进行排序。
<TextView
.....
android:clickable="false"
....
/>
如果列表的任何行项包含可聚焦或可点击的视图,则OnItemClickListener将不起作用。
Reffer Here