简体   繁体   中英

Can't make custom SimpleAdapter

Here is my adapter

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;
}

} It gives me an error: java.lang.NullPointerException at TextView tt = (TextView) v.findViewById(R.id.vk_name); Where is my mistake? I assume the mistake is in the creating View

You first need to inflate your XML (which contains the layout of your rows) before using it.

@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);

    ...

I think you could use ViewHolder Pattern (the android documentation recommends it).

See:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM