Java 是否删除Android中的Listview项?

Java 是否删除Android中的Listview项?,java,android,Java,Android,我想删除Android中列表视图项的第0个位置。我已经完成了从SQLite数据库填充列表视图的操作。但是现在我想在onCreate()方法中从列表视图中删除第0个位置项。我不知道该怎么做。我试过了,但我的应用程序崩溃了,ArrayIndexOutOfBoundException异常无效索引3,大小为3。有人能帮我弄清楚怎么做吗。感谢赞赏 这是我的适配器类代码 public class EmployeeList_Adapter extends BaseAdapter { Context c

我想删除Android中列表视图项的第0个位置。我已经完成了从SQLite数据库填充列表视图的操作。但是现在我想在onCreate()方法中从列表视图中删除第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)
公共类EmployeeList\u适配器扩展了BaseAdapter
{
语境;
ArrayList员工列表;
受保护的数据库数据库;
公共EmployeeList_适配器(上下文,
ArrayList(员工列表){
超级();
this.context=上下文;
员工名单=员工名单;
}
@凌驾
public int getCount(){
//TODO自动生成的方法存根
返回员工_List.size();
}
@凌驾
公共对象getItem(int位置){
//TODO自动生成的方法存根
返回员工列表。获取(职位);
}
@凌驾
公共长getItemId(int位置){
//TODO自动生成的方法存根
返回位置;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组arg2){
//TODO自动生成的方法存根
员工EmployeeListItems=员工列表.get(职位);
if(convertView==null){
LayoutFlater充气器=(LayoutFlater)上下文
.getSystemService(上下文布局\充气机\服务);
convertView=充气机。充气(R.layout.staff\u employee\u list\u项,空);
}
TextView tvID=(TextView)convertView.findViewById(R.id.textViewStaff\u emp\u id);
tvID.setText(EmployeeListItems.getEmployeeId()+);
ImageView imgProfile=(ImageView)convertView.findViewById(R.id.imgStaff_Emp);
TextView tvName=(TextView)convertView.findViewById(R.id.textViewStaff\u Emp\u ame);
setText(EmployeeListItems.getName());
TextView tvDepartment=(TextView)convertView.findViewById(R.id.textViewStaff\u Emp\u Department);
tvDepartment.setText(EmployeeListItems.getDepartment());
TextView tvDesignation=(TextView)convertView.findViewById(R.id.textViewStaff\u Emp\u Designation);
tvsignation.setText(EmployeeListItems.getDesignation());
TextView tPerformanceDate=(TextView)convertView.findViewById(R.id.textDate);
tvPerformanceDate.setText(EmployeeListItems.getDate());
TextView tPerformanceRate=(TextView)convertView.findViewById(R.id.empLoyeeRate);
tvPerformanceRate.setText(EmployeeListItems.getPerformancerate());
最终员工empPOsition=Employee_List.get(职位);
int positionA=员工名单索引OF(empPOsition);
notifyDataSetChanged();
系统输出打印项次(“empPOsition=“+positionA”);
如果(位置A==0)
{
员工名单。删除(职位);
}
返回视图;
}
}
这是我的日志猫错误
07-15 11:41:56.322:E/AndroidRuntime(651):致命异常:主
07-15 11:41:56.322:E/AndroidRuntime(651):java.lang.IndexOutOfBoundsException:索引3无效,大小为3
07-15 11:41:56.322:E/AndroidRuntime(651):位于java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
07-15 11:41:56.322:E/AndroidRuntime(651):在java.util.ArrayList.get(ArrayList.java:311)
07-15 11:41:56.322:E/AndroidRuntime(651):在com.sqlitedemo.EmployeeList_Adapter.getView(EmployeeList_Adapter.java:49)
07-15 11:41:56.322:E/AndroidRuntime(651):在android.widget.AbsListView.obtainView(AbsListView.java:1430)
07-15 11:41:56.322:E/AndroidRuntime(651):在android.widget.ListView.makeAndAddView(ListView.java:1745)
07-15 11:41:56.322:E/AndroidRuntime(651):在android.widget.ListView.fillDown(ListView.java:670)
07-15 11:41:56.322:E/AndroidRuntime(651):在android.widget.ListView.fillFromTop(ListView.java:727)
07-15 11:41:56.322:E/AndroidRuntime(651):位于android.widget.ListView.layoutChildren(ListView.java:1598)
07-15 11:41:56.322:E/AndroidRuntime(651):在android.widget.AbsListView.onLayout(AbsListView.java:1260)
07-15 11:41:56.322:E/AndroidRuntime(651):在android.view.view.layout(view.java:7175)
07-15 11:41:56.322:E/AndroidRuntime(651):在android.widget.RelativeLayout.onLayout(RelativeLayout.java:912)
07-15 11:41:56.322:E/AndroidRuntime(651):在android.view.view.layout(view.java:7175)
07-15 11:41:56.322:E/AndroidRuntime(651):在android.widget.FrameLayout.onLayout(FrameLayout.java:338)
07-15 11:41:56.322:E/AndroidRuntime(651):在android.view.view.layout(view.java:7175)
07-15 11:41:56.322:E/AndroidRuntime(651):在android.widget.FrameLayout.onLayout(FrameLayout.java:338)
07-15 11:41:56.322:E/AndroidRuntime(651):在android.view.view.layout(view.java:7175)
07-15 11:41:56.322:E/AndroidRuntime(651):在android.view.ViewRoot.performTraversals(ViewRoot.java:1140)
07-15 11:41:56.322:E/AndroidRuntime(651):在android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
07-15 11:41:56.322:E/AndroidRuntime(651):在android.os.Handler.dispatchMessage(Handler.java:99)上
07-15 11:41:56.322:E/AndroidRuntime(651):在android.os.Looper.loop(Looper.java:123)上
07-15 11:41:56.322:E/AndroidRuntime(651):位于android.app.ActivityThread.main(ActivityThread.java:3683)
07-15 11:41:56.322:E/AndroidRuntime(651):位于java.lang.reflect.Method.Invokenactive(本机方法)
07-15 11:41:56.322:E/AndroidRuntime(651):在java.lang.reflect.Method.invoke(Method.java:507)
07-15 11:41:56.322:E/AndroidRuntime(651):在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-15 11:41:56.322:E/AndroidRuntime(651):位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-15 11:41:56.322:E/AndroidRuntime(651):在dalvik.system.NativeStart.main(本机方法)
试试这个

改变这个

Employee_List.remove(position);

仪表
Employee_List.remove(positionA);
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;
    }

}