Search code examples
androidlistviewbuttononclicklistenersimplecursoradapter

setOnItemClickListener doesn't work for a button in a ListView


I am trying to place a button in each list item. This is the idea:

Text1____________[btn]

Text2____________[btn]

etc...

The XML for the items is:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:descendantFocusability="blocksDescendants">

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="?android:attr/listPreferredItemHeight"
        android:gravity="center_vertical"
        android:id="@+id/list_item_textview"/>

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/deleteButton"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:background="@null"
        android:clickable="true"
        android:focusable="false"
        android:src="@drawable/ic_delete_black_24dp"/>
</RelativeLayout>

There are two activities sharing the same fragment. This fragment uses a SimpleCursorAdapter to fetch the data from a database. That is working perfectly.

After creating and setting the adapter, I try to find the button and do a setOnClickListener. This is NOT working.

Here is the code for the onCreateView method in the fragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_item_scroll, container, false);

    ListView listView = (ListView) rootView.findViewById(R.id.listview_item);

    SimpleCursorAdapter adapter = new SimpleCursorAdapter(
            getActivity(),
            R.layout.list_item,
            ((ListItems)getActivity()).getCursor(1,1),
            new String[] {((ListItems) getActivity()).getColumnName()},
            new int[] {R.id.list_item_textview}, 0);

    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(getActivity(), ((ListItems) getActivity()).getGoToClass());
            intent.putExtra("apiary_id",(int) id);
            intent.putExtra("hive_id", -1);
            startActivity(intent);

        }
    });


/* SOMEWHERE BELOW IS THE PROBLEM */

    View list_item_view = inflater.inflate(R.layout.list_item, container, false);

    ImageButton deleteButton = (ImageButton) list_item_view.findViewById(R.id.deleteButton);
    System.out.println("====================>" + deleteButton.toString()); //This prints something which includes the id of the button, so it is finding it.
    deleteButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            System.out.println("THIS IS NOT WORKING!!!!");
        }
    });

    return rootView;
}

This is my first android application and I'm having this problem for a few days now without any progress. Any hint is greatly appreciated!

Thank you very much!


Solution

  • Make a custom list adapter like this

    public class CustomListAdapter extends BaseAdapter {
    private String[] items;
    private LayoutInflater inflater;
    Context context;
    public CustomListAdapter(Context c, String[] items) {
        this.items = items;
        inflater =LayoutInflater.from(c);
        context = c;
    }
    
    @Override
    public int getCount() {
        return items.length;
    }
    
    @Override
    public Object getItem(int location) {
        return items[location];
    }
    
    @Override
    public long getItemId(int position) {
        return position;
    
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null)
            convertView = inflater.inflate(R.layout.list_item, null);
        TextView txt = (TextView) convertView.findViewById(R.id.list_item_textview);
        ImageButton button = (ImageButton) convertView.findViewById(R.id.deleteButton);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "clicked ",
                        Toast.LENGTH_SHORT).show();
            }
        });
        txt.setText(""+items[position]);
    
        return convertView;
    }
    

    }

    and in your fragment

        CustomListAdapter customListAdapter;
    customListAdapter = new CustomListAdapter(getActivity(),str);
            list.setAdapter(customListAdapter);
    

    hope this will help you !!!