Listview与按钮OnItemClickListener没有调用

时间:2014-08-17 19:26:03

标签: android xml listview android-listview onitemclicklistener

我使用listview和2 TextView创建了自定义ImageButton。但是onitemclicklistner没有打电话。这是我的代码

    police_station_list = (ListView) findViewById(R.id.police_station_listView_xml);
    adapter = new ArrayAdapter<String>(this, R.layout.coustome_listview, R.id.district_listview_xml1, police_station_name);
    PSAdapter = new Police_Station_Adapter(this);
    police_station_list.setAdapter(PSAdapter);

    police_station_list.setOnItemClickListener(PSAdapter);

这是我的自定义ArrayAdapter课程:

public class Police_Station_Adapter extends ArrayAdapter<String> implements OnItemClickListener{
Context context;

public Police_Station_Adapter(Context context) {
    //super(context, R.layout.police_station_listview, R.id.police_station_listView_textView, police_station_name);
    super(context, R.layout.police_station_listview, police_station_name);
    this.context = context;
}

private class ViewHolder {
    TextView tv;
    ImageButton mobile, phone;
    public ViewHolder(View v) {
        tv = (TextView) v.findViewById(R.id.police_station_listView_textView);
        mobile = (ImageButton) v.findViewById(R.id.police_station_listView_imageButton_mobile);
        phone = (ImageButton) v.findViewById(R.id.police_station_listView_imageButton_phone);
    }
}

ViewHolder holder;

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;

    if(v == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.police_station_listview, parent, false);
        holder = new ViewHolder(v);
        v.setTag(holder);
    }
    else {
        holder = (ViewHolder) v.getTag();
    }

    holder.tv.setText(police_station_name[position]);


    holder.mobile.setTag(position);

    holder.mobile.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            new Message(context, police_station_name[(Integer) v.getTag()]+" button", 500);
        }
    });


    return v;
}

@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
    new Message(context, "Item Clicked", 500); //Toast message

}

}

以下是自定义ListView xml:

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

    <TextView
        android:id="@+id/police_station_listView_textView"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="8"
        android:textSize="16sp"
        android:layout_marginRight="10dp"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <ImageButton
        android:id="@+id/police_station_listView_imageButton_mobile"
        android:layout_gravity="center_vertical"
        android:layout_width="27dp"
        android:layout_height="30dp"
        android:layout_weight="1"
        android:layout_marginRight="15dp"
        android:background="@drawable/mobile_icon" />

    <ImageButton
        android:id="@+id/police_station_listView_imageButton_phone"
        android:layout_gravity="center_vertical"
        android:layout_width="27dp"
        android:layout_height="30dp"
        android:layout_weight="1"
        android:layout_marginRight="10dp"
        android:background="@drawable/telephone_icon" />

</LinearLayout>

它似乎可以为textview添加onClikcListner,但它不会显示任何按下的动画。它可能不是合适的解决方案。我需要setOnItemClickListner的正确方法。

2 个答案:

答案 0 :(得分:1)

您需要在Activity中设置 OnItemClickListener 。然后改变这个

 police_station_list.setOnItemClickListener(PSAdapter); to 
 police_station_list.setOnItemClickListener(this);

从适配器中删除 OnItemClickListener 的实现。 Check this tutorial

答案 1 :(得分:0)

将此属性添加到imageButton:

 android:focusable="false"