从SQLite数据库填充一个列表视图

从SQLite数据库填充一个列表视图

本文介绍了从SQLite数据库填充一个列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在那里我写的是插入到数据库名称的活动。在另一项活动我希望把它填充了这些名字这是在数据库或直接添加新项目时,我写在从第一个活动的EditText一个ListView。    我意识到插入到数据库中的一部分,但我无法弄清楚如何填充的列表视图。

I have an activity where i write a name which is insert in the database. In another activity I want to put a ListView which is populated with those names which are in the database or to add the new item directly when i write it in the edittext from the first activity. I realized the insert into database part but i can't figure out how to populate that listview.

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);}
}

和这里就是我要实现的列表视图与项目活动:

And here is the activity where I want to implement the listview with that items :

     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);

}}

我会很感激,如果你能解释我怎么做,或者给我一个建议或教程,可以帮助我一个链接。非常感谢你!

I would be very grateful if you could explain me how to do it or give me a link with an advice or tutorial which could help me. Thank you very much!

推荐答案

请遵循一些简单的教程首先,一些好的教程是:

Please follow some simple tutorials first, some good tutorials are:

1 这个

2 - 简单的例子

3 从SQLiteDatabase

这篇关于从SQLite数据库填充一个列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 19:23