54

I'm making class like as below

// All necessary imports are here

public class More extends Activity {

    String[] MoreItems = { "Transfers", "Budgets", "Branches", "Tools", "News",
            "Customer Service", "Settings", "Help", "About" };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.more_view);

        ListView moreListView = (ListView) findViewById(R.id.moreListView);
        MoreListAdapter listAdapter = new MoreListAdapter();
        moreListView.setAdapter(listAdapter);

        // accountsTypeListView.setOnItemClickListener(listClickListner);
    }

    class MoreListAdapter extends ArrayAdapter<String> {
        MoreListAdapter() {
            super(More.this, R.layout.list_item, MoreItems);
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            View row;

            if (convertView == null) {
                LayoutInflater inflater = getLayoutInflater();
                row = inflater.inflate(R.layout.list_item, parent, false);
            } else {
                row = convertView;
            }
            TextView tv = (TextView) row.findViewById(R.id.textItem);

            tv.setText(getItem(position));

            return row;
        }
    }
}

It will generate the List, I want to call respective activities on respective click, like if User click Transfer then it will show transfer Activity, How can I call onClickListener on this list and how can I start Activity on click.

António Almeida
  • 9,620
  • 8
  • 59
  • 66
Chatar Veer Suthar
  • 15,541
  • 26
  • 90
  • 154

4 Answers4

62

you can also do like this..

moreListView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub
        Log.d("############","Items " + MoreItems[arg2] );
    }
});
kometen
  • 6,536
  • 6
  • 41
  • 51
Sujit
  • 10,512
  • 9
  • 40
  • 45
49

There are two option to handle click event for each row.

1) If your class extends ListActivity, you can override following method.

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
  super.onListItemClick(l, v, position, id);
  //do something here using the position in the array
}

2) Handle click event of row in getView() method

row.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

    }
});
Sabeeh
  • 1,123
  • 9
  • 11
Dharmendra
  • 33,296
  • 22
  • 86
  • 129
  • 2nd method crashes after onclik on the item of searched/refreshed in list view while calling intent inside the onclick method ! any solution for that?.. the problem comes when we use our own search filter. – LOG_TAG Jan 15 '13 at 08:25
  • Necro, but the second method would be great if you need to get the screen position of any children in a list item. – Wenger Jun 05 '14 at 14:17
14

For example:

ListView lv = getListView();
lv.setAdapter(listAdapter); 
lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
        Intent i = new Intent(More.this, NextActvity.class);
        //If you wanna send any data to nextActicity.class you can use
        i.putExtra(String key, value.get(position));
        startActivity(i);
    }
});
kometen
  • 6,536
  • 6
  • 41
  • 51
April Smith
  • 1,810
  • 2
  • 28
  • 52
1

Your "More" class has to extend ListActivity instead of Activity, then you can override onListItemClick

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    //do something here using the position in the arrya
}

Edit: Forgot to say, in your layout your ListView has to be called

android:id="@android:id/list"
kometen
  • 6,536
  • 6
  • 41
  • 51
Johann
  • 12,158
  • 11
  • 62
  • 89