显示的自定义列表为空

时间:2011-09-13 08:58:23

标签: android listview android-layout

我有一个donorview类,它扩展了linearlayout,如下所示:

public class donorview extends LinearLayout {
private TextView donortext;
private String donorstr;
private ImageButton call;
private ImageButton msg;
private String donornumber;
private Context context1;
private View convertView;

public donorview(Context context, String donorname1, String donornum) {
    super(context);
    context1 = context;
    // TODO Auto-generated constructor stub
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = inflater.inflate(R.layout.listformat, null);
    convertView.setClickable(true);
    donortext = (TextView) findViewById(R.id.donornametext);
    call = (ImageButton) findViewById(R.id.call);
    msg = (ImageButton) findViewById(R.id.msg);
    System.out.println("donorname:"+donorname1);
    System.out.println("donornum:"+donornum);
    donortext.setText(donorname1);
    System.out.println("after setting text");
    donorstr = donorname1;
    donornumber = donornum;
}

void onClickCall(){
    Intent callIntent = new Intent(Intent.ACTION_CALL);
    callIntent.setData(Uri.parse("tel:" + donornumber));
      convertView.getContext().startActivity(callIntent);
}
public void setdonorname(String donorname12) {
    donortext.setText(donorname12);
}

public String getdonorname() {
    return this.donorstr;
}
}

我得到一个NullPoinerException为

java.lang.NullPonintereException
at com.wglxy.example.dash1.donorview.<init>(donorview.java:35)

设置textview donortext的文本。我的listformat的XML布局是

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout android:id="@+id/linerlayout1"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView android:id="@+id/donornametext" android:layout_height="wrap_content"
        android:layout_width="wrap_content"  android:layout_weight="1" android:text="a"/>

    <ImageButton android:layout_width="wrap_content" android:id="@+id/call"
        android:layout_height="wrap_content" android:contentDescription="@string/description_about"
        android:src="@drawable/call" android:onClick="onClickCall"
        android:background="#ffffff" />
    <ImageButton android:layout_width="wrap_content" android:id="@+id/msg"
        android:layout_height="wrap_content" android:contentDescription="@string/description_about"
        android:src="@drawable/msg" android:onClick="onClickMsg"
        android:background="#ffffff" />

</LinearLayout>

我不明白我做错了什么。显示的列表为空。当我正在打印donorname和donornum时,它正在打印,但显示的自定义列表是空的。我一直试图弄清楚它很长一段时间。有人可以帮帮我吗? 我的适配器代码如下

   public class donorAdapter extends BaseAdapter {

private Activity activity;

ArrayList<String> names = new ArrayList<String>();
ArrayList<String> nums = new ArrayList<String>();
private Context context;

private String strnum;

private String strname;
private static LayoutInflater inflater = null;


public donorAdapter(Context context, Activity a, ArrayList<String> names,
        ArrayList<String> nums) {
    this.context = context;
    activity = a;
    this.names = names;
    this.nums = nums;
    inflater = LayoutInflater.from(activity);
}

public int getCount() {
    return names.size();
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    donorview fv;
    if (convertView == null) {
        System.out.println("inside getview:"+names.get(position).toString());
        strname=names.get(position).toString();
        strnum=nums.get(position).toString();
        System.out.println("inside getview:"+nums.get(position).toString());
        fv = new donorview(context, strname,strnum);
        convertView = fv;
    }
    else {
        ((donorview) convertView).setdonorname(names.get(position)
                .toString());
    }
    convertView.setClickable(true);
    convertView.setFocusable(true);
    return convertView;
}

}

- 提前谢谢

2 个答案:

答案 0 :(得分:4)

你应该使用

donortext = (TextView)convertView.findViewById(R.id.donornametext);

并且还用

替换你的代码
convertView = inflater.inflate(R.layout.listformat, this);

答案 1 :(得分:1)

替换

findViewById

convertView.findViewById()

你使用findViewById,当你已经设置了活动的内容时,如果你没有设置活动的内容,那么你必须指定它必须查看哪个视图来获取id。您必须对其余视图执行相同的操作。