Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从SQLite数据库检索数据并将其显示在ListView上_Sqlite_Listview_Android Arrayadapter_Android Sqlite - Fatal编程技术网

从SQLite数据库检索数据并将其显示在ListView上

从SQLite数据库检索数据并将其显示在ListView上,sqlite,listview,android-arrayadapter,android-sqlite,Sqlite,Listview,Android Arrayadapter,Android Sqlite,我已经在互联网上搜索过了,我不知道我需要做什么才能将数据从数据库显示到列表视图。他们有教程,但我不太懂。 这是我的数据库处理程序代码 package com.example.databasetest; import android.content.ContentValues; import android.content.Context; import android.database.SQLException; import android.datab

我已经在互联网上搜索过了,我不知道我需要做什么才能将数据从数据库显示到列表视图。他们有教程,但我不太懂。 这是我的数据库处理程序代码

    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“我到底应该放什么?将使用它或字符串的活动?”)。这个方法应该是什么样的


我将非常感谢您的帮助。

这个解决方案对我很有效。您需要将
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;
}
public ArrayList getData(){
ArrayList值=新的ArrayList();
String columns[]=新字符串[]{COL_NAME};//考虑到要返回NAME
游标c=db.query(表名称、列、null、null、null、null、,
无效);
字符串结果;
int iName=c.getColumnIndex(列名称);
for(c.moveToFirst();!c.isAfterLast();c.moveToNext()){
结果=c.getString(iName);
值。添加(结果);
}
返回值;
}
现在,在要显示listview的主类中,输入以下代码:

    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));
}
private-DBHandler-DBHandler;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
dbHandler=newdbhandler(Classname.this);
dbHandler.open();
ArrayList data=dbHandler.getData();
最终ListView ListView=getListView();
setListAdapter(新阵列适配器)(ExpSubList.this,
android.R.layout.simple_list_item_1,data));
}
这将有望在listview中显示名称。我没有使用xml布局来定义listview,而是直接在java中定义的。你可以按要求做