4

我有一个活动,我写了一个插入数据库的名称。在另一个活动中,我想放置一个 ListView,其中填充了数据库中的那些名称,或者当我在第一个活动的 edittext 中写入新项目时直接添加新项目。我实现了插入数据库部分,但我不知道如何填充该列表视图。

public class Add extends Activity implements OnClickListener{

Button sqlUpdate;
EditText sqlName;


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add);
    sqlUpdate = (Button) findViewById(R.id.bFinishAdd);
    //sqlView = (Button) findViewById(R.id.bSQLopenView);
    sqlName = (EditText) findViewById(R.id.etAdd);
    sqlUpdate.setOnClickListener(new OnClickListener(){

        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            boolean didItWork = true;

            try{
            String name = sqlName.getText().toString();

            DBhelp entry = new DBhelp(Add.this);
            entry.open();
            entry.createEntry(name);
            entry.close();
            Intent i = new Intent(Add.this, Liste.class);
            startActivity(i);

            }catch(Exception i){
                didItWork = false;
            }finally{
        }
        }


    });

}


public void onClick(View arg0) {
    // TODO Auto-generated method stub

}

}

这是数据库助手:

      public class DBhelp {

public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "persons_name";



private static final String DATABASE_NAME = "DBhelpdb";
private static final String DATABASE_TABLE = "peopleTable";
private static final int DATABASE_VERSION = 1;

private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;


private static class DbHelper extends SQLiteOpenHelper{

    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
                KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                KEY_NAME + " TEXT NOT NULL);"


        );
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
        onCreate(db);
    }

}

public DBhelp(Context c){
    ourContext = c;
}
public DBhelp open() throws SQLException{
    ourHelper = new DbHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();
    return this;
}
public void close(){
    ourHelper.close();
}
public void createEntry(String name) {
    // TODO Auto-generated method stub
    ContentValues cv = new ContentValues();
    cv.put(KEY_NAME, name);

    ourDatabase.insert(DATABASE_TABLE, null, cv);
}
public String getData() {
    // TODO Auto-generated method stub
    String[] columns = new String[]{KEY_ROWID, KEY_NAME};
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
    String result = "";

    int iRow = c.getColumnIndex(KEY_ROWID);
    int iName = c.getColumnIndex(KEY_NAME);

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
        result = result + c.getString(iRow) + " " + c.getString(iName) + "\n";
    }

    return result;
}
public String getName(long l) {
    // TODO Auto-generated method stub
    String[] columns = new String[]{KEY_ROWID, KEY_NAME};
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "=" + l, null, null, null, null);
    if (c!=null){
        c.moveToFirst();
        String name = c.getString(1);
        return name;
    }
    return null;
}

public void updateEntry(long lRow, String mName) {
    // TODO Auto-generated method stub
    ContentValues cvUpdate = new ContentValues();
    cvUpdate.put(KEY_NAME, mName);
    ourDatabase.update(DATABASE_TABLE, cvUpdate, KEY_ROWID + "=" + lRow, null);
}
public void deteleEntry(long lRow1) {
    // TODO Auto-generated method stub
    ourDatabase.delete(DATABASE_TABLE, KEY_ROWID + "=" + lRow1, null);}
}

这是我想用这些项目实现列表视图的活动:

     public class Liste extends Activity{

Button btnAdd;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.liste);
    btnAdd = (Button) findViewById(R.id.bAdd);
    btnAdd.setOnClickListener(new OnClickListener(){

        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent i = new Intent("com.project.mapshop.ADD");
            startActivity(i);
        }

    });
    TextView tv = (TextView) findViewById(R.id.tvSQLinfo);
    DBhelp info = new DBhelp(this);
    info.open();
    String data = info.getData();
    info.close();
    tv.setText(data);

}}

如果您能解释我如何做到这一点,或者给我一个可以帮助我的建议或教程的链接,我将非常感激。非常感谢你!

4

2 回答 2

8

请先学习一些简单的教程,一些好的教程是:

1-简单的例子

2-来自 SQLiteDatabase 的数据列表视图

于 2011-12-28T14:24:18.040 回答
3

根据您的要求,这是一个非常详尽的教程,我相信它会回答您的问题:http ://www.vogella.de/articles/AndroidListView/article.html#overview 。

它向您解释了如何设置一个ListActivity然后创建您自己的适配器来填充列表。

希望能帮助到你!

于 2011-12-28T14:14:01.887 回答