0

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

这里是:

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

4 回答 4

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);
        }       
于 2012-07-07T12:05:39.350 回答
0

String txt = itemsList.get(position);
itemsList.get(position)返回一个整数值,并且您尝试将其存储在字符串中。这可能是原因。

于 2012-07-07T11:52:30.067 回答
0

虽然我没有清楚地了解您的要求..我假设您正在重新填充整个 ArrayAdapter....

所以试试这个…………

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

例如:

ListView.removeView(); 
ListView.setAdapter(yourAdapter);
于 2012-07-07T12:01:58.673 回答
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;
}
}
于 2012-07-07T12:10:04.187 回答