应用自定义的ListView对于一个的CursorAdapter

应用自定义的ListView对于一个的CursorAdapter

本文介绍了应用自定义的ListView对于一个的CursorAdapter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立用于列出从联系人光标接触与该联系人的电话号码和姓名,以及用于图像的地方两个文本视图列表,自定义列表视图。我得到一个错误nullpoint当我尝试的setText我的TextView即使我检查变量,看看他们是否包含字符串我没有想法什么不妥的地方。

 公共查看getView(INT位置,查看convertView,父母的ViewGroup){
 查看rowView = convertView;
 视图缓冲视图缓冲= NULL; 如果(rowView == NULL){
  LayoutInflater吹气=(LayoutInflater)_context.getSystemService(
        Context.LAYOUT_INFLATER_SERVICE);  rowView = inflater.inflate(R.layout.row,NULL);
  视图缓冲=新视图缓冲(rowView);
  rowView.setTag(视图缓冲);
 }
 其他{
  视图缓冲=(视图缓冲)rowView.getTag();
 }
 _cursor.moveToPosition(位置);
 INT ID = _cursor.getColumnIndex(People._ID);
 INT numbercolumn = _cursor.getColumnIndex(People.NUMBER);
 INT名称列= _cursor.getColumnIndex(People.NAME);
 字符串标记=标记;
 Log.i(标签的getItem(位置)的ToString()); 字符串名称;
 串号;
 数= _cursor.getString(numbercolumn); 名称= _cursor.getString(名称列); Log.i(test6386号码);
 Log.i(Test456,名); TextView的nameView = viewCache.getName();
 TextView的numberView = viewCache.getNumber();
 nameView.setText(名);
 numberView.setText(数); URI URI = ContentUris.withAppendedId(People.CONTENT_URI,_cursor.getLong(ID));
 位图位图= People.loadContactPhoto(_context,URI,R.drawable.icon,NULL);
 viewCache.getIcon()setImageBitmap(位图)。 回报(rowView);
    }


解决方案

我看着办吧,你必须覆盖NewView的和bindview方法

 公共类RowAdapter扩展的CursorAdapter {私人最终LayoutInflater mInflater;公共RowAdapter(上下文的背景下,光标C){
超(背景下,C);
mInflater = LayoutInflater.from(上下文);}@覆盖
公共无效bindView(查看视图,上下文的背景下,光标光标){视图缓冲视图缓冲= NULL;
查看rowView =视图。
视图缓冲=(视图缓冲)rowView.getTag();ImageView的ImageView的=(ImageView的)view.findViewById(R.id.C​​ontactIcon);
INT ID = cursor.getColumnIndex(HelpiDB.KEY_ID);
INT名称列= cursor.getColumnIndex(HelpiDB.KEY_NAME);
INT numbercolumn = cursor.getColumnIndex(HelpiDB.KEY_NUMBER);
URI URI = ContentUris.withAppendedId(People.CONTENT_URI,cursor.getLong(ID));位图位图= People.loadContactPhoto(背景下,URI,R.drawable.icon,NULL);字符串名称= cursor.getString(名称列);
串号= cursor.getString(numbercolumn);imageView.setImageBitmap(位图);TextView的nameTextView = viewCache.getName();
nameTextView.setText(名);TextView的numberTextView = viewCache.getNumber();
numberTextView.setText(数);
ImageView的图标=(ImageView的)view.findViewById(android.R.id.icon);
布尔交叉=通过Boolean.valueOf(cursor.getString(HelpiDB.CHECK_COLUMN));如果(交叉){
    icon.setImageState(新INT [] {} android.R.attr.state_checked,真实);}其他{
    icon.setImageState(新INT [] {},真);}}@覆盖
公共查看NewView的(上下文的背景下,光标光标的ViewGroup父){最后查看视图= mInflater.inflate(R.layout.row,父母,假);
视图缓冲视图缓冲=新视图缓冲(视图);
view.setTag(视图缓冲);
bindView(查看,背景,光标);
返回视图。}

}

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);
    }
解决方案

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;

}

}

这篇关于应用自定义的ListView对于一个的CursorAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 21:26