ArrayAdapter超越了界限

时间:2012-07-07 11:44:37

标签: android

我已填充ArrayAdapter ArrayList。每次我点击其中任何一个项目时,我都会重新填充ArrayList并将notifyOnDataSetChange()发送到adapter。但是对于我来说未知的原因,它在ArrayList方法中超出了getView()范围,它填充了它的项目。我不明白为什么会这样。你们能解释一下getView() invokation的理论,所以我理解为什么会这样。提前谢谢!

这是:

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在外类声明。

4 个答案:

答案 0 :(得分:1)

像这样改变

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);
        }       

答案 1 :(得分:0)

String txt = itemsList.get(position);
 itemsList.get(position)返回一个Integer Value,并且您尝试存储在String中。这可能是原因。

答案 2 :(得分:0)

虽然我没有清楚地了解你在问什么......我假设你正在重新填充整个ArrayAdapter ....

所以试试这个.........

在将适配器设置为ListView之前,在ListView上使用removeView()

例如:

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

答案 3 :(得分:0)

试试这段代码:

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;
}
}