Eugene
Eugene

Reputation: 60184

ArrayAdapter goes beyond bounds

I have an ArrayAdapter with ArrayList filled. Each time I click on any of its item I re-fill the ArrayList and send notifyOnDataSetChange() to the adapter. But for unknown for me reason it goes out of ArrayList bounds in it's getView() method where it populates its items. I don't understand why this happens. Can you guys explain the theory of getView() invokation so I understand why this going on. Thanks in advance!

Here it is:

class MAdapter extends ArrayAdapter<String> {
    public MAdapter(Context context, int textViewResourceId, List<String> objects) {
        super(context, textViewResourceId, objects);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null) {
        LayoutInflater vi = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.file_explorer_row, null);
    } else {

    }

        String txt = itemsList.get(position); // Out of bounds happens here
        if (!txt.equals("")) {
            TextView tt = (TextView) v.findViewById(R.id.file_explorer_tv_filename);
            tt.setText(txt);
        }

    return v;
}

itemsList is declared in Outer Class.

Upvotes: 0

Views: 1133

Answers (4)

AkashG
AkashG

Reputation: 7888

Try this code:

class MAdapter extends BaseAdapter {
    List<String> objects;
    Context context;
    public MAdapter(Context context,List<String> objects) {
        super();
        this.context=context;
        this.objects=objects;
    }



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

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

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

    public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    Holder holder;
    LayoutInflater vi;
    if (v == null) {
        holder=new Holder();
        vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.file_explorer_row, null);
        holder.tt= (TextView) v.findViewById(R.id.file_explorer_tv_filename);
        v.setTag(holder);
    } else {
        holder = (Holder) v.getTag();
    }
        String txt = objects.get(position); // Out of bounds happens here
        if (!txt.equals("")) {
            holder.tt.setText(txt);
        }

    return v;
}

static class Holder{
    TextView tt;
}
}

Upvotes: 0

NagarjunaReddy
NagarjunaReddy

Reputation: 8645

change like this

public View getView(int position, View convertView, ViewGroup parent) {         
        View view = convertView;
        if (view == null) 
        {              
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);          
            view = inflater.inflate(R.layout.file_explorer_row, parent, false);
        }       

Upvotes: 1

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33534

Though i am not getting a clear view of what you are asking..i am assuming that , you are refilling the entire ArrayAdapter again....

So try this.........

Use removeView() on ListView before setting the adapter to it...

Eg:

ListView.removeView(); 
ListView.setAdapter(yourAdapter);

Upvotes: 0

Haresh Chaudhary
Haresh Chaudhary

Reputation: 4400

String txt = itemsList.get(position);
itemsList.get(position) returns a Integer Value and that you are try to store in a String..this might be the reason.

Upvotes: 0

Related Questions