0

3 つのフィールドを持つデータベースを作成しました。これらのデータを、カスタマイズしたリストビューのテキスト ボックスにバインドする必要があります。

4

4 に答える 4

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"/>

于 2012-04-27T06:24:14.037 に答える
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;
}}
于 2012-04-27T06:16:26.407 に答える
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();

    }}
于 2012-04-27T06:19:33.007 に答える
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);
    }
于 2012-04-27T09:02:05.567 に答える