-1

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.

Nivil Boban
  • 286
  • 1
  • 13
Vlad Skurtolov
  • 1,024
  • 1
  • 7
  • 20

4 Answers4

2
 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

Arpan Sharma
  • 2,142
  • 11
  • 22
1

Change this

return super.getView(position, convertView, parent)

to

return convertView;

Dishonered
  • 8,449
  • 9
  • 37
  • 50
1

You have to inflate convertview when it is null.

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

Also return convertview instead of super

Nivil Boban
  • 286
  • 1
  • 13
0

I suggest use BaseAdapter, it's basic, ArrayAdapter is BaseAdapter' subclass, In most cases, you do not need to inherit the BaseAdapter, use it directly.

ArrayAdapter adapter = new ArrayAdapter(context, R.layout.list_item, R.id.text, stringList);

public class MyCustomAdapter extends BaseAdapter {

    private List<Task> listData;

    public MyCustomAdapter(List<Task> list) {
        listData = list;
    }

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
        }
        Task data = getItem(position);
        TextView text = (TextView) convertView.findViewById(R.id.text);
        return convertView;
    }
}
codingnow
  • 94
  • 4