Rakeeb Rajbhandari
Rakeeb Rajbhandari

Reputation: 5063

ListView (button not running)

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);
        }
    });

Upvotes: 0

Views: 41

Answers (1)

Peri Hartman
Peri Hartman

Reputation: 19474

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;
    }
  }
}

Upvotes: 1

Related Questions