How can I set onClickListener on ArrayAdapter?

AndroidListviewAndroid Arrayadapter

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

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

    });

Solution 3 - Android

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

Solution 4 - Android

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"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChatar Veer SutharView Question on Stackoverflow
Solution 1 - AndroidSujitView Answer on Stackoverflow
Solution 2 - AndroidDharmendraView Answer on Stackoverflow
Solution 3 - AndroidApril SmithView Answer on Stackoverflow
Solution 4 - AndroidJohannView Answer on Stackoverflow