Custom list displayed is empty
I have a donorview class which extends linearlayout which is as follows:
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);
开发者_StackOverflow社区}
public void setdonorname(String donorname12) {
donortext.setText(donorname12);
}
public String getdonorname() {
return this.donorstr;
}
}
I get a NullPoinerException as
java.lang.NullPonintereException
at com.wglxy.example.dash1.donorview.<init>(donorview.java:35)
while setting the text of the textview donortext. My XML layout of the listformat is
<?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>
I dont understand what I am doing wrong. The list that is being displayed is empty. When i am printing the donorname and donornum it is being printed but the custom list displayed is empty.I have been trying to figure it out from a long time. Could anyone please help me with this?
My adapter code is as follows 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;
}
}
-Thanks in advance
You should use
donortext = (TextView)convertView.findViewById(R.id.donornametext);
and also replace your code by
convertView = inflater.inflate(R.layout.listformat, this);
Replace
findViewById
with
convertView.findViewById()
you use findViewById, when you have already set the content for the activity, in your case you did not , so you have to specify which view it has to look to get the id. You have to do the same for the rest of your views.
精彩评论