Search code examples
androidlistviewandroid-adapterlistadapter

onListItemClick not working in my custom ListActivity


I'm new to Android development. I'm learning about it at the moment. This isnt the first practice app I've done, its one of the first few.

The aim of this app is: The screen should display a list of items. Each item should display a thumbnail of an image thats in a particular folder on the SD card, and the filename. Clicking on the row should open that image into full screen.

I have the items displaying in a list, but the items arent responding to any clicks. My onListItemClick() is not being called. Nothing is appearing in logcat when I do a click on a row.

I've tried a lot of things myself and cannot get it to work. When I learnt about this, I was shown an example.

I've also done some reading about it on SO and reading of tutorials. For example, I did see a similar question on SO. One answer was to add this to a Textview:

android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"

So I tried adding that to my TextView and ImageView and still no luck.

Appreciate any help with this, its really frustrating.

Heres the initial activity:

public class MainActivity extends ListActivity {

    private PictureListAdapter mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mAdapter = new PictureListAdapter(getApplicationContext());
        readImages();
        setListAdapter(mAdapter);
    }

    @Override
    protected void onListItemClick(ListView listView, View v, int pos, long l){
        File selectedFile = (File) getListAdapter().getItem(pos);
        Intent intent = new Intent(getApplicationContext(), ImageActivity.class);
        intent.putExtra("fullImage", BitmapFactory.decodeFile(selectedFile.getAbsolutePath()));
        startActivity(intent);
    }

    private void readImages(){
        File dir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        for(File f : dir.listFiles()){
            mAdapter.add(f);
        }
    }

}

And here is my adapter:

public class PictureListAdapter extends BaseAdapter {

    List<File> mItems = new ArrayList<>();
    Context context;

    //Other implements methods here

    public PictureListAdapter(Context context){
        this.context = context;
    }

    public void add(File file){
        mItems.add(file);
        notifyDataSetChanged();
    }

    @Override
    public View getView(int pos, View convertView, ViewGroup parent) {
        File picture = mItems.get(pos);

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout view = (LinearLayout) inflater.inflate(R.layout.picture_layout, parent, false);
        view.setClickable(true);

        ImageView imageView = (ImageView) view.findViewById(R.id.thumbnail);
        final Bitmap fullImage = BitmapFactory.decodeFile(picture.getAbsolutePath());
        imageView.setImageBitmap(Bitmap.createScaledBitmap(fullImage, 160, 120, true));

        TextView textView = (TextView) view.findViewById(R.id.image_filename);
        textView.setText(picture.getName());
        return view;
    }

}

And finally, here is the XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/thumbnail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="The thumbnail" />

    <TextView
        android:id="@+id/image_filename"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

EDIT: I've got the click working now. I just removed the line in the adapter that said:

view.setClickable(true);

I dont understand it though. It starts being clickable when I no longer tell it to be clickable? That doesnt make sense. Can anyone explain this please?

I am now getting another error by the way, but I am atleast hitting the onListItemClick() method which is what this question was about. I'm now getting Failed Binder Transaction, but I think I know whats causing it and will fix it tomorrow.


Solution

  • It was so because your view intercepted click event. Now you removed clickable property from view what allowed list to process clicks.