无法制作自定义SimpleAdapter

时间:2015-04-05 14:24:07

标签: java android

这是我的适配器

public class Vk_row_adapter extends SimpleAdapter {
final String ATTRIBUTE_NAME_TEXT_NAME = "text_name";
final String ATTRIBUTE_NAME_TEXT_PLACE = "text_place";
final String ATTRIBUTE_NAME_IMAGE = "image";
private List<? extends Map<String, ?>> results;


public Vk_row_adapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
    super(context, data, resource, from, to);
    this.results = data;
}
public View getView(int position, View view, ViewGroup parent){

    View v = view;

    TextView tt = (TextView) v.findViewById(R.id.vk_name);
    tt.setText((CharSequence) results.get(position).get(ATTRIBUTE_NAME_TEXT_NAME));

    TextView bt = (TextView) v.findViewById(R.id.vk_raiting);
    tt.setText((CharSequence) results.get(position).get(ATTRIBUTE_NAME_TEXT_PLACE));

    ImageView vt = (ImageView)v.findViewById(R.id.vk_photo);
    vt.setImageBitmap((android.graphics.Bitmap) results.get(position).get(ATTRIBUTE_NAME_IMAGE));

    return v;
}

} 它给了我一个错误: java.lang.NullPointerException TextView tt = (TextView) v.findViewById(R.id.vk_name); 我的错误在哪里?我认为错误在于创建视图

1 个答案:

答案 0 :(得分:2)

首先需要在使用之前对XML(包含行的布局)进行充气。

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(R.layout.rowlayout, parent, false);

    TextView textView = (TextView) rowView.findViewById(R.id.label);

    ...

我认为您可以使用ViewHolder Pattern(Android文档推荐它)。

请参阅: