參考文章:http://blog.csdn.net/a_large_swan/article/details/7535337 java
數據源不一樣而已
1. String[]: ArrayAdapter
2. List<Map<String,?>>: SimpleAdapter
3. 數據庫Cursor: SimpleCursorAdapter android
第一種參考 數據庫
若是僅僅將數組的內容顯示到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>,須要轉換一下才能夠使用。 數組
寫一下第二種用法: app
一,mainActivity.class ide
1,數組類型: 佈局
ArrayList<HashMap<String,Object>>
2,適配器類型
SimpleAdapter/**第一個參數:context
第二個參數:數組 this
第三個參數:適配器樣式佈局 spa
第四個參數:hashmap中須要顯示出來的key值 .net
第五個參數:適配器樣式佈局中與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>
3、數組取值問題:
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; }