无法在listview表单中检索SQLite数据。当我运行应用程序时,我的应用程序崩溃了。 我不知道实际问题在哪里。有人可以帮我解决如何检索listview中的所有数据。
先谢谢。
这是我的适配器代码。
public class EmployeeList_Adapter extends BaseAdapter
{
Context context;
ArrayList<Employee> Employee_List;
public EmployeeList_Adapter(Context context,
ArrayList<Employee> employee_List) {
super();
this.context = context;
Employee_List = employee_List;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return Employee_List.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return Employee_List.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup arg2) {
// TODO Auto-generated method stub
Employee EmployeeListItems = Employee_List.get(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.staff_employee_list_item, null);
}
TextView tvEmpId = (TextView) convertView.findViewById(R.id.textViewStaff_emp_Id);
tvEmpId.setText(EmployeeListItems.getEmployeeId());
TextView tvName = (TextView) convertView.findViewById(R.id.textViewStaff_Emp_ame);
tvName.setText(EmployeeListItems.getName());
TextView tvDepartment = (TextView) convertView.findViewById(R.id.textViewStaff_Emp_Department);
tvDepartment.setText(EmployeeListItems.getDepartment());
TextView tvDesignation = (TextView) convertView.findViewById(R.id.textViewStaff_Emp_Designation);
tvDesignation.setText(EmployeeListItems.getDesignation());
return convertView;
}
}
Here is my Activity code
public class Employee_List extends Activity
{
DatabaseHelper databaseHelper = new DatabaseHelper(this);
Context mContext;
Employee emp;
protected SQLiteDatabase db;
protected Cursor cursor;
protected ListAdapter adapter;
ListView listEmployee;
SimpleCursorAdapter mAdapter;
public static final String EMPLOYEE_DETAILS_TABLE = "employee_details";
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.staff_employee_list);
listEmployee = (ListView)findViewById(R.id.mylist);
showList();
}
private void showList()
{ ArrayList<Employee> EmployeeList = new ArrayList<Employee>();
EmployeeList.clear();
String query = "SELECT * FROM employee_details";
SQLiteDatabase sqlDatabase = databaseHelper.getWritableDatabase();
Cursor c1 =sqlDatabase.rawQuery(query, null);
//Cursor c1 =db.rawQuery(query, null);
if (c1 != null && c1.getCount() != 0)
{
if (c1.moveToFirst())
{
do
{
Employee EmployeeListItems = new Employee();
EmployeeListItems.setEmployeeId(c1.getInt(c1.getColumnIndex("_id")));
EmployeeListItems.setName(c1.getString(c1.getColumnIndex("staff_emp_name")));
EmployeeListItems.setDepartment(c1.getString(c1.getColumnIndex("department")));
EmployeeListItems.setDesignation(c1.getString(c1.getColumnIndex("designation")));
EmployeeList.add(EmployeeListItems);
} while (c1.moveToNext());
}
}
EmployeeList_Adapter contactListAdapter = new EmployeeList_Adapter(Employee_List.this, EmployeeList);
listEmployee.setAdapter(contactListAdapter);
}
}
Here is pojo class
public class Employee
{
int employeeId;
private String name;
private String department;
private String designation;
public Employee() {
super();
}
public Employee(int employeeId, String name, String department,
String designation) {
super();
this.employeeId = employeeId;
this.name = name;
this.department = department;
this.designation = designation;
}
public Employee(String str_employeeName, String str_Department,
String str_Designation)
{
// TODO Auto-generated constructor stub
this.name = str_employeeName;
this.department = str_Department;
this.designation = str_Designation;
}
public int getEmployeeId() {
return employeeId;
}
public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
}
Log cat error
06-29 15:22:00.434: E/AndroidRuntime(639): FATAL EXCEPTION: main
06-29 15:22:00.434: E/AndroidRuntime(639): android.content.res.Resources$NotFoundException: String resource ID #0x1
06-29 15:22:00.434: E/AndroidRuntime(639): at android.content.res.Resources.getText(Resources.java:201)
06-29 15:22:00.434: E/AndroidRuntime(639): at android.widget.TextView.setText(TextView.java:2857)
06-29 15:22:00.434: E/AndroidRuntime(639): at com.sqlitedemo.EmployeeList_Adapter.getView(EmployeeList_Adapter.java:55)
06-29 15:22:00.434: E/AndroidRuntime(639): at android.widget.AbsListView.obtainView(AbsListView.java:1430)
06-29 15:22:00.434: E/AndroidRuntime(639): at android.widget.ListView.makeAndAddView(ListView.java:1745)
06-29 15:22:00.434: E/AndroidRuntime(639): at android.widget.ListView.fillDown(ListView.java:670)
06-29 15:22:00.434: E/AndroidRuntime(639): at android.widget.ListView.fillFromTop(ListView.java:727)
06-29 15:22:00.434: E/AndroidRuntime(639): at android.widget.ListView.layoutChildren(ListView.java:1598)
06-29 15:22:00.434: E/AndroidRuntime(639): at android.widget.AbsListView.onLayout(AbsListView.java:1260)
06-29 15:22:00.434: E/AndroidRuntime(639): at android.view.View.layout(View.java:7175)
06-29 15:22:00.434: E/AndroidRuntime(639): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
06-29 15:22:00.434: E/AndroidRuntime(639): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
06-29 15:22:00.434: E/AndroidRuntime(639): at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
06-29 15:22:00.434: E/AndroidRuntime(639): at android.view.View.layout(View.java:7175)
06-29 15:22:00.434: E/AndroidRuntime(639): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
06-29 15:22:00.434: E/AndroidRuntime(639): at android.view.View.layout(View.java:7175)
06-29 15:22:00.434: E/AndroidRuntime(639): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
06-29 15:22:00.434: E/AndroidRuntime(639): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
答案 0 :(得分:1)
变化
tvEmpId.setText(EmployeeListItems.getEmployeeId());
通过
tvEmpId.setText(EmployeeListItems.getEmployeeId()+"");