【问题标题】:Android setOnItemClickListener Not workingAndroid setOnItemClickListener 不工作
【发布时间】:2014-04-10 16:59:45
【问题描述】:

我已经在我的列表视图上实现了 onitemCLickListener(),但它不起作用。问题是,在通过自定义适配器更新我的列表视图时,我已经通过我的代码向它添加了一个视图。 我也尝试过将 Focusable 和 focusableintouch 模式设置为 false 并且还尝试阻止后代,但我没有得到任何解决方案。请在下面找到我的代码。

<?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;

卡了这么久。请帮忙

【问题讨论】:

  • 发布您的适配器getView() 代码。您只发布了 3 行。您的适配器中只有这些吗?
  • 添加了代码。请检查

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


【解决方案1】:

当您单击列表项时,您的 ImageView 将获得焦点

将此添加到LinearLayout 中的show_time_item.xml

android:descendantFocusability="blocksDescendants"

【讨论】:

  • @AnchitMittal 然后在你有监听器的地方发布更多代码。您发布了 3 行和 xml。发布您的适配器代码
  • @AnchitMittal 也将这个android:descendantFocusability="blocksDescendants" 删除为ListView :(
  • @AnchitMittal 它应该可以删除android.R.layout.simple_spinner_dropdown_item for listview
  • 移除了 ListView 的 android:descendantFocusability="blocksDescendants"。不工作
  • @AnchitMittal 相同的 xml 试过了,没问题,问题应该出在你的适配器本身
【解决方案2】:

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

【讨论】:

  • 它仍然是相同的视图,因此它也应该适用于 listview 项目单击侦听器。
  • 我知道,但不知道为什么会这样。无论如何,非常感谢您的支持
  • 这意味着还有其他问题,尽管我找不到任何问题。我什至尝试使用相同的 xml 进行交叉检查,它可以工作
【解决方案3】:

您可以随时尝试使用 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。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-08
    相关资源
    最近更新 更多