2012-02-24 71 views
2

我已經做了MatrixCursor來訪問我的數據。它有4列。我有一個ListView其中每行有3個元素(其中一個是ImageView。所以我只需要2列)。這是我第一次使用遊標,因此哪種適配器最適合做這個?這是我迄今的工作。如何從Matrix中填充ListView光標

MatrixCursor:

static MatrixCursor getnameList() { 
    ArrayList<String> fsitem = getfsiList(); 
    String[] columnNames = {"id", "name", "info","icon"}; 
    MatrixCursor cursor = new MatrixCursor(columnNames); 
    for (int i = 0; i < fsitem.size(); i++) { 
     try { 
      File root = new File(Environment.getExternalStorageDirectory() 
        .getName() + "/" + fsitem.get(i)); 
      if (root.canRead()) { 
       File namefile = new File(root, ".name"); 
       FileReader namereader = new FileReader(namefile); 
       BufferedReader in = new BufferedReader(namereader); 
       String id = in.readLine(); 
       String name = in.readLine(); 
       String info = in.readLine(); 
       String[] fsii = new String[4]; 
       fsii[0]= id; 
       fsii[1]= name; 
       fsii[2]= info; 
       fsii[3]= null; 
       cursor.addRow(fsii); 
      } 

     } catch (IOException e) { 
      Log.e("NameManager.java : ", ("Error!! Not Writable!!" 
        + Environment.getExternalStorageDirectory().getName() 
        + "/" + fsitem.get(i))); 
     } 
    } 
    Log.d("NameManager.getnameList()",cursor.toString()); 
    return cursor; 

} 

row.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal" 
    android:padding="8dp" > 

    <ImageView 
     android:id="@+id/icon" 
     android:layout_width="22dp" 
     android:layout_height="22dp" 
     android:layout_marginLeft="4dp" 
     android:layout_marginRight="10dp" 
     android:layout_marginTop="4dp" 
     android:src="@drawable/list_icon" > 
    </ImageView> 

    <TextView 
     android:id="@+id/name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="16dp" 
     android:layout_marginLeft="38dp" 
     android:textStyle="bold" /> 

    <TextView 
     android:id="@+id/Info" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="5dp" 
     android:layout_toRightOf="@id/info" 
     android:textSize="16dp" 
     android:textStyle="bold" /> 
</RelativeLayout> 

回答

5

使用SimpleCursorAdapter。 您需要一個帶有要放入列表中的列的字符串數組,您的適配器將在其上綁定列中的數據的佈局中帶有TextView的ID的int數組。另外爲了使用遊標與ListView光標必須有一個添加到查詢列_id

String[] from = {BaseColumns._ID, "col1", "col2"}; 
int[] to = {R.id.Text1, R.id.text2} 
SimpleCursorAdapter adap = new SimpleCursorAdapter(this, R.layout.the_row_layout, curosrObject, from, to); 

編輯: 要在您的MatrixCursor你能在一個領域_id列在其中創建光標類:

private static int key = 0; 

然後在你的MatrixCursor命名BaseColumns._ID添加另一列。當您將添加在MatrixCursor新行添加,而不是String數組的新Object陣列,像這樣:

Object[] fsii = new Object[4]; 
fsii[0]= key; 
fsii[1]= name; 
fsii[2]= id; 
fsii[3]= info; 
cursor.addRow(fsii); 
key++; // the _id must have unique values so increment the key value; 

然後使用光標像上面(見小編輯)。

+0

感謝哥們,但正如你可以看到我的光標沒有_id列。我如何解決這個問題? – 2012-02-24 18:08:58

+0

@BinoyBabu我編輯了我的答案。 – Luksprog 2012-02-24 18:25:43

0
public class matrixCursor extends ListActivity { 

    private static int key = 0; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     fillData(); 
    } 

    private void fillData() { 
     MatrixCursor matrixCursor = new MatrixCursor(new String[] { "_id","colName1","colName2" }); 
     String name="sethu"; 
     int id=1; 
     String info="something"; 
     for (int i = 0; i < 4; i++){ 
      matrixCursor.addRow(new Object[] { key,name, info}); 
      key++; 
     } 

     String[] columnNames = {"_id","colName1","colName2" }; 
     int[] to = {R.id.Text1, R.id.Text2, R.id.Text3}; 

     SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.the_row_layout, 
       matrixCursor, columnNames, to); 
     setListAdapter(adapter); 
    } 
}