Android setOnItemClickListener不工作

时间:2014-04-10 16:59:45

标签: android android-layout listview android-listview onitemclicklistener

我在listview上实现了onitemCLickListener(),但它无效。问题是,在通过自定义适配器更新我的列表视图时,我已经通过我的代码添加了一个视图。 我也试过Focusable和focusableintouch模式也是假的,并试图阻止后代,但我没有得到任何解决方案。请在下面找到我的代码。

<?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="match_parent"
android:background="@drawable/bg"
android:orientation="vertical" >

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/top_greenbox"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/backArrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:padding="10dp"
        android:layout_centerVertical="true"
        android:src="@drawable/topleft_arrow" />

    <TextView
        android:id="@+id/showdate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Today is, 07 March 2014"
        android:textColor="@android:color/white" />

    <ImageView
        android:id="@+id/nextArrow"
        android:padding="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_gravity="right"
        android:src="@drawable/topright_arrow" />
</RelativeLayout>

<ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="60dp"
    android:descendantFocusability="blocksDescendants"
    android:smoothScrollbar="true" />

</LinearLayout>

在我的自定义适配器中,我以他的方式添加视图:

LinearLayout list = (LinearLayout) view.findViewById(R.id.show_time);
View line = list.inflate(mContext, R.layout.show_time_item, null);
list.addView(line);

我的布局show_time_item是:

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

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/separator" />

<TextView
    android:id="@+id/time4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:background="#55b237"
    android:gravity="center"
    android:padding="10dp"
    android:text="9:45 AM"
    android:textColor="@android:color/white"
    android:textSize="14sp" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/separator" />

</LinearLayout>

监听

my_list.setOnItemClickListener(new OnItemClickListener() {



        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {

            Log.e("hello","hi");

            Intent i = new Intent(ABCDListActivity.this,
                    ShowABCDProfile.class);

            i.putExtra("Doc_name", name_main);
            startActivity(i);


        }

    });

自定义适配器getView()

    View view = null;
    view = inflator.inflate(R.layout.doctor_list_item, null);
    ViewHolder holder = new ViewHolder();

    holder.doc_image = (ImageView) view.findViewById(R.id.doc_img);
    holder.doc_name = (TextView) view.findViewById(R.id.doc_name);
    holder.doc_qualification = (TextView) view
            .findViewById(R.id.doc_qualification);
    holder.doc_speciality = (TextView) view.findViewById(R.id.Specialities);
    holder.doc_experience = (TextView) view.findViewById(R.id.experience);


    holder.doc_experience.setText(list.get(position)
            .get(ResponseConst.TAG_STATUS_DOCTOR_EXP).toString());
    holder.doc_name.setText((list.get(position).get(
            ResponseConst.TAG_STATUS_DOCTOR_NAME).toString()));

    JSONArray hours = avail_list.get(position);

    Log.e("hours are", "size is  " + hours.length());

    LinearLayout list = (LinearLayout) view.findViewById(R.id.show_time);

    for (byte a = 0; a < hours.length(); a++) {

        try {
            JSONObject object_avail = hours.getJSONObject(a);
            String doctor_id = object_avail
                    .getString(ResponseConst.TAG_STATUS_DOCTOR_ID);
            String hour = object_avail
                    .getString(ResponseConst.TAG_STATUS_DOCTOR_AVAILABILITY_HOUR);
            String min = object_avail
                    .getString(ResponseConst.TAG_STATUS_DOCTOR_AVAILABILITY_MIN);
            String ampm = object_avail
                    .getString(ResponseConst.TAG_STATUS_DOCTOR_AVAILABILITY_AMPM);
            String status = object_avail
                    .getString(ResponseConst.TAG_STATUS_DOCTOR_AVAILABILITY_STATUS);

            String time = hour + ":" + min + " " + ampm;
            View line = list.inflate(mContext, R.layout.show_time_item, null);
            holder.time = (TextView) line.findViewById(R.id.time4);
            if(status.equals(0))
                holder.time.setTextColor(Color.GRAY);
            else holder.time.setTextColor(Color.WHITE);
            holder.time.setText(time);
            list.addView(line);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    String drawablename = "no_image.png";
    int resID = mContext.getResources().getIdentifier(drawablename,
            "drawable", mContext.getPackageName());
    holder.doc_image.setBackgroundResource(resID);


    return view;

坚持了这么久。请帮忙

3 个答案:

答案 0 :(得分:1)

单击列表项

时,ImageView会获得焦点

将其添加到LinearLayout中的show_time_item.xml

android:descendantFocusability="blocksDescendants"

答案 1 :(得分:1)

我只是在自定义适配器的getView()中添加了点击侦听器。我仍然不知道为什么听众没有参与我的活动。

答案 2 :(得分:0)

您可以随时尝试使用onclick功能。

XML

 <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="60dp"
        android:descendantFocusability="blocksDescendants"
        android:smoothScrollbar="true"
        android:onClick="Start" />

在活动中,您只需编写一个函数Start。