【问题标题】:Retrieving data from a SQLite database and displaying it on the ListView从 SQLite 数据库中检索数据并将其显示在 ListView 上
【发布时间】:2013-01-29 14:19:31
【问题描述】:

我已经在 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?”)。方法应该是什么样的?

非常感谢您的帮助。

【问题讨论】:

    标签: sqlite listview android-arrayadapter android-sqlite


    【解决方案1】:

    这个解决方案对我有用。您需要将 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中定义。您可以根据需要进行操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多