如何使arrayAdapter显示多个数组的内容?

时间:2012-09-20 15:43:17

标签: android multidimensional-array android-listview android-arrayadapter

我有一个multidimentional arrayList.eg

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

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

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

在下面的片段数组中是多维数组。我已将硬编码位置(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

2 个答案:

答案 0 :(得分: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;
      }
    } 

答案 1 :(得分:1)

您需要将BaseAdapter子类化并使用它来对数据执行任何操作。 Here is an article有关创建自定义适配器的信息。