0

I am getting this error when I click on item in listView,I have 2 items in the arraylist,when I press on the first one its logging the number of the object ,however,when I press the second one its crashing and give me this error below:

java.lang.IndexOutOfBoundsException: Index: 2, Size: 2

listView on item click:

feedListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
        // TODO Auto-generated method stub


            MyData item = (MyData) parent.getAdapter().getItem(position);


            Log.e("Item in List", String.valueOf(item.getItem(position)));

    }
});

Adding objects and adapter to listView:

ParseQuery<ParseObject> query = ParseQuery.getQuery("_User");
    query.orderByDescending("createdAt");
    query.setLimit(20);

    query.findInBackground(new FindCallback<ParseObject>() {
        @Override
        public void done(List<ParseObject> objects, ParseException e) {

            if (e == null){

                for (ParseObject allData : objects){

                     userNameString = allData.getString("username");
                     nameOfBusinessString = allData.getString("BussinessName");

                     image = (ParseFile) allData.get("Pictures");

                    if (image == null){
                        Log.e("Image", "NULL");
                    }else{
                        image.getDataInBackground(new GetDataCallback() {
                            @Override
                            public void done(byte[] data, ParseException e) {

                                if (e == null){

                                    byteArray = data;

                                    icon = BitmapFactory.decodeByteArray(
                                            data, 0, data.length);

                                    date = allData.getCreatedAt();
                                    DateFormat df = new SimpleDateFormat("HH:mm:ss\nyyyy-MM-dd");
                                    reportDate = df.format(date);

                                    arrayList.add(new MyData(reportDate,allData.getString("username"),allData.getString("BussinessName")allData.getString("PeopleInBussiness"),data));
                                }

                                adapter = new MyAdapter(FeedActivity.this, arrayList);
                                feedListView.setAdapter(adapter);
                                adapter.notifyDataSetChanged();

                            }
                        });
                    }




                }


            }

        }
    });

Adapter:

class MyAdapter extends BaseAdapter {
    private final Context context;
    private final ArrayList<MyData> arrayList;

    public MyAdapter(Context context, ArrayList<MyData> arrayList) {
        this.context = context;
        this.arrayList = arrayList;
    }
    @Override
    public int getCount() {
        return arrayList.size();
    }
    @Override
    public Object getItem(int position) {
        return arrayList.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        Log.e("getView", String.valueOf(position));

        convertView = null;

        ViewHolder holder;

        if (convertView == null){
            holder = new ViewHolder();

            convertView = LayoutInflater.from(context).inflate(R.layout.mylist, parent, false);

            holder.Date = convertView.findViewById(R.id.Date);
            holder.userName = convertView.findViewById(R.id.userName);
            holder.BusinessName = convertView.findViewById(R.id.BusinessName);
            holder.imageView = convertView.findViewById(R.id.icon);
            convertView.setTag(holder);

        }else {
            holder = (ViewHolder) convertView.getTag();
        }





        holder.Date.setText(arrayList.get(position).getDate());
        holder.userName.setText(arrayList.get(position).getUserName());
        holder.BusinessName.setText(arrayList.get(position).getBusinessName());

        byte[] byteArray = arrayList.get(position).getPicture();

        Bitmap icon = BitmapFactory.decodeByteArray(
                byteArray, 0, byteArray.length);


        holder.imageView.setImageBitmap(icon);


        return convertView;
    }

    static class ViewHolder {
        TextView Date;
        TextView userName;
        TextView BusinessName;
        ImageView imageView;
    }


}

MyData Class:

public class MyData {

    private final String userName;
    private final String businessName;
    private final String date;
    private final String worthComming;
    private final String peopleInBusiness;
    private final byte[] picture;

    public MyData(String date, String userName, String businessName, byte[] picture) {

        this.userName = userName;
        this.businessName = businessName;
        this.date = date;
        this.picture = picture;
    }


    public int getItem(int position) {
        return position;
    }

    public String getUserName() {
        return userName;
    }

    public String getBusinessName() {
        return businessName;
    }

    public String getDate() {
        return date;
    }


    public byte[] getPicture() {
        return picture;
    }




}

Thank you !

HoLoGram
  • 167
  • 10
  • Your `MyData` class is messed up. It does not contain a field `position` – leonardkraemer May 10 '21 at 16:01
  • Get thanks for reply! The link above didn't helped. how can I add the field "position" to MyData class ? Thanks – HoLoGram May 10 '21 at 16:15
  • 2
    People can't just "add the field position" without knowing what you're trying to do, and frankly, your code is convoluted enough to make your intentions non-obvious. Look at `MyData.getItem` it takes an int called `position` and immediately returns that very same int. Why? What is the item that getItem refers to? Shouldn't you be returning some object from some data structure at that index, rather than the index itself? – MarsAtomic May 10 '21 at 16:23
  • @MarsAtomic thanks for your reply.I am trying to get the position of the listview row so I can take data only from that row and pass it to another activity. – HoLoGram May 10 '21 at 16:35
  • `MyData` has nothing to do with the presentation of your data, so you shouldn't be asking the data where it is in the ListView. When you click on a ListView, you get an onItemClick event. That event provides the position of the selected item, and you can get the item via `list.getItemAtPosition(position)`. – MarsAtomic May 10 '21 at 17:06
  • Also, look at LogCat. The exception is always listed with the line number of the code causing the exception. Which line of code is that? Not the line number, but the actual line of code? I'm guessing it's your Log statement. – MarsAtomic May 10 '21 at 17:10
  • Thanks,but when I am trying to get another data its giving me the same error,for example I added these lines to get another data,the first item on the list is ok but the other one crash with the same error------ MyData myData = (MyData) arrayList.get(position); String test = myData.getBusinessName(); Log.e("test",test); – HoLoGram May 10 '21 at 17:11
  • This is the error line in logcat -------- MyData myData = (MyData) arrayList.get(position); – HoLoGram May 10 '21 at 17:11
  • Look at my previous comment. `position` should come from the event. You shouldn't be arbitrarily making up some position field on your own. You need to fix your code to obtain position from the event. – MarsAtomic May 10 '21 at 19:09

0 Answers0