Search code examples
javaandroidonitemclicklistener

setOnItemClickListener not working in android 6.0.1


I have a listView with baseAdapter and I want to use set onItemClickListener in my activity and that works good in most of the phones except LG's phones with 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>

This is my listview code snippet

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");

}

Here I implemented onItemClickListener

This code is working good everywhere except android 6.0.1 on Lg phone!! Help!!


Solution

  • You didn't hookup your list view with the itemclicklistener. If you implemented the interface to the whole class, you should do this:

    listview.setOnItemClicklistener(this);