【发布时间】:2018-02-22 10:09:52
【问题描述】:
我有一个listView 和baseAdapter,我想在我的活动中使用设置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