我是android新手,我现在正在学习,请帮助meeeeeee .....我想使用适配器在ListView中显示所有Emp名称,我从数据库中获取了值...请帮助我解决这个问题。 ..
这是我的代码...
// POJO类
public class Emp {
int id;
String fName;
String lName;
public Emp() {
}
public Emp(String fName, String lName){
this.fName = fName;
this.lName = lName;
}
public Emp(int id, String fName, String lName) {
this.id = id;
this.fName = fName;
this.lName = lName;
}
// setter and getters...
}
// 布局 demo.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/empListView"
android:layout_width="fill_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
//数据库助手
public List<Emp> getAllEmp() {
List<Emp> empList = new ArrayList<Emp>();
String selectQuery = "SELECT * FROM Emp" ;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
Emp emp = new Emp();
emp.setId(Integer.parseInt(cursor.getString(0)));
emp.setfName(cursor.getString(1));
emp.setlName(cursor.getString(2));
empList.add(emp);
} while (cursor.moveToNext());
}
return empList;
}
//主要活动
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo);
DbHelper helper = new DbHelper(getApplicationContext());
helper.open();
List<Emp> empList = helper.getAllEmp();
// Here i have to write a logic that to get only fName and set to adaptor...
list = (ListView) findViewById(R.id.empListView);
list.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, empList));
}
在这里,我正在获取一组 emp 记录...
这是数据库
|-------|-----------|-----------|
|___id__|___fName___|___lName___|
| | | |
| 1 | Rahul | Dravid |
| 2 | Sachin | Tendulkar |
| 3 | Saurav | Ganguly |
|-------|-----------|-----------|
提前致谢...