Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - how to add an item click method to an ArrayAdapter

I have a simple ArrayAdapter. I want to set up a listener for every row click of my list such that a new Activity opens. How would I do that? My ArrayAdapter code -

public class CountryListAdapter extends ArrayAdapter<String> {
private final Activity context;
private final ArrayList<String> names;

public CountryListAdapter(Activity context, ArrayList<String> names) {
    super(context, R.layout.rowlayout, names);
    this.context = context;
    this.names = names;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    View rowView = inflater.inflate(R.layout.rowlayout, null, true);
    TextView textView = (TextView) rowView.findViewById(R.id.label);
    textView.setText(names.get(position));
    return rowView;
}
like image 274
Suchi Avatar asked Nov 14 '11 18:11

Suchi


People also ask

Which function is automatically called when the list item view is ready to be displayed?

The ListView instance calls the getView() method on the adapter for each data element. In this method the adapter creates the row layout and maps the data to the views in the layout. This root of the layout is typically a ViewGroup (layout manager) and contains several other views, e.g., an ImageView and a TextView .

What is simple_list_item_1?

R. layout. simple_list_item_1 , which is a layout built into Android that provides standard appearance for text in a list, and an ArrayList called restaurants (not seen here).

What is Android AdapterView?

AdapterView is a ViewGroup that displays items loaded into an adapter. The most common type of adapter comes from an array-based data source.


2 Answers

Assuming you are using a ListActivity implementing OnItemClickListener you could use this code:

ArrayAdapter<Object> ad = new ArrayAdapter<Object>(this,
                android.R.layout.simple_list_item_checked, items);
        setListAdapter(ad);
        ListView list = getListView();
        list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        //list.setItemChecked(0, true);
        list.setOnItemClickListener(this);

EDIT: Otherwise, if you don't extend ListActivity, have a listview in your layout and replace ListView list = getListView() with something like ListView list = findViewById(R.id.listView). Replace list.setOnItemClickListener(this) with

list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });
like image 58
Force Avatar answered Nov 07 '22 00:11

Force


Simply implement AdapterView.OnItemClickListener.

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
    Intent i = new Intent(this, ProductActivity.class);
    i.putExtra("item_id", manager.getItemIdAtIndex(pos));
    startActivity(i);
}

Then just set the class with that method as the onItemClickListener in your adaptor.

like image 27
Matthew Runo Avatar answered Nov 07 '22 00:11

Matthew Runo