Reputation: 175
I'm trying to override the getview method like this:
public class MyArrayAdapter extends ArrayAdapter {
private final Context context;
private final ArrayList<DataList> values;
private final LayoutInflater li = LayoutInflater.from(this.getContext());
public MyArrayAdapter(Context context, int textViewResourceId,ArrayList<DataList> values) {
super(context,textViewResourceId, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
convertView=li.inflate(R.layout.simplerow,parent,false);
holder=new ViewHolder();
holder.text=(TextView) convertView.findViewById(R.layout.simplerow);
convertView.setTag(holder);
}else{
Log.d("a","entre else");
holder=(ViewHolder)convertView.getTag();
}
DataList b=values.get(1);
String c=b.toString();
holder.text.setText(values.get(position).toString());
try {
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return convertView;
}
static class ViewHolder {
TextView text;
}
every time i get to holder.text.setText(values.get(position).toString())
i receive the null pointer exception
error. VALUES is a arraylist
of type DataList and Datalist is my class who return two strings. I know Values is not null.
Upvotes: 1
Views: 763
Reputation: 86948
The reason holder.text
throws a NullPointerException is because you are using the wrong id:
convertView.findViewById(R.layout.simplerow);
So findViewById()
returns null
. Try something like:
convertView.findViewById(R.id.textView); // I made up a name, but notice the difference?
Upvotes: 3