gpt4 book ai didi

java - Android中TextView的Resource NotFound异常

转载 作者:行者123 更新时间:2023-11-30 02:49:22 27 4
gpt4 key购买 nike

无法检索 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)

最佳答案

改变

tvEmpId.setText(EmployeeListItems.getEmployeeId());

tvEmpId.setText(EmployeeListItems.getEmployeeId()+"");

关于java - Android中TextView的Resource NotFound异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24475044/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com