自定义BaseAdapter的getView()方法未触发

时间:2019-05-13 06:01:35

标签: android android-arrayadapter

我正在为数组列表设置自定义基本适配器,但是未调用我的适配器的getView()方法。我尝试了许多解决方案,但没有一个对我有用。我正在从Aynctask的postExecute调用setAdapter()函数

@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");

        }

我的自定义Adapter类是

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

适配器的构造函数正在调用,但getView()方法不是

更新:我也尝试使用ArrayAdapter而不是BaseAdapter,但是不起作用

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 个答案:

答案 0 :(得分:0)

更新getView()中的条件,并删除else并扩展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;
    }

答案 1 :(得分:-1)

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