如何将数据从数据库绑定到自定义列表视图

时间:2012-04-27 05:41:36

标签: android listview

我创建了一个包含3个字段的数据库。我需要将这些数据绑定到我自定义列表视图中的文本框中。

4 个答案:

答案 0 :(得分:0)

试试这个例子: class file->

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;
}}

答案 1 :(得分:0)

这是db类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();

    }}

答案 2 :(得分: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"/>

答案 3 :(得分: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);
    }