1
我使用SimpleCursorAdapter
來管理我的查詢到數據庫,然後顯示在ListView
結果的CursorAdapter。的Android SimpleCursorAdapter到
這裏是我的代碼:
database.open();
ListView listContent = (ListView)findViewById(android.R.id.list);
Cursor c = database.getData();
startManagingCursor(c);
String[] from = new String[]{Database._GROUP_NAME};
int[] to = new int[]{android.R.id.text1};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, query, from, to);
listContent.setAdapter(adapter);
database.close();
雖然在第一次看,就它的工作如預期,我認爲,這不應該繼續,因爲有CursorAdapter
的方式。
我知道,這是一個noob問題,但由於剛剛起步,目前已經在Android的編程我仍然不知道多少。
我怎樣才能通過這個使用SimpleCursorAdapter
到CursorAdapter
? 已經搜索互聯網上,但無法知道如何可以做到這一點。
不問代碼只是一些方向。
感謝
favolas
更新後mainu COMMENT
database.open();
ListView listContent = (ListView)findViewById(android.R.id.list);
Cursor c = database.getData();
MyAdapt cursorAdapter = new MyAdapt(this, query);
listContent.setAdapter(adapter);
database.close();
MyAdapt類:
public class MyAdapt extends CursorAdapter {
private final LayoutInflater mInflater;
public MyAdapt(Context context, Cursor c) {
super(context, c);
mInflater = LayoutInflater.from(context);
mContext = context;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView groupName = (TextView) view.findViewById(android.R.id.text1);
groupName.setText(cursor.getString(cursor
.getColumnIndex(Database._GROUP_NAME)));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final View view = mInflater.inflate(
android.R.layout.simple_list_item_1, parent, false);
return view;
}
}
這是正確的方法是什麼?
favolas
創建一個類SimpleCursorAdapter這將延長一個CursorAdapter。你會得到一些默認的overRideed方法。 UU將被清除,然後 – mainu 2012-07-30 11:39:32
@mainu謝謝。請參閱我的更新 – Favolas 2012-07-30 15:05:38