如何跨活动重用适配器

时间:2012-07-20 21:33:01

标签: android android-listview

我有一个自定义适配器,我正用于我的一项活动。它完美地运作。我正试图在我的其他活动中使用相同的适配器。这不起作用!我试图删除大多数依赖项(最好我的知识)但显然有些仍然存在。或者可能还有别的东西。我试图在适配器内打印出一个语句。它打印第一个活动,不打印第二个活动。因此,我可以告诉控件在第二种情况下没有到达适配器。我没有收到任何错误。这是我的活动的java代码:

mydashboard是第一个活动中setContentView内使用的资源文件的名称。 dashboardlistmydashboard中的UI组件。

RelativeLayout layout = (RelativeLayout) findViewById(R.id.inflate_workrequestsearch);

        LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

        View dashboard = layoutInflater.inflate(R.layout.mydashboard, null);

        layout.addView(dashboard);

        mListView = (ListView) dashboard.findViewById(R.id.dashboardList);



        adapter = new MyCustomAdapter(SearchActivity.this, 
              R.layout.testdashboard_row, dashboardBean);

        adapter.clear();

    for (DashboardEntry dashboardEntry : dashboardEntries){
        System.out.println("Step 2");

        startDate = dashboardEntry.getCaseOpenDate();
        workRequests = dashboardEntry.getWorkRequestName(); 
        projects = dashboardEntry.getProjectName();
        status = dashboardEntry.getStatusDesc();
        actualHrs = dashboardEntry.getActHrs();
        actualMins = dashboardEntry.getActMins();


       dashboardBean.add(new DashboardBean(startDate, projects, workRequests, status, 
            actualHrs, actualMins)); 
       }    

        mListView.setAdapter(adapter);

CustomeAdapter:

DashboardBean只是一个容纳数据的bean。

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.startDate  = (TextView)row.findViewById(R.id.start_date);
        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);
        holder.actualHours = (TextView) row.findViewById(R.id.actual_hours);

        row.setTag(holder);
    }
    else
    {
        holder=(MyStringReaderHolder) row.getTag();
    }

 /*   if (position % 2 == 0){
        row.setBackgroundResource(R.drawable.alterselector1);
    } else {
        row.setBackgroundResource(R.drawable.alterselector2);
    }
  */  
    DashboardBean mrb = data.elementAt(position);
   // System.out.println("Position="+position);

    holder.startDate.setText(mrb.startDate);
    holder.project.setText(mrb.project);
    holder.workRequest.setText(mrb.workRequest);
    holder.status.setText(mrb.status);
    holder.actualHours.setText(mrb.actualHours+" h "+mrb.actualMins+" m ");

    System.out.println("Inside adapeter");
    return row;
}

 static class MyStringReaderHolder
 {
     TextView project, workRequest, startDate, status, actualHours;

 }
 }

有谁能告诉我,我在这里失踪了什么?谢天谢地。

0 个答案:

没有答案