0

我已经在 Internet 上进行了搜索,但我不明白将数据库中的数据显示到 ListView 需要做什么。他们有教程,但我不太明白。这是我的数据库处理程序代码

    package com.example.databasetest;

    import android.content.ContentValues;
    import android.content.Context;
    import android.database.SQLException;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    import android.util.Log;
    import android.widget.Toast;

    public class DBHandler {

        public static final String TABLE_NAME = "tableKo";
        public static final String DATABASE_NAME = "databaseKo";
        private static final int DATABASE_VERSION = 1;
        private static final String TAG = "DBHandler";

        public static final String COL_ID = "_id";
        public static final String COL_NAME = "name";
        public static final String COL_ADDRESS = "address";
        public static final String COL_PHONE = "phone";
        public static final String COL_EMAIL = "email";

        private final Context context;
        private SQLiteDatabase db;
        private MySQLiteOpenHelper DBHelper;
        private String[] data;

        private static final String CREATE_DATABASE ="create table "
                + TABLE_NAME + "(" + COL_ID
                + " integer primary key, " + COL_NAME
                + " text not null, " + COL_ADDRESS + " text not null,"
                + COL_PHONE + " text not null," + COL_EMAIL + " text not null);";
        public DBHandler(Context ctx) {
            this.context = ctx;
            DBHelper = new MySQLiteOpenHelper(context);
        }
        private static class MySQLiteOpenHelper extends SQLiteOpenHelper{
            public MySQLiteOpenHelper(Context context) {
                super(context, DATABASE_NAME, null, DATABASE_VERSION);
            }
            @Override
            public void onCreate(SQLiteDatabase db) {
                // TODO Auto-generated method stub
                try {
                    db.execSQL(CREATE_DATABASE);
                  } catch (SQLException e) {
                    e.printStackTrace();
                  } 
            }

            @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
                // TODO Auto-generated method stub
                Log.w(TAG, oldVersion + " to " + newVersion
                        + ", which will destroy all old data");
                db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
                onCreate(db);
            }

        }
        public DBHandler open() throws SQLException {
            db = DBHelper.getWritableDatabase();
            return this;
        }

        public void close() {
            DBHelper.close();
        }
        public void insertData (String name, String address, String phone, String email) {
            open();
            ContentValues values = new ContentValues();
            values.put(COL_NAME, name);
            values.put(COL_ADDRESS, address);
            values.put(COL_PHONE, phone);
            values.put(COL_EMAIL, email);

            db.insert(TABLE_NAME, null, values);
            //db.execSQL("Insert into " +TABLE_NAME+ " VALUES('"+COL_ID+"','"+name+"','"+address+"','"+phone+"','"+email+"');");
            db.close();

        }
// now here I want to make a return type method to return "I don't know what data type or anything that will fit the listView
        public void getData() {

            DBHelper.getReadableDatabase();

        }
    }

我想做一个方法来返回适合 ListView 的东西。我应该返回 arrayAdapter 还是只返回一个简单的字符串数组?如果它是arrayAdapter,我不知道我应该放入什么类型(为了澄清我的意思是这个ArrayAdapter“我到底应该放什么?将使用它的活动还是String?”)。该方法应该是什么样的?

我将非常感谢您的帮助。

4

1 回答 1

0

这个解决方案对我有用。您需要将public void getData()方法声明为 ArrayList 或 ArrayAdapter,如下所示:

    public ArrayList<String> getData() {
    ArrayList<String> values = new ArrayList<String>();
    String columns[] = new String[] { COL_NAME }; //considering you wanna return name
    Cursor c = db.query(TABLE_NAME, columns, null, null, null, null,
            null);
    String result;
    int iName = c.getColumnIndex(COL_NAME);
    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
        result = c.getString(iName);
        values.add(result);
    }
    return values;
}

现在在要显示列表视图的主类中,输入以下代码:

    private DBHandler dbHandler;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    dbHandler = new DBHandler(Classname.this);
    dbHandler.open();
    ArrayList<String> data = dbHandler.getData();
    final ListView listView = getListView();
    setListAdapter(new ArrayAdapter<String>(ExpSubList.this,
            android.R.layout.simple_list_item_1, data));
}

这应该有望在您的列表视图中显示名称。我没有使用xml布局来定义listview,直接在java中定义。您可以根据需要进行操作。

于 2013-04-18T14:51:22.747 回答