0

I'm making an inventory tracker app for Android for a practice project. I use SQLite for the storage and my ListView displays the contents of the database using a CursorAdapter. I use CursorLoader to fetch the data from the database

Each row in my ListView has a couple of TextViews and a Button. I plan to use the Button to decrement the quantity column/property of the selected item in the database.

Where do I setup the button OnClick listener? In my Activity class or my CursorAdapter class' bindView()?

Also how can I detect which row the button was pressed on from the button click?

I've already used the ListView's onItemClickListener to send the user to a detailed Activity that display more info about the current row. That had an id argument that gets passed. So I'm finding an equivalent that I can use for the buttons I put on each row.

fishhau
  • 459
  • 1
  • 3
  • 15

5 Answers5

0

Look the following. I used the Custom adapter to add the onItemClickListener:

private class MyListAdapter(val ctx: Context, val values: List<MyListInformatin>) : ArrayAdapter<MyListInformatin>(ctx, -1, values) {
    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
         val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        val rowView = inflater.inflate(R.layout.item_my_layout, parent, false)
        rowView.setOnClickListener {
            val detailsActivity = Intent(ctx, DetailsActivity::class.java)
            detailsActivity.putExtra(DetailsActivity.ROUTE_NUMBER_PARAM, values.get(position).routeNumber)
            val bundle = Bundle()
            bundle.putParcelable(detailsActivity.ROUTE_NO_STRUCTURE_PARAM, values.get(position).strucutre)
            detailsActivity.putExtra(detailsActivity.PARAM_BUNDLE, bundle)
            ctx.startActivity(detailsActivity)
        }
        return rowView
    }
}
Tobias Lukoschek
  • 355
  • 1
  • 3
  • 20
0

You can create an interface inside your adapter and a declare a fucntion in this interfae like ** void itemClick(int position)** and implement that interface in your activity

for void itemClick(int position) method :- Define it on

holder.setOnClickListener() block like -

 viewHolder.button.setOnClickListener(new OnClickListener() {  

            @Override  
            public void onClick(View v) {  
                if (yourInterface != null) {  
                    yourInterface.itemClick(position);  
                }  

            }  
        }); 

For detailed description refer the below link -

https://www.c-sharpcorner.com/UploadFile/9e8439/create-custom-listener-on-button-in-listitem-listview-in-a/

Ankita
  • 1,129
  • 1
  • 8
  • 15
0

This question has been answered already, you need to setup your list items as not clickable and manually listen for onClick events of buttons, see a detailed answer here: Android : How to set onClick event for Button in List item of ListView

0
public class MyCustomAdapter extend Adapter {
    private MyOnClickListener mListener;

    @Override
    public View getView(int position, View view, ViewGroup parent)
    {

        ...

        // Your view
        mContainer.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                mListener.onWholeItemClick(position);
            }
        });

        // Your button
        mButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                mListener.onButtonClick(position);
            }
        });
    }

    public void setListener(MyOnClickListener listener) {
        this.mListener = listener;
    }

    // Listener interface
    public interface MyOnClickListener() {
        void onWholeItemClick(int position); // Can pass data as a parametter
        void onButtonClick(int position); 
    }
}

public class MyActivity extend Activity implement MyOnClickListener() {
    ...

    @Override
    public void onCreate(...) {
        mAdapter = new MyCustomAdapter(this);
        mAdapter.setListener(this); // Pass the listener to adapter
    }

    @Override
    public void onWholeItemClick(int position) {
        // 
    }

    @Override 
    public void onButtonClick(int position) {
        //
    }

}

That how you can fully hanhle click events

Liar
  • 1,235
  • 1
  • 9
  • 19
0

In onBindViewHolder give a tag to that button. that tag can be the position of item or the item itself . ( tag can be object )

 override fun onBindViewHolder(holder: ItemHolder, position: Int) {
   CUSTOMMODEL item = items[position];
   ...
   holder.button.setTag(item);
   //OR
   holder.button.setTag(position);
   ...
   holder.button.setOnClickListener(clickListener);
}

in your onClick(View view) method, get tag from the view and you can understand which item is clicked.

Omid Heshmatinia
  • 5,089
  • 2
  • 35
  • 50