2
我有一個定製適配器用於我的ListView
。它給我NullPointerException
在setAdapter
。我經歷了幾次我的代碼,無法弄清楚導致NPE的原因。以下是相關的代碼片段:定製適配器不工作
活動:
ListView mListView;
Vector<DashboardBean> dashboardBean= new Vector<DashboardBean>();
MyCustomAdapter adapter;
adapter = new MyCustomAdapter(MyDashboardActivity.this,
R.layout.testdashboard_row, dashboardBean);
adapter.clear();
dashboardBean.add(new DashboardBean(projects, workRequests, status));
mListView.setAdapter(adapter);
Dashboardbean:
public class DashboardBean {
public String project;
public String workRequest;
public String startDate;
public String status;
public String actualHours;
public String actualMins;
public DashboardBean(String project, String workRequest,String status)
{
this.project = project;
this.workRequest = workRequest;
this.status = status;
}
}
CustomAdapter:
public class MyCustomAdapter extends ArrayAdapter<DashboardBean> {
Context context;
int layoutResourceId;
DashboardBean currentMRB;
Vector<DashboardBean> data;
public MyCustomAdapter(Context context, int layoutResourceId, Vector<DashboardBean> data)
{
super(context,layoutResourceId,data);
this.layoutResourceId = layoutResourceId;
this.context=context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
MyStringReaderHolder holder;
if(row==null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent,false);
holder= new MyStringReaderHolder();
holder.project = (TextView)row.findViewById(R.id.project);
holder.workRequest = (TextView)row.findViewById(R.id.work_request);
holder.status = (TextView) row.findViewById(R.id.status);
row.setTag(holder);
}
else
{
holder=(MyStringReaderHolder) row.getTag();
}
DashboardBean mrb = data.elementAt(position);
holder.project.setText(mrb.project);
holder.workRequest.setText(mrb.workRequest);
holder.status.setText(mrb.status);
return row;
}
static class MyStringReaderHolder
{
TextView project, workRequest, startDate, status, actualHours;
}
}
誰能幫助?
嗯,它被初始化了。但是,對於錯誤的ID。非常感謝你指出:) – Harsh 2012-07-10 21:06:18