gpt4 book ai didi

java - 在 Android 中删除 Listview 项目?

转载 作者:行者123 更新时间:2023-12-01 12:45:25 25 4
gpt4 key购买 nike

我想从 Android 的 ListView 项中删除第 0 个位置。我已经完成了从 SQLite 数据库填充 ListView 。但是现在我想在 onCreate() 方法中从 ListView 中删除第 0 个位置项。我不知道该怎么做。我已经尝试过了,但是我的应用程序崩溃了,并且出现 ArrayIndexOutOfBoundException 无效索引 3、大小为 3 的异常。有人可以帮我弄清楚如何做到这一点。感谢欣赏。

这是我的适配器类代码。

public class EmployeeList_Adapter extends BaseAdapter
{
Context context;
ArrayList<Employee> Employee_List;

protected SQLiteDatabase db;


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 tvID = (TextView) convertView.findViewById(R.id.textViewStaff_emp_Id);
tvID.setText(EmployeeListItems.getEmployeeId()+"");

ImageView imgProfile = (ImageView) convertView.findViewById(R.id.imgStaff_Emp);


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());

TextView tvPerformanceDate = (TextView) convertView.findViewById(R.id.textDate);
tvPerformanceDate.setText(EmployeeListItems.getDate());

TextView tvPerformanceRate = (TextView) convertView.findViewById(R.id.empLoyeeRate);
tvPerformanceRate.setText(EmployeeListItems.getPerformancerate());

final Employee empPOsition = Employee_List.get(position);
int positionA = Employee_List.indexOf(empPOsition);

notifyDataSetChanged();
System.out.println(" empPOsition = " + positionA);

if( positionA == 0)
{
Employee_List.remove(position);
}

return convertView;
}

}


Here is my Log Cat Error


07-15 11:41:56.322: E/AndroidRuntime(651): FATAL EXCEPTION: main
07-15 11:41:56.322: E/AndroidRuntime(651): java.lang.IndexOutOfBoundsException: Invalid index 3, size is 3
07-15 11:41:56.322: E/AndroidRuntime(651): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
07-15 11:41:56.322: E/AndroidRuntime(651): at java.util.ArrayList.get(ArrayList.java:311)
07-15 11:41:56.322: E/AndroidRuntime(651): at com.sqlitedemo.EmployeeList_Adapter.getView(EmployeeList_Adapter.java:49)
07-15 11:41:56.322: E/AndroidRuntime(651): at android.widget.AbsListView.obtainView(AbsListView.java:1430)
07-15 11:41:56.322: E/AndroidRuntime(651): at android.widget.ListView.makeAndAddView(ListView.java:1745)
07-15 11:41:56.322: E/AndroidRuntime(651): at android.widget.ListView.fillDown(ListView.java:670)
07-15 11:41:56.322: E/AndroidRuntime(651): at android.widget.ListView.fillFromTop(ListView.java:727)
07-15 11:41:56.322: E/AndroidRuntime(651): at android.widget.ListView.layoutChildren(ListView.java:1598)
07-15 11:41:56.322: E/AndroidRuntime(651): at android.widget.AbsListView.onLayout(AbsListView.java:1260)
07-15 11:41:56.322: E/AndroidRuntime(651): at android.view.View.layout(View.java:7175)
07-15 11:41:56.322: E/AndroidRuntime(651): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:912)
07-15 11:41:56.322: E/AndroidRuntime(651): at android.view.View.layout(View.java:7175)
07-15 11:41:56.322: E/AndroidRuntime(651): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
07-15 11:41:56.322: E/AndroidRuntime(651): at android.view.View.layout(View.java:7175)
07-15 11:41:56.322: E/AndroidRuntime(651): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
07-15 11:41:56.322: E/AndroidRuntime(651): at android.view.View.layout(View.java:7175)
07-15 11:41:56.322: E/AndroidRuntime(651): at android.view.ViewRoot.performTraversals(ViewRoot.java:1140)
07-15 11:41:56.322: E/AndroidRuntime(651): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
07-15 11:41:56.322: E/AndroidRuntime(651): at android.os.Handler.dispatchMessage(Handler.java:99)
07-15 11:41:56.322: E/AndroidRuntime(651): at android.os.Looper.loop(Looper.java:123)
07-15 11:41:56.322: E/AndroidRuntime(651): at android.app.ActivityThread.main(ActivityThread.java:3683)
07-15 11:41:56.322: E/AndroidRuntime(651): at java.lang.reflect.Method.invokeNative(Native Method)
07-15 11:41:56.322: E/AndroidRuntime(651): at java.lang.reflect.Method.invoke(Method.java:507)
07-15 11:41:56.322: E/AndroidRuntime(651): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-15 11:41:56.322: E/AndroidRuntime(651): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-15 11:41:56.322: E/AndroidRuntime(651): at dalvik.system.NativeStart.main(Native Method)

最佳答案

而不是检查 positionA检查 position仅在膨胀布局后和设置 View 数据之前尝试删除。

试试这个

public class EmployeeList_Adapter extends BaseAdapter
{
Context context;
ArrayList<Employee> Employee_List;

protected SQLiteDatabase db;


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

if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.staff_employee_list_item, null);
}
if( position == 0)
{
Employee_List.remove(position);
}

Employee EmployeeListItems = Employee_List.get(position);
TextView tvID = (TextView) convertView.findViewById(R.id.textViewStaff_emp_Id);
tvID.setText(EmployeeListItems.getEmployeeId()+"");

ImageView imgProfile = (ImageView) convertView.findViewById(R.id.imgStaff_Emp);


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());

TextView tvPerformanceDate = (TextView) convertView.findViewById(R.id.textDate);
tvPerformanceDate.setText(EmployeeListItems.getDate());

TextView tvPerformanceRate = (TextView) convertView.findViewById(R.id.empLoyeeRate);
tvPerformanceRate.setText(EmployeeListItems.getPerformancerate());

final Employee empPOsition = Employee_List.get(position);
int positionA = Employee_List.indexOf(empPOsition);

notifyDataSetChanged();
System.out.println(" empPOsition = " + positionA);



return convertView;
}

}

我认为这会起作用,谢谢;-)

关于java - 在 Android 中删除 Listview 项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24751029/

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