使用数据库中的自己的布局填充listview

时间:2015-10-08 16:09:11

标签: android database sqlite listview android-listview

我正在尝试使用带有来自数据库的两个字符串的自定义布局填充列表视图,但遇到了一些困难。我尝试了很多方法,但没有一种方法有效。我希望有人能给我一个更好的解决方案。请帮我完成自定义布局。

这是数据库的构造函数

public class Vault {
public Vault() //a constructor is created for the creation of the database
{

}

public static abstract class noteInfo implements BaseColumns // a table is then created
{
    public static final String USER_NAME = "user_name";
    public static final String NOTE = "note";
    public static final String TIMESTAMP = "timestamp";
    public static final String DATABASE_NAME = "vault";
    public static final String TABLE_NAME = "note_cabinet";
}

}

这是数据库的适配器     public class noteOperations扩展了SQLiteOpenHelper {     public static final int database_version = 1;

public String CREATE_QUERY = "CREATE TABLE " + noteInfo.TABLE_NAME + "(" + noteInfo.NOTE+ " TEXT," + noteInfo.TIMESTAMP + " TEXT," + noteInfo.USER_NAME + " TEXT);";

public noteOperations(Context context) {
    super(context, noteInfo.DATABASE_NAME, null, database_version);
    Log.d("Note operations", "Database created");
}

@Override
public void onCreate(SQLiteDatabase sdb) {
    sdb.execSQL(CREATE_QUERY);
    Log.d("Note operations", "Table created");

}

@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
    // TODO Auto-generated method stub

}
public void putInformation(noteOperations no, String note, String timestamp, String user)
{
    SQLiteDatabase SQ = no.getReadableDatabase();
    ContentValues cv = new ContentValues();
    cv.put(noteInfo.NOTE, note);
    cv.put(noteInfo.TIMESTAMP, timestamp);
    cv.put(noteInfo.USER_NAME, user);
    SQ.insert(noteInfo.TABLE_NAME, null, cv);
    Log.d("Note operations", "One note inserted");
}
public Cursor getInformation(noteOperations no)
{
    SQLiteDatabase SQ = no.getReadableDatabase();
    String[] columns = {noteInfo.NOTE, noteInfo.TIMESTAMP};
    Cursor CR = SQ.query(noteInfo.TABLE_NAME, columns, null, null, null, null, null);
    Log.d("Note operations", "Table combed");
    return CR;
}
public void deleteUser(noteOperations no, String user_name)
{
    String selection = noteInfo.USER_NAME + " LIKE ? AND " + noteInfo.NOTE + " LIKE ?" + noteInfo.TIMESTAMP + " LIKE ?";
    String args[] = {user_name};
    SQLiteDatabase SQ = no.getWritableDatabase();
    SQ.delete(noteInfo.TABLE_NAME, selection, args);
    Log.d("Databsse operations", "User deleted");
}
public Cursor getallrows(noteOperations no)
{
    SQLiteDatabase SQ = no.getReadableDatabase();
    String[] columns = {noteInfo.NOTE, noteInfo.TIMESTAMP};
    Cursor CR = SQ.query(noteInfo.TABLE_NAME, columns, null, null, null, null, null, null);
    if (CR != null)
    {
        CR.moveToFirst();
    }
    return CR;
}
}

这是listview

的自定义布局的xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bubble_drawable" >

<TextView
    android:id="@+id/noteView_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:padding="4dp"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#ffffff"
    android:textStyle="bold" />

<TextView
    android:id="@+id/timestampView_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/noteView_layout"
    android:padding="3dp"
    android:text="TextView"
    android:textColor="#ffffff"
    android:textStyle="bold" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

使用CursorAdapter。 Ib onCreateViewHolder创建行视图。在onBindViewHolder中设置光标数据 另请阅读this