适用于listview和数据库的适配器是什么?

时间:2014-02-11 21:08:40

标签: android android-listview android-adapter listadapter

我是Android(Java)编程的新手。 我有一个数据库活动:

public class DataBase {
      public static final String DB_NAME = "appdb";
      public static final int DB_VERSION = 1;
      static final String TABLE_NAME = "passtab";
      final String LOG_TAG = "myLogs";
      public static final String COLUMN_ID = "_id";
      public static final String COLUMN_TITLE = "title_enter";
      public static final String COLUMN_LOGIN = "login_enter";
      public static final String COLUMN_PASSWORD = "password_enter";
      public static final String COLUMN_URL = "link_enter";
      public static final String COLUMN_COMMENT = "comment_enter";
      public static final String COLUMN_DATE = "date_enter";
      public String results;
      private DBHelper phdb;
      private static Context pcontext;
      private SQLiteDatabase pdb;

      private static final String DB_CREATE = 
                "create table " + TABLE_NAME + "(" +
                  COLUMN_ID + " integer primary key autoincrement, " +
                  COLUMN_TITLE + " VARCHAR(255), " +
                  COLUMN_LOGIN + " VARCHAR(255), " +
                  COLUMN_PASSWORD + " VARCHAR(255), " +
                  COLUMN_URL + " VARCHAR(255), " +
                  COLUMN_COMMENT + " text, " +
                  COLUMN_DATE + " VARCHAR(255)" +
                ");";

public class DBHelper extends SQLiteOpenHelper{


          public DBHelper(Context context) {
                super(context, DB_NAME, null, DB_VERSION);
              }

            @Override
          public void onCreate(SQLiteDatabase sqLiteDatabase) {
            sqLiteDatabase.execSQL(DB_CREATE);
          }

          @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            }


    }

public DataBase(Context c) {
    pcontext = c;
}

public DataBase open() throws SQLiteException {
    phdb = new DBHelper(pcontext);
    pdb = phdb.getWritableDatabase();
    return this;
}

public SQLiteDatabase getDatabase() {
    return pdb;
}

public void close() {
    pdb.close();
}

public long createEntry(String tt, String lg, String ps, String ul,
        String cm, String dt) {
    ContentValues cv = new ContentValues();
    cv.put(COLUMN_TITLE, tt);
    cv.put(COLUMN_LOGIN, lg);
    cv.put(COLUMN_PASSWORD, ps);
    cv.put(COLUMN_URL, ul);
    cv.put(COLUMN_COMMENT, cm);
    cv.put(COLUMN_DATE, dt);

    return pdb.insert(TABLE_NAME, null, cv);
}
}

MainScreen活动将显示listview(其中包含自定义列表行xml)。

MainScreen活动:

public class MainScreen extends FragmentActivity implements OnClickListener {

      ListView listViewMain;
      SQLiteDatabase db;
      DataBase DB;
      DBHelper dbH;
      Cursor cursor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
            setContentView(R.layout.main_screen);

                    listViewMain = (ListView) findViewById(R.id.listViewMain);

                    ????
}

并列出行自定义xml:

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/titleView"
            android:text="" />

        <TextView
            android:id="@+id/dateView"
            android:text="" />

    </RelativeLayout>

我应该使用什么适配器从数据库接收数据(COLUMN_TITLE和COLUMN_DATE)并填写listview行项目(textview = titleView,textview = dateView)?

如果可以,请写一个代码示例。

P.S。谢谢!

2 个答案:

答案 0 :(得分:0)

SimpleCursorAdapter符合您的需求,因为您只在视图中使用了文字视图。这个post给出了关于如何使用它的正确解释。

答案 1 :(得分:0)