0

我正在创建简单的数据库和我显示给 ListView 的数据库值,但我的 ListView 无法显示我的数据库值

这是我创建数据库的代码

public class MyDb extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "MyDb.db";
       private SQLiteDatabase myDataBase; 
    public MyDb(Context context) {
        super(context, DATABASE_NAME, null, 1);
    }


    public void createTable(SQLiteDatabase db){
        db.execSQL("DROP TABLE IF EXISTS users");
        db.execSQL("CREATE TABLE if not exists users (id_user INTEGER PRIMARY KEY AUTOINCREMENT, username VARCHAR);");

    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
    }
    public ArrayList<ArrayList<Object>> getAllRows(SQLiteDatabase db) {
        ArrayList<ArrayList<Object>> dataArray = new ArrayList<ArrayList<Object>>();
        Cursor cur;

        final String ROW_ID = "id_user";
        final String ROW_NAME = "username";
        try {
            cur = db.query("users", new String[] { ROW_ID, ROW_NAME }, null, null, null, null, null);
            cur.moveToFirst();
            if (!cur.isAfterLast()) {
                do {
                    ArrayList<Object> dataList = new ArrayList<Object>();
                    dataList.add(cur.getString(0));
                    dataList.add(cur.getString(1));

                    dataArray.add(dataList);

                } while (cur.moveToNext());

            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Log.e("DEBE ERROR", e.toString());
        }
        return dataArray;

    }
     public void insert(String table, ContentValues values) throws SQLException {
            myDataBase.insert(table, null, values);
        }

}

这是我向 ListView 显示数据库值的代码

public class DbScriptActivity extends Activity {

    private MyDb dbHelper;
    SQLiteDatabase db;
    Cursor customAdapter;
    ListView list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_db_script);
        dbHelper = new MyDb (this);
        db = dbHelper.getWritableDatabase();
        dbHelper.createTable(db);
        //dbHelper.openDataBase();
        //ListView list = (ListView)findViewById(R.id.list);
        list= (ListView)findViewById(R.id.list);
        getData();


        Button btn = (Button)findViewById(R.id.button1);
        btn.setOnClickListener(new View.OnClickListener() {

            ContentValues values = new ContentValues();
            public void onClick(View v) {
                LayoutInflater inflater = DbScriptActivity.this.getLayoutInflater();
                final View v1 =inflater.inflate(R.layout.dialog , null) ;

                new AlertDialog.Builder(DbScriptActivity.this)
                    .setTitle("input your name:")
                    .setView(v1)
                    .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dlg, int sumthin) {
                            EditText name= (EditText)v1.findViewById(R.id.name);


                            String yourname = name.getText().toString();
                            values.put("username",yourname);

                           Log.v("aaaa", yourname);

                            db.insert("users",null,values);
                        }
                    })
                    .show();

            }
        }); 
    }

    private void getData() {

        String[] name = { "" };
        ArrayList<ArrayList<Object>> data = dbHelper.getAllRows(db) ;
        name = new String[data.size()];
        for (int i = 0; i < data.size(); i++) {

            ArrayList<Object> row = data.get(i);
            name[i] = row.get(1).toString();

        }
        ArrayAdapter<String> aname = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, name);
        list.setAdapter(aname);

    }
}

这是我的 xml 文件,用于在我的 ListView 中查看数据库值

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DbScriptActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:text="Register Name" />

    <ListView
        android:id="@+id/list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1" >
    </ListView>

</RelativeLayout>

我的代码有什么问题?

4

1 回答 1

1

您可以使用 SimpleCursorAdapter 将数据加载到 ListView 中。这是很好的教程。

http://www.vogella.com/articles/AndroidSQLite/article.html

于 2013-02-14T04:36:57.713 回答