-1

My app contains 3 tabs.All the tabs contains List so I have used ListFragment. There is a button in each ListItem. I want to do "something" when "Click" button is clicked within the ListItem as shown in the figure.

ListFragment How do I implement this. There are tuts about doing same thing for ListActivity but not ListFragment.Thanks in advance. :)

AnonymousCoder
  • 582
  • 2
  • 5
  • 18
  • I have done everything accept what I have mentioned in the question :) How do we get the id of element clicked inside OnItemClickListener() ? – AnonymousCoder Jun 11 '14 at 18:53

1 Answers1

3

First of all, you need to implement a custom adapter for your ListView. Please, read this article if you are not familiar with this.

Next, you have to disable click on ListView items. If you don't know how, check this out.

Now, in your getView method from your custom adapter, you can find your Button and set up an onClickListener, but only after you have inflated the view for the current ListView position.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.label);
    textView.setText(values[position]);

    Button mButton = (Button) view.findViewById(R.id.my_button_id);
    mButton.setOnClickListener(mClickListener);

    return rowView;
}
Community
  • 1
  • 1
DDsix
  • 1,966
  • 1
  • 18
  • 23
  • Thanks alot :) I did it! Your answer was very helpful! – AnonymousCoder Jun 12 '14 at 18:22
  • I have one more doubt. When I delete some records(ListItem) from the sqlite table. It should be reflected in the ListFragment. How to refresh the ListView? – AnonymousCoder Jun 13 '14 at 18:24
  • First of all, I'd recommend you not to work directly with the database, for it works really slow, but keep the data in some sort of cache memory and update your UI from that and sync the database. Second of all, if your data has been changed and you want to update your listview, just call adapter.notifyDataSetChanged(), where adapter is tha adapter that was previously set on your listview. – DDsix Jun 13 '14 at 20:32
  • But I am updating the tables inside the Custom Adapter Using a Dialog. I want the ListView to be updated when the Dialog is dismissed. We cant use **adapter.notifyDataChanged()**, However we can use just **notiftDataChanged**, inside the Custom Adapter but that doesn't work! – AnonymousCoder Jun 13 '14 at 21:28