I am setting a custom base adapter for a array list But the getView() method of the my adapter is not called. I tried many solutions but non of them worked for me. I am calling setAdapter() function from postExecute of Aynctask

@Override
        protected void onPostExecute(ArrayList<Object> objects) {
            MyListAdapter myAdapter = new MyListAdapter(MySearchList.this,objects);
            ListView lv  = findViewById(R.id.MyList);
            lv.setAdapter(myAdapter);
            Log.d("done", "onPostExecute: Done");

        }

And my custom Adapter class is

public class MyListAdapter extends BaseAdapter {
    private final ArrayList<Object> myList;
    private final Activity context;
    private LayoutInflater inflater;

    public MyListAdapter(Activity context , ArrayList<Object> myList) {

        this.myList = myList;
        inflater = LayoutInflater.from(context);
        this.context = context;
        Log.d("constructor", String.valueOf(myList.size()));
    }


    @Override
    public int getCount() {
        Log.d("", "getCount: ");
        return myList.size();
    }

    @Override
    public Object getItem(int position) {
        return myList.get(position);
    }

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


    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View rowView;
        Log.e("adapting", myList.get(position).toString() );
        if (myList.get(position) instanceof wordDefination){
            Log.e("adapting", myList.get(position).toString() );
            rowView = inflater.inflate(R.layout.word_meaning_single,null,true);

        }

        else{
            rowView = inflater.inflate(R.layout.word_meaning_single,null,true);
        }

        return rowView;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

constructor of adapter is calling but the getView() method is not

update : i also tried to use ArrayAdapter instead of BaseAdapter But not working

public class MyListAdapter extends ArrayAdapter<Object> {
    private final ArrayList<Object> myList;
    private final Activity context;
    private LayoutInflater inflater;

    public MyListAdapter(Activity context , ArrayList<Object> myList) {
        super(context,R.layout.word_meaning_single,myList);
        this.myList = myList;
        inflater = LayoutInflater.from(context);
        this.context = context;
        Log.d("constructor", String.valueOf(myList.size()));
    }
@NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View rowView;
        Log.e("adapting", myList.get(position).toString() );
        if (myList.get(position) instanceof wordDefination){
            Log.e("adapting", myList.get(position).toString() );
            rowView = inflater.inflate(R.layout.word_meaning_single,null,true);

        }

        else{
            rowView = inflater.inflate(R.layout.word_meaning_single,null,true);
        }

        return rowView;
    }
}

2 Answers

-1
kundan kamal On
 Log.e("adapting", myList.get(position).toString() );
        if (myList.get(position) instanceof wordDefination){
            Log.e("adapting", myList.get(position).toString() );
            rowView = inflater.inflate(R.layout.word_meaning_single,parent,false);

        }

        else{
            rowView = inflater.inflate(R.layout.word_meaning_single,parent,false);
        }

Also check the size of list in


    @Override
    public int getCount() {
        Log.d("", "getCount: "+myList.size());
        return myList.size();
    }
0
pratik vekariya On

Update if condition in your getView() and remove else and extends BaseAdapter

public class MyListAdapter extends BaseAdapter {
    private final ArrayList<Object> myList;
    private final Activity context;

    public MyListAdapter(Activity context , ArrayList<Object> myList) {

        this.myList = myList;
        this.context = context;
        Log.d("===constructor", String.valueOf(myList.size()));
    }


    @Override
    public int getCount() {
        Log.d("===", "getCount: "+myList.size());
        return myList.size();
    }

    @Override
    public Object getItem(int position) {
        return myList.get(position);
    }

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


    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View rowView=convertView;
        Log.d("===adapting", myList.get(position).toString() );
        if (convertView == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            rowView = inflater.inflate(R.layout.word_meaning_single,parent,false);
        Log.d("adapting", myList.get(position).toString() );

        }


        return rowView;
    }