我想在android SQlite中开发一个小项目..
我想将员工信息存储到数据库中。 字段是姓名,城市,邮件ID和电话号码..
然后我想获取List-view中的数据..它将按员工姓名显示.. 当我点击名称(另一个意图)时,它将显示该名称的所有信息。
我已经创建了数据库帮助程序类,我可以存储信息。但是我无法通过名称检索数据库。任何人都可以帮助我?????
public class DBHelper extends SQLiteOpenHelper {
static final String DATABASE = "contactapp.db";
static final int VERSION = 1;
static final String TABLE = "contact";
static final String C_ID = "_id";
static final String C_ENAME = "ename";
static final String C_city = "city";
static final String C_phno = "ph_no";
public DBHelper(Context context) {
super(context, DATABASE, null, VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE + " ( " + C_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + C_ENAME + " text, "
+ C_city + " text, " + C_phno + " integer )");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("Drop table " + TABLE);
onCreate(db);
}
}
以及在sqlite数据库中存储名称,城市和电话号码的management.java ..
public class Management extends Activity {
EditText tname,tcity,tph_no;
Button button1;
DBHelper helper;
SQLiteDatabase db;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.management);
helper = new DBHelper(this);
tname = (EditText) findViewById(R.id.tname);
tcity = (EditText) findViewById(R.id.tcity);
tph_no = (EditText) findViewById(R.id.tph_no);
button1=(Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Context context = getApplicationContext();
int s2=Integer.parseInt(tph_no.getText().toString());
ContentValues values = new ContentValues();
values.put(DBHelper.C_ENAME, tname.getText().toString());
values.put(DBHelper.C_city, tcity.getText().toString());
values.put(DBHelper.C_phno, s2);
db = helper.getWritableDatabase();
db.insert(DBHelper.TABLE, null, values);
db.close();
Toast.makeText(context, "Employee Added Successfully",
Toast.LENGTH_LONG).show();
}
});
}
}
现在我想检索/获取名称作为列表..当我点击名称时,它将转到另一个意图,并将显示名称(再次),城市和电话号码..
答案 0 :(得分:1)
- 醇>
SimpleCursorAdapter
如果您直接使用数据库,可以使用SimpleCursorAdapter
来 定义ListView的数据。