-1

My problem is when call AllMessageAdapter this BaseAdapter I am set getCount() is 1 but getView(int i, View _view, ViewGroup viewGroup) is call always 3 times in getCount. This BaseAdapter is called in a Fragment

BaseAdapter Code

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

I have Check through Log then Log.e("EEEEEEEEEEEE","NNNNNNNNNNNN"); this is print 1 time but Log.e("VALUE",""+i); is print 3 time. I don't no what is problem.

Mike
  • 4,550
  • 4
  • 33
  • 47
  • *I don't no what is problem.* me too ... seriously, why you think that multiple getView calls is the problem? you shouldn't bother about this ... (unless, unless you did put ListView into Scrollable and apply the stupid "fix" from SO) – Selvin Dec 09 '15 at 12:34
  • @Selvin it's nothing a short debug-session couldn't find the answer to. – Shark Dec 09 '15 at 13:02
  • Just don't bother ... the AdapterView(like ListView) **may call it multiple times** why? it depends on used AdapterView ... if you are intersted, add `new Excpetion().printStackTrace();` and study the logs :) (i bet on 2x from onMesure and once from "real" adding to the parents layout) – Selvin Dec 09 '15 at 13:03
  • why call multiple time. this is my Questin if you know then given simple answer.@Selvin – Abhinav Singh Dec 09 '15 at 13:09

2 Answers2

0

Can you put some more information like the layout xml. getView may be called multiple times when it is changing maybe you set your height=wrap_content on listview

BigApeWhat
  • 321
  • 3
  • 15
0

You can see here the different internal calls to the Adapter.getCount() method.

But you must keep in mind that you have no control on how this method is gonna be called.

It can be called multiple times and that's why you have to keep it as fast as possible.

   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;
}
}
Abhinav singh
  • 1,448
  • 1
  • 14
  • 31