【问题标题】:setOnItemClickListener not working in android 6.0.1setOnItemClickListener 在 android 6.0.1 中不起作用
【发布时间】:2018-02-22 10:09:52
【问题描述】:

我有一个listViewbaseAdapter,我想在我的活动中使用设置onItemClickListener,并且除了LG 的Android 6.0.1 手机之外,它在大多数手机中都很好用?

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginLeft="120dp"
    >

    <ImageView
        android:id="@+id/image_country"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/india"
        android:padding="10dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="5dp"
        android:textSize="18dp"
        android:text="India"
        android:clickable="false"
        android:layout_weight="1"
        android:textColor="@color/black"
        android:id="@+id/text_country"/>

</LinearLayout>

这是我的listview 代码 sn-p

languagesListAdapter = new LanguagesListAdapter(getApplicationContext(), prepareDataCountryList());
    listView.setAdapter(languagesListAdapter);

       @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Drawable drawable = getResources().getDrawable(languagesListAdapter.languagesToTranslates.get(position).getImagges());


    linearLayoutselectLanguageToTranslate.setVisibility(View.GONE);
    mInputView.setVisibility(View.VISIBLE);
    constraintLayoutBellowKeyboard.setVisibility(View.VISIBLE);
    Toast.makeText(getApplicationContext(), "Your text will be translated in " +
            languagesListAdapter.languagesToTranslates.get(position).getLanguae(), Toast.LENGTH_SHORT).show();
    PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().putString("languageToTranslatInstantText",
            languagesListAdapter.languagesToTranslates.get(position).getCodeCountry()).apply();
    languageToTranslate = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("languageToTranslatInstantText", "en");

}

这里我实现了onItemClickListener

除了 Lg 手机上的 android 6.0.1 之外,此代码在任何地方都运行良好!! 救命!!

【问题讨论】:

  • 在这里分享日志猫
  • 这似乎是了解调试器和断点的好机会。
  • 我没有看到你设置监听器的那一行。
  • 你必须分享你的那个电话错误的日志。但我建议你使用parent.getAdapter().languagesToTranslates.get(position).getImagges() 而不是languagesListAdapter.languagesToTranslates.get(position).getImagges()。这是访问适配器的更安全的方式。
  • 如果您有以下设备之一:您是否测试过它是否进入 onItemClick 方法?

标签: java android onitemclicklistener


【解决方案1】:

您没有将列表视图与 itemclicklistener 挂钩。如果你实现了整个类的接口,你应该这样做:

listview.setOnItemClicklistener(this);

【讨论】:

  • 我做到了 evrithing 在所有设备上运行良好,除了 LG 和 android 6.0.1
  • 可能设备有软件/硬件问题
  • 没有,因为我用这个 android 版本检查了 3 台设备 LG,而且 bug 无处不在
  • 我解决了 RecyclerView 的问题和 on itemClickListener 的接口!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-12
  • 1970-01-01
  • 1970-01-01
  • 2017-11-03
相关资源
最近更新 更多