基础适配器

时间:2015-12-09 12:17:06

标签: android baseadapter

我的问题是,当AllMessageAdapter来电时BaseAdapter我设置为getCount()为1,但getView(int i, View _view, ViewGroup viewGroup)getCount中始终被调用3次。这个BaseAdapterFragment

中调用

BaseAdapter代码

public class AllMessageAdapter extends BaseAdapter {

    private static LayoutInflater inflater = null;

    public AllMessageAdapter(Context _context, JSONArray jobj, View.OnClickListener listener){
        inflater = (LayoutInflater)_context.getSystemService(_context.LAYOUT_INFLATER_SERVICE);

        Log.e("EEEEEEEEEEEE","NNNNNNNNNNNN");
    }

    @Override
    public int getCount() {
        return 1;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View _view, ViewGroup viewGroup) {
        Log.e("VALUE",""+i);

        View view = _view;
        if(view == null) {
            view = inflater.inflate(R.layout.all_message, null);
        }
        return view;
    }
}

我已检查Log,然后Log.e("EEEEEEEEEEEE","NNNNNNNNNNNN");这是打印1次,但Log.e("VALUE",""+i);打印3次。我不是没有问题。

2 个答案:

答案 0 :(得分:0)

你能提供更多的信息,比如布局xml。 getView可能会在更改时被多次调用,也许您可​​以在listview上设置height=wrap_content

答案 1 :(得分:0)

您可以看到hereAdapter.getCount()方法的不同内部调用。

但是你必须记住,你无法控制如何调用这种方法。

它可以被多次调用,这就是为什么你必须尽可能快地保持它。

   public class AllMessageAdapter extends BaseAdapter {

private static LayoutInflater inflater = null;

public AllMessageAdapter(Context _context, JSONArray jobj, View.OnClickListener listener){
    inflater = (LayoutInflater)_context.getSystemService(_context.LAYOUT_INFLATER_SERVICE);

    Log.e("EEEEEEEEEEEE","NNNNNNNNNNNN");
}

@Override
public int getCount() {
    return 1;
}

@Override
public Object getItem(int i) {
    return i;
}

@Override
public long getItemId(int i) {
    return i;
}

@Override
public View getView(int i, View _view, ViewGroup viewGroup) {
    Log.e("VALUE",""+i);

    View view = _view;
    if(view == null) {
        view = inflater.inflate(R.layout.all_message, null);
    }
    return view;
}
}