androidandroid-arrayadaptergetview

My getView method in my custom array adapter is not working


can you give me some advice about my getView methood;
I have custom adapter that extends ArrayAdapter(context, id, list)

public class myCustomAdapter extends ArrayAdapter{


private List obj = null;
public myCustomAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull List objects) {
    super(context, resource, objects);
    obj = objects;

}

@Override
public int getCount() {
    return obj.size();
}

@Nullable
@Override
public Object getItem(int position) {
    return super.getItem(position);
}



@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    Task myTask = (Task) getItem(position);
    if(convertView==null){
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent);
    }
    TextView myTextView = (TextView) convertView.findViewById(R.id.textView);
    myTextView.setText(myTask.getTaskText());
    CheckBox myCheckBox = (CheckBox) convertView.findViewById(R.id.checkBox);
    myCheckBox.setChecked(myTask.getChecked());

    return convertView;
}

}

problem 1 : super: Unchecked call to ArrayAdapter(..) as member of raw type
problem 3 : getTaskText //methood from my Task class// may produce NPE

I would appreciate any help if possible.


Solution

  •  if(convertView!=null){
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent);
    }
    

    should be

    if(convertView==null){
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent);
        }
    

    you have to inflate when the convertview is null. not when its not null. And then return this from your getView() method

    Also i can not see getCount() method. It is also very important to tell the adapter about how many items to inflate