1

我有一个多维arrayList.eg

arrayList 0 --> {startdate, duration, status}
          1 --> {startdate, duration, status}
          2 --> {startdate, duration, status}

我已经捆绑了这个数组并将其发送到下一个 Activity。在这个活动中,我可以将 arrayList 的第一个索引传递给适配器并显示数据(虽然它显示了 5 次而不是一次,但另一个问题)。

我的问题是如何将多个数组传递给适配器或让适配器迭代多个数组?

在下面的片段数组中是多维数组。我已经硬编码了 position(0) 以传递给适配器。我如何将内部的其他 7 个数组传递给适配器。

public class GetRota extends Activity {

    private static final String TAG = GetRota.class.getSimpleName();
    ListView listView;
    Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.getrotalayout);

        listView = (ListView)findViewById(R.id.rotalist);
        intent = this.getIntent();
        Bundle bundle = intent.getBundleExtra("rotaArrayBundle");

        @SuppressWarnings("unchecked")
        ArrayList array =  (ArrayList) bundle.get("rotaArray");             
        String record = array.get(0).toString();
        String[] itemsInRecord = record.split(",");
        String[] recordItem = new String[5];

       for(int x = 0; x < itemsInRecord.length; x++){

           Log.e(TAG, "token = " + itemsInRecord[x]);
           recordItem[x] = itemsInRecord[x];
      }

       MySimpleArrayAdapter arrayAdapter = new MySimpleArrayAdapter(this, recordItem);
       listView.setAdapter(arrayAdapter);


    }// end of onCreate


    private class MySimpleArrayAdapter extends ArrayAdapter<String> {
          private final Context context;
          private final String[] values;

          public MySimpleArrayAdapter(Context context, String[] values) {
            super(context, R.layout.rotarowlayout, values);
            this.context = context;
            this.values = values;
          }

          @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.rotarowlayout, parent, false);
           TextView startTime = (TextView)rowView.findViewById(R.id.rowstarttime);
           TextView duration = (TextView)rowView.findViewById(R.id.rowduration);
           TextView status = (TextView)rowView.findViewById(R.id.rowstatus);
           TextView name = (TextView)rowView.findViewById(R.id.rowclientname);

           startTime.setText("Start Time: " + values[0]);
           duration.setText("Duration:" + values[1]);
           status.setText("Status:" + values[2]);
           name.setText("Client:" + values[3] + values[4]);



            return rowView;
          }
        }   
    }// end of GetRota
4

2 回答 2

2

通过从 BaseAdapter 扩展来编写自己的适配器,然后将值数组传递给适配器,而不是传递 ArrayList 本身。您可以使用位置值访问 getView 中的正确索引。这就是您的代码的样子

private class MySimpleArrayAdapter extends BaseAdapter {
      private final Context context;
      private final ArrayList array;

      public MySimpleArrayAdapter(Context context, ArrayList array) {
        this.context = context;
        this.array = array;
      }

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

    public Object getItem(int position) {
        return array.get(position);
    }

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

      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
        String[] values = array.get(position).toString();
        View rowView;

        if (convertView == null) { // if it's not recycled, initialize some
                                // attributes
            rowView = new View(context);
            // get layout from gridview_item.xml
            rowView = inflater.inflate(R.layout.rotarowlayout, parent, false);
        } else {
            rowView = (View) convertView;
        }


       TextView startTime = (TextView)rowView.findViewById(R.id.rowstarttime);
       TextView duration = (TextView)rowView.findViewById(R.id.rowduration);
       TextView status = (TextView)rowView.findViewById(R.id.rowstatus);
       TextView name = (TextView)rowView.findViewById(R.id.rowclientname);

       startTime.setText("Start Time: " + values[0]);
       duration.setText("Duration:" + values[1]);
       status.setText("Status:" + values[2]);
       name.setText("Client:" + values[3] + values[4]);



        return rowView;
      }
    } 
于 2012-09-20T16:20:24.980 回答
1

您需要继承 BaseAdapter 并使用它来对数据执行任何操作。 这是一篇关于创建自定义适配器的文章。

于 2012-09-20T15:47:05.697 回答