0

Practicing on the ListView, I thought of adding buttons as well to it, rather than showing only content. But in my implementation, the button does not do anything at all.

Plus I was confused whether I could get the position of the button clicked. For now I am just sending the toSend declared inside the OnItemClick in an intent.

listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            // TODO Auto-generated method stub
            final int toSend = position;
            TextView refId = (TextView)view.findViewById(R.id.list_id);
            TextView refName = (TextView)view.findViewById(R.id.list_name);
            TextView refAdd = (TextView)view.findViewById(R.id.list_address);
            Button edit = (Button)view.findViewById(R.id.edit);
            edit.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View view) {
                    // TODO Auto-generated method stub
                    Intent i = new Intent(ListActivity.this, EditLayout.class);
                    i.putExtra("position", toSend);
                    startActivity(i);
                }
            });
            String sd_id = refId.getText().toString();
            String sd_name = refName.getText().toString();
            String sd_add = refAdd.getText().toString();
            buildAlert(sd_id, sd_name, sd_add);
        }
    });
Rakeeb Rajbhandari
  • 5,043
  • 6
  • 43
  • 74
  • 1
    I believe this is what you're looking for: http://stackoverflow.com/questions/1709166/android-listview-elements-with-multiple-clickable-buttons?rq=1 – Bedimindtricks Nov 15 '13 at 00:14

1 Answers1

1

You're pretty close. The "inside" setOnClickListener needs to happen when you create the list row view (the view containing id, name, address, edit).

You can do that during getView(). But where to send the clicks? Instead of creating a new onClickListener, use "this" (your activity). Put an onClick() handler in the activity.

Then, when you get a click, the onClick method will execute. Next problem: how do you know which row clicked? The easiest way that comes to mind is to give the button a different id for e ach row - use the row index (you might need to start at 1 rather than 0 - be warned).

Finally, given the row id, it's easy to start your "nested" activity.

Hope this helps.

(added later) I do it like this; you'll need to define a layout for your row view:

class MyActivity extends Activity implements View.OnClickListener
{
  public void onCreate (Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView (R.layout.my_page);

    ListView list = (ListView)findViewById (android.R.id.list);
    MyArrayAdapter adapter = new MyArrayAdapter (this, <your array of data>);
    list.setAdapter (adapter);
  }

  @Override
  public void onClick(View v)
  {
    int buttonId = v.getId();
    if (buttonId is within range)
      ... do what you need to do with the click ...
  }

  private class MyArrayAdapter extends ArrayAdapter<MyData>
  {
    private Activity act;

    //-----------------------------------------------------------------------------
    public MyArrayAdapter (Activity act, MyData array)
    {
      super (act, R.layout.list_row, array);
      this.act = act;
    }

    //-----------------------------------------------------------------------------
    @Override
    public View getView (int position, View convertView, ViewGroup parent)
    {
      ViewGroup rowView = (ViewGroup)convertView;
      if (rowView == null)
      {
        LayoutInflater inflater = act.getLayoutInflater();
        rowView = (ViewGroup) inflater.inflate (R.layout.list_row,
                                                parent, false);
      }

      Button button = (Button)rowView.findViewById (R.id.is_member);
      button.setId (position+1); // add one to avoid 0 as an id.
      button.setOnClickListener (act);

      // set field values here -- not shown

      return rowView;
    }
  }
}
Peri Hartman
  • 19,314
  • 18
  • 55
  • 101
  • I get what you're talking about ... but this requires a custom view right ?? I am currently using a `SimpleCursorAdapter`. Is this possible inside this generic adapter ? – Rakeeb Rajbhandari Nov 15 '13 at 00:19
  • 1
    Yes you can; I updated my answer with sample code. It may have some syntax errors as I cut and pasted it from my app, taking out parts that weren't relevant. Oh, my example is built of an ArrayAdapter, but it will also work from extending a SimpleCursorAdapter. – Peri Hartman Nov 15 '13 at 00:33