无法解决java.lang.IndexOutOfBoundsException:索引1无效,大小为0

时间:2015-12-24 10:08:58

标签: android sqlite listview indexoutofboundsexception

Edit_WorkDetails 中,它有listView用于填充从SQLite检索到的数据。

     ListView listViewUpdate;
     CustomBaseAdapter obj;
     private SimpleCursorAdapter dataAdapter;
     Long ID, iD;
     ArrayList<DetailsBean> results = new ArrayList<DetailsBean>();

     public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View edit_details = inflater.inflate(R.layout.edit_work_details, container, false);
            dbHelper = new MyDatabaseHelper(getActivity());
            sqlcon = new WorkDetailsAPI(getContext());
            listViewUpdate = (ListView) edit_details.findViewById(R.id.listViewEdit);
            obj = new CustomBaseAdapter(getActivity(), results, listViewUpdate);
            listViewUpdate.setAdapter(obj);
            Bundle bundle = this.getArguments();
            if (getArguments() != null) {
                ID = bundle.getLong("ID"); // receive ID from other activity, Assume is ID=1
                BuildEditDetails(ID);
            }

     public void BuildEditDetails(long ID) {
            final long id = ID;
            Toast.makeText(getActivity(), id + "", Toast.LENGTH_LONG).show();
            sqlcon.open();
            Cursor cursor = sqlcon.readData(id);

            String[] columns = new String[]{MyDatabaseHelper.Project, MyDatabaseHelper.WorkDescription};

            int[] to = new int[]
                    {
                         R.id.Project, R.id.Description     };
            dataAdapter = new SimpleCursorAdapter(getActivity(), R.layout.retrieve_details, cursor, columns, to, 0);

            listViewUpdate.setAdapter(dataAdapter);

        }

单击列表后,它将转到 Edit_Details 进行编辑。

  listViewUpdate.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> listView, View view,
                                    int position, long id) {
                mClickedPosition = position;
                Cursor cursor = (Cursor) listView.getItemAtPosition(position);
                iD = cursor.getLong(cursor.getColumnIndexOrThrow("_id"));
                Toast.makeText(getActivity(), iD + "", Toast.LENGTH_LONG).show();
                Intent intent = new Intent(getActivity(), Edit_Details.class);
                intent.putExtra("ID", iD);
                startActivityForResult(intent, PROJECT_REQUEST_CODE);

            }
        });

iD代表listView中的id,并显示值2

Edit_Details

属于iD(2)的所有数据都将显示在 Edit_Details 上。点击保存button后,更新后的值将保存到SQLite并返回 Edit_WorkDetails listView

save.setOnClickListener(new View.OnClickListener() {     // if save button clicked
            @Override
            public void onClick(View v) {
                Intent returnIntent = new Intent();
                project1=Project2.getSelectedItem().toString();
                description=Description.getText().toString();
                sqlcon.open();
                sqlcon.Update(ID, project1, description);  // update the latest value in SQLite
                returnIntent.putExtra("project1", project1);
                returnIntent.putExtra("description", description);
                setResult(Activity.RESULT_OK, returnIntent);
                finish();

            }
        });

这是 Edit_WorkDetails

OnActivityResult
 @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) { // receive from Activity B and populate ListView A
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == PROJECT_REQUEST_CODE) {
                ReceiveProject = data.getStringExtra("Project");
                ReceiveDescription = data.getStringExtra("Description");

                if (mClickedPosition == -1) {  // if icon clicked
                    if (obj != null) {
                        Toast.makeText(getActivity(), "S", Toast.LENGTH_LONG).show();
                    }

                } else {
                    if (obj != null)  // obj is CustomBaseAdapter object
                        obj.changeItem(mClickedPosition, ReceiveProject, ReceiveDescription);

                }
            }
        }
    }

CustomBaseAdapter

public class CustomBaseAdapter extends BaseAdapter{   // for ListView


    private static ArrayList<DetailsBean> searchArrayList;
    private LayoutInflater mInflater;
    ListView listview;


    public CustomBaseAdapter(Context context, ArrayList<DetailsBean> results,ListView listview) {
        searchArrayList = results;
        this.listview=listview;
         mInflater = LayoutInflater.from(context);

    }

    public void changeItem(int m,String P,String D)
    {
       DetailsBean obj = new DetailsBean();
        obj.setProject("   "+P);
        obj.setDescription("  "+" Work Description : " + " "+D);
        searchArrayList.set(m,obj);
        this. notifyDataSetChanged();
    }


    public int getCount() {
        return searchArrayList.size();
    }


    public Object getItem(int position) {
        return searchArrayList.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

DetailsBean

private String project="";
 private String description="";

public class DetailsBean {

  public void setProject(String project){
            this.project=project;
        }

        public String getProject()
        {
            return project;
        }


public void setDescription(String description){
            this.description=description;
        }

        public String getDescription()
        {
            return description;
        }

    }

单击Edit_Details中的保存按钮时,应用程序崩溃。

LogCat错误

  Caused by: java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0
            at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
            at java.util.ArrayList.set(ArrayList.java:481)
            at com.example.project.myapplication.Adapter.CustomBaseAdapter.changeItem(CustomBaseAdapter.java:47)
            at com.example.project.myapplication.GUI.Edit_WorkDetails.onActivityResult(Edit_WorkDetails.java:125)
            at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:156)

有人能帮助我吗?

这两个代码有错误

 obj.changeItem(mClickedPosition, ReceiveProject, ReceiveDescription);

searchArrayList.set(m,obj);

1 个答案:

答案 0 :(得分:1)

我不认为自定义适配器中的此成员类应该是静态的:

static ArrayList<DetailsBean> searchArrayList;

&#34;静态&#34;意味着它与班级相关联,而不是与实例相关联。当你在构造函数中设置它时,每个实例都是如此。

这也是一个坏主意:

searchArrayList = results;

您应该制作副本,以便引用保持私密状态:

this.searchArrayList = new ArrayList<DetailsBean>(results);

这是问题所在:

searchArrayList.set(m,obj);

您传递了m=1,但List已初始化,因此它已空。

ArrayList<DetailsBean> results = new ArrayList<DetailsBean>();

创建CustomBaseAdapter时传递给构造函数的List。我不知道你在哪里改变它。这些结果来自SQL数据库吗?也许你的查询没有返回任何结果,但你认为它确实没有结果。

什么是DetailsBean?这两个通用名称相结合,无法提供任何有关此对象的信息。我重新考虑一下。看起来您想要该对象内的项目名称和描述。为什么不叫它Project?那是什么。

这些私有变量是否应该在DetailsBean内?如果没有,谁拥有它们?那看起来也不错。这甚至可以编译吗?

您的代码不遵循Java编码标准。您的命名不一致且令人困惑。变量名称应以小写字母开头;课程以大写字母开头。