I want to remove 0th position from list view item in Android. I have done to populate the list view from SQLite database.But right now i want to remove 0th position item from list view in onCreate() method. I don't know how to do this.I have tried but my app is crashes and getting exception for ArrayIndexOutOfBoundException Invallid Index 3, Size is 3.Can some one help me to sort out how to do this. Thanks to appreciat.
Here is my Adapter Class Code.
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)
Instead of check for positionA
check for position
only and try to remove after inflate layout and befor set the view data.
Try this
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;
}
}
I think This Will work,thanks;-)
Try this..
Change this
Employee_List.remove(position);
to
Employee_List.remove(positionA);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.