Simple SimpleAdapter and simpleadapter for Android

Source: Internet
Author: User

Simple SimpleAdapter and simpleadapter for Android

SimpleAdapter is the most basic and commonly used. Although you can implement your own adapter (baseadapter, etc.), it is complicated and unnecessary.

package;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.content.Intent;import android.os.Bundle;import;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.ListView;import android.widget.SimpleAdapter;public class MainActivity extends ActionBarActivity {private ListView lView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);lView = (ListView)findViewById(;lView.setAdapter(new SimpleAdapter(this, getdata(), R.layout.mylist_item,new String[]{"title",}, new int[]{}));lView.setOnItemClickListener(new OnItemClickListener() {@SuppressWarnings("unchecked")@Overridepublic void onItemClick(AdapterView<?> parent, View view,int position, long id) {Map<String, Object> map = (Map<String, Object>)parent.getItemAtPosition(position);Intent intent =(Intent)map.get("intent");startActivity(intent);}});}private List<Map<String, Object>> getdata() {List<Map<String, Object>> myData = new ArrayList<Map<String,Object>>();addItem(myData,"title01",new Intent(this, Demo01.class));addItem(myData,"ttile02",new Intent(this, Demo02.class));return myData;}private void addItem(List<Map<String, Object>> myData, String string,Intent intent) {Map<String, Object> map = new HashMap<String, Object>();map.put("title", string);map.put("intent", intent);myData.add(map);}}
You can customize the layout file R. layout. mylist_item.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.