我創建了一個包含3個字段的數據庫。我需要將這些數據綁定到我自定義列表視圖中的文本框。如何將數據從數據庫綁定到自定義列表視圖
0
A
回答
0
嘗試這個例子: 類文件 - >
public class SimpleDBExActivity extends Activity {
/** Called when the activity is first created. */
DataBaseHelper dbHelper;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ListView list = (ListView) findViewById(R.id.lis1);
this.dbHelper=new DataBaseHelper(SimpleDBExActivity.this);
dbHelper.insert("121", "some text111");
dbHelper.insert("122", "some text222");
dbHelper.insert("123", "some text333");
dbHelper.insert("124", "some text444");
ArrayList<String> resultsArr = dbHelper.getAllMsgs();
list.setAdapter(new MyCustomBaseAdapter(this, resultsArr));
}}
class MyCustomBaseAdapter extends BaseAdapter {
private static ArrayList<String> searchArrayList;
private LayoutInflater mInflater;
public MyCustomBaseAdapter(Context context, ArrayList<String> results) {
searchArrayList = results;
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return searchArrayList.size();
}
public Object getItem(int position) {
return searchArrayList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.listrow, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.textID);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(searchArrayList.get(position));
return convertView;
}
static class ViewHolder {
TextView text;
}}
0
這是分貝類DataBaseHelper.java
public class DataBaseHelper extends SQLiteOpenHelper{
private static final String DB_NAME="SampleMsgDb";
private static final String TABLE="SampleTABLE";
private static final int DB_VERSION=1;
private static final String COLUMN1="received_Time";
private static final String COLUMN2="col1";
private static final String COLUMN3="col2";
private Context myContext;
private SQLiteDatabase myDataBase;
DataBaseHelper dbHelper;
public DataBaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
myContext=context;
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String sql = "create table " + TABLE + "("+ COLUMN1 + " integer , " + COLUMN2 + " text not null, "
+ COLUMN3 + " text not null);";
Log.d("EventsData", "onCreate: " + sql);
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
if (oldVersion >= newVersion)
return;
String sql = null;
if (oldVersion == 1)
sql = "alter table " + TABLE + " add note text;";
if (oldVersion == 2)
sql = "";
Log.d("EventsData", "onUpgrade : " + sql);
if (sql != null)
db.execSQL(sql);
}
public void insert(String number, String message) {
dbHelper=new DataBaseHelper(myContext);
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(DataBaseHelper.COLUMN1, System.currentTimeMillis());
values.put(DataBaseHelper.COLUMN2, number);
values.put(DataBaseHelper.COLUMN3, message);
db.insert(DataBaseHelper.TABLE, null, values);
System.out.println("inserted success");
db.close();
if (myDataBase != null)
myDataBase.close();
}
@SuppressWarnings("null")
public ArrayList<String> getAllMsgs(){
ArrayList<String> finalResArr = null;
String result = "";
String resultQuery = "select * from " + TABLE + "";
dbHelper = new DataBaseHelper(myContext);
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.rawQuery(resultQuery, null);
finalResArr = new ArrayList<String>();
if (cursor.moveToFirst()) {
do {
result = cursor.getString(1)+":"+cursor.getString(2);
finalResArr.add(result);
} while (cursor.moveToNext());
}
db.close();
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
return finalResArr;
}
@Override
public synchronized void close() {
super.close();
if (myDataBase != null)
myDataBase.close();
}}
0
main.xml中
<ListView android:id="@+id/lis1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></ListView>
在listrow.xml
<TextView android:id="@+id/textID"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#ffffff"/>
0
Cursor c = mydbhelper.getAllCategories();
startManagingCursor(c);
// Create an array to specify the fields we want to display in the list (TITLE,DATE,NUMBER)
String[] from = new String[] {dbadapter.KEY_CATEGORYDESC};
// an array of the views that we want to bind those fields to (in this case text1,text2,text3)
int[] to = new int[] {R.id.text1};
// Now create a simple cursor adapter and set it to display
SimpleCursorAdapter adapter =
new SimpleCursorAdapter(this, R.layout.cate_row, c, from, to);
setListAdapter(adapter);
}
相關問題
- 1. Android:從數據庫綁定數據到列表視圖
- 2. 從數據庫中讀取數據並將它們綁定到自定義列表視圖
- 3. 如何從sqlite數據庫中添加自定義列表視圖的數據?
- 4. WPF將常見數據從數據庫綁定到列表視圖
- 5. 從SQLite數據庫顯示數據到自定義列表視圖
- 6. 將數據庫中的圖像綁定到數據列表
- 7. 將數據從列表框綁定到數據庫c#
- 8. 如何將自動填充框綁定到數據庫表列?
- 9. 自定義列表視圖從數據添加數據
- 10. iOS圖表庫 - 無法將數據綁定到圖表視圖
- 11. 如何將數據綁定到網頁中的列表視圖
- 12. 將選定的列從數據表綁定到數據網格
- 13. 未將JSON數據加載到自定義列表視圖中
- 14. 來自數據庫的自定義列表視圖
- 15. 自定義列表視圖與圖像和數據庫連接
- 16. 如何將數據庫數據綁定到Datagrid列?
- 17. 將數據從自定義表格插入Wordpress數據庫表
- 18. 如何將數據從數據庫綁定到控制字段?
- 19. 如何將數據從內部數據庫綁定到devExpress gridcontrol
- 20. 如何將數據從數據庫綁定到silverlight中的treeview?
- 21. 數據綁定列表視圖
- 22. 列表視圖數據綁定混淆
- 23. WPF列表視圖數據綁定
- 24. WPF如何將自引用數據集綁定到樹視圖
- 25. 將數據庫數據綁定到下拉列表中
- 26. 如何顯示特定的數據從數據庫到列表視圖sqlitedatabase
- 27. 從數據庫綁定數據到下拉列表+ ASP.NET MVC
- 28. 將數據綁定到滾動視圖
- 29. 從數據庫綁定下拉列表
- 30. 如何將用戶自定義數據添加到數據庫?
它作爲你的問題很簡單。只需運行查詢,您將擁有'Cursor'並遍歷遊標並獲取數據並將其顯示在ListView中。 – 2012-04-27 05:49:41
使用自定義數組列表中的任何三個輸入,並簡單地顯示它在listview.i已經在它上面。 – Furqi 2012-04-27 06:14:49