![](/img/trans.png)
[英]how to fetch the data from sqlite database and show in listview in android
[英]Android listview fetch the data from sqlite database
我想在android SQlite中开发一个小项目。
我想将员工信息存储到数据库中。这些字段是姓名,城市,邮件ID和电话号码。
然后,我想在列表视图中获取数据。它将按员工姓名显示。当我单击姓名(另一个意图)时,它将显示该姓名的所有信息。
我已经创建了数据库帮助程序类,并且可以存储信息。但是我无法按名称检索数据库。
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);
}
}
和management.java将名称,城市和电话号码存储在sqlite数据库中。
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();
}
});
}
}
现在我只想检索/获取名称作为列表。.当我单击名称时,它将转到另一个意图,并显示名称(再次),城市和电话号码。
签出此链接
SimpleCursorAdapter
如果直接使用数据库,则可以使用SimpleCursorAdapter
定义ListView的数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.