2012-04-27 86 views
0

我創建了一個包含3個字段的數據庫。我需要將這些數據綁定到我自定義列表視圖中的文本框。如何將數據從數據庫綁定到自定義列表視圖

+0

它作爲你的問題很簡單。只需運行查詢,您將擁有'Cursor'並遍歷遊標並獲取數據並將其顯示在ListView中。 – 2012-04-27 05:49:41

+0

使用自定義數組列表中的任何三個輸入,並簡單地顯示它在listview.i已經在它上面。 – Furqi 2012-04-27 06:14:49

回答

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); 
    } 
相關問題