参考文章:http://blog.csdn.net/a_large_swan/article/details/7535337
数据源不同而已
1. String[]: ArrayAdapter
2. List<Map<String,?>>: SimpleAdapter
3. 数据库Cursor: SimpleCursorAdapter
第一种参考
如果仅仅将数组的内容显示到ListView 上ArrayAdapter就够了 public classTest extends ListActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); String[] sw = new String[10]; for (int i = 0; i < 10; i++) { sw[i] = "List_" + i; } ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,sw);//使用系统已经实现好的xml文件simple_list_item_1 setListAdapter(adapter); } }
大概就是这三种,第一种的话一定要是string[]数组才可以,不能用ArrayList<string>,需要转换一下才可以使用。
写一下第二种用法:
一,mainActivity.class
1,数组类型:
ArrayList<HashMap<String,Object>>
2,适配器类型
SimpleAdapter/**第一个参数:context
第二个参数:数组
第三个参数:适配器样式布局
第四个参数:hashmap中需要显示出来的key值
第五个参数:适配器样式布局中与hashmap中需要显示出来的key值对应的控件
//显示分类列表 listItem=getALLlist(); SimpleAdapter listadapet=new SimpleAdapter(sortlist_java.this, listItem, R.layout.list_adapter, new String[]{"image","task_type"}, new int[]{R.id.imageView2,R.id.textView3});
R.layout.list_adapter界面
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="45dp" android:background="@color/white" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context=".MainActivity"> <ImageView android:layout_width="30dp" android:layout_height="30dp" android:id="@+id/imageView2" android:background="@drawable/type_two" android:layout_marginTop="10dp" android:layout_marginLeft="10dp" android:layout_alignBottom="@+id/textView3" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Text" android:id="@+id/textView3" android:textSize="20dp" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginBottom="5dp" android:layout_marginLeft="30dp" android:layout_marginTop="15dp" /> </LinearLayout>
三、数组取值问题:
ArrayList<HashMap<String,Object>>
//获取数据库的所有待办信息到List public ArrayList<HashMap<String,Object>> getALLlist_todo() { MySqliteHelper mySqliteHelper=new MySqliteHelper(this,"mydata.db",null,1); //mydata.db的数据库 ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String , Object>>(); SQLiteDatabase db = mySqliteHelper.getReadableDatabase(); Cursor c = db.query("my_table", new String[]{"_id", "task", "date", "task_type","cycle_time","remark","state","done_time"}, null, null, null, null, null); if (c != null) { while (c.moveToNext()) { HashMap<String, Object> item = new HashMap<String, Object>(); if(c.getString(c.getColumnIndex("state")).equals("todo")) { item.put("_id", c.getInt(c.getColumnIndex("_id"))); item.put("task", c.getString(c.getColumnIndex("task"))); item.put("date", c.getString(c.getColumnIndex("date"))); item.put("task_type", c.getString(c.getColumnIndex("task_type"))); item.put("cycle_time", c.getString(c.getColumnIndex("cycle_time"))); item.put("remark", c.getString(c.getColumnIndex("remark"))); item.put("state", c.getString(c.getColumnIndex("state"))); item.put("done_time",c.getString(c.getColumnIndex("done_time"))); item.put("image", R.drawable.quan1); list.add(item); } } } c.close(); return list; }