10

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

3 Answers 3

11

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) {

            }
        });
Sign up to request clarification or add additional context in comments.

1 Comment

Is there any way to bind the onClick logic to the ArrayAdapter(or any other adapter)? I have one adapter, but multple ListViews, that are filled with it, and it would be nicer to bind it to the Adapter itself?
4

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.

Comments

-1

Once you have set your adapter using:

mListView.setAdapter(myCountryListAdapter); 

Then you can setup a click listener for the listview:

mListView.setOnParentClickListener(new  OnClickListener() {         
    public void onClick(View view,) {
            ///do what you want the click to do
    }       
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.