我有一个活动,我写了一个插入数据库的名称。在另一个活动中,我想放置一个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);}
}
以下是我想用这些项目实现listview的活动:
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);
}}
如果您能解释我该怎么做或给我一个可以帮助我的建议或教程的链接,我将非常感激。非常感谢你!
答案 0 :(得分:8)
答案 1 :(得分:3)
根据您的要求,这是一个非常全面的教程,我相信它会回答您的问题:http://www.vogella.de/articles/AndroidListView/article.html#overview。
它解释了如何设置ListActivity
,然后创建自己的适配器来填充列表。
希望它有所帮助!