tags:

views:

1045

answers:

1

I am building a custom listview used to list contacts from the contact cursor into a list with two text view for the phone number and name of the contact as well as a place for the images. I am getting a nullpoint error when i try to setText for my TextView Even though i checked the variable to see if they contained a string I have no ideas whats wrong with it.

 public View getView(int position, View convertView, ViewGroup parent) {
 View rowView=convertView;
 ViewCache viewCache=null;

 if (rowView==null) {
  LayoutInflater inflater=(LayoutInflater)_context.getSystemService(
        Context.LAYOUT_INFLATER_SERVICE);

  rowView=inflater.inflate(R.layout.row, null);
  viewCache = new ViewCache(rowView);
  rowView.setTag(viewCache);
 }
 else {
  viewCache=(ViewCache)rowView.getTag();
 }
 _cursor.moveToPosition(position);
 int id = _cursor.getColumnIndex(People._ID);
 int numbercolumn = _cursor.getColumnIndex(People.NUMBER);
 int namecolumn = _cursor.getColumnIndex(People.NAME);
 String tag = "tag";
 Log.i(tag, getItem(position).toString());

 String name;
 String number;
 number = _cursor.getString(numbercolumn);

 name = _cursor.getString(namecolumn);

 Log.i("test6386", number);
 Log.i("Test456", name);

 TextView nameView = viewCache.getName();
 TextView numberView = viewCache.getNumber();
 nameView.setText(name);
 numberView.setText(number);

 Uri uri = ContentUris.withAppendedId(People.CONTENT_URI, _cursor.getLong(id));
 Bitmap bitmap = People.loadContactPhoto(_context, uri, R.drawable.icon, null);


 viewCache.getIcon().setImageBitmap(bitmap);

 return(rowView);
    }
+1  A: 

I figure it out you have to overwrite the newview and bindview methods

public class RowAdapter extends CursorAdapter {

private final LayoutInflater mInflater;

public RowAdapter(Context context, Cursor c) {
super(context, c);
mInflater = LayoutInflater.from(context);

}

@Override
public void bindView(View view, Context context, Cursor cursor) {

ViewCache viewCache = null;
View rowView = view;
viewCache = (ViewCache) rowView.getTag();

ImageView imageView = (ImageView) view.findViewById(R.id.ContactIcon);
int id = cursor.getColumnIndex(HelpiDB.KEY_ID);
int namecolumn = cursor.getColumnIndex(HelpiDB.KEY_NAME);
int numbercolumn = cursor.getColumnIndex(HelpiDB.KEY_NUMBER);
Uri uri = ContentUris.withAppendedId(People.CONTENT_URI, cursor.getLong(id));

Bitmap bitmap = People.loadContactPhoto(context, uri, R.drawable.icon, null);

String name = cursor.getString(namecolumn);
String number = cursor.getString(numbercolumn);

imageView.setImageBitmap(bitmap);

TextView nameTextView = viewCache.getName();
nameTextView.setText(name);

TextView numberTextView = viewCache.getNumber();
numberTextView.setText(number);
ImageView icon = (ImageView) view.findViewById(android.R.id.icon);
boolean crossed = Boolean.valueOf(cursor.getString(HelpiDB.CHECK_COLUMN));

if (crossed) {
    icon.setImageState(new int[] { android.R.attr.state_checked }, true);

} else {
    icon.setImageState(new int[] {}, true);

}

}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {

final View view = mInflater.inflate(R.layout.row, parent, false);
ViewCache viewCache = new ViewCache(view);
view.setTag(viewCache);
bindView(view, context, cursor);
return view;

}

}