数据库值无法在ListView中显示

时间:2013-02-14 03:45:13

标签: android sqlite listview

我创建简单的数据库和数据库的值我向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>

我的代码出了什么问题?

1 个答案:

答案 0 :(得分:1)

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

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