0

我有一个应用程序,我在其中显示我从网络服务获得的记录。该记录有 5 个字段,startTime、duration、status 和 clientname。(clientname 实际上由 first 和 last 2 个字段组成)。我可以将 arrayList 中的字段放入 String 数组,然后将该数组传递给适配器。字段显示在 ListView 中。到目前为止一切都很好,除了相同的记录在 ListView 中显示了 5 次。为什么是这样?我将数组传递给适配器一次。

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");





        for(int i = 0; i < array.size(); i++){

             Log.e(TAG, "array pos " + i + " = " +  array.get(i));

        }

        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

. [更新]

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();
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            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[position][0]);
           duration.setText("Duration:" + values[position][1]);
           status.setText("Status:" + values[position][2]);
           name.setText("Client:" + values[position][3] + values[position][4]);




            return rowView;
          }
        } 

[更新hitesh]

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

        public MySimpleArrayAdapter(Context context,ArrayList<String[]> list) 
        {      

        super(context, R.layout.rotarowlayout);
         Log.e(TAG, "inside adapter constructor");
                this.context = context;           
                this.list= list;
              }

              @Override
              public View getView(int position, View convertView, ViewGroup parent) {
                  Log.e(TAG, "inside getView");
                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);

               String[] values = list.get(position);

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



                return rowView;

              }


              @Override
              public int getCount() {
                  return this.list.size();
              }

    }// end of adapter class
4

2 回答 2

1

您正在使用 split() 将每条记录转换为字符串数组。

String[] itemsInRecord = record.split(",");

如果这是一条记录,那么记录数组应该是二维的。但是您的记录数组是一维的。

String[] recordItem = new String[5];

这将传递给您的适配器,该适配器可能正在为values. 对于每个位置,您的getView()方法是每次读取相同的五个值。

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

因此,您的数据需要两个维度:列表项的数量和每个项目的字段。您当前的代码使用与两者相同的维度,这就是您看到同一记录的五个副本的原因。

String[][] recordItem = new String[numRecords][5];
/* ... */
startTime.setText("Start Time: " + values[position][0]);
duration.setText("Duration:" + values[position][1]);
status.setText("Status:" + values[position][2]);
name.setText("Client:" + values[position][3] + values[position][4]);

由于您已经拥有它,我建议使用ArrayList原始记录并将其传递给您的适配器。如果您的记录以您定义的某个类的形式出现,请使用它!ArrayList支持泛型类型,你可以让你的适配器期望它。您不必扩展ArrayAdapter;您可以扩展并提供、、 等BaseAdapter的必要实现getCount(),使用.getItem(position)getItemId(position)ArrayList

于 2012-09-20T15:40:36.507 回答
-2

实际上你的代码是错误的。使用 ArrayList 字符串数组列表

for(int i=0;i<array.length;i++)
{
String record = array.get(i).toString();
String[] itemsInRecord = record.split(",");
list.add(record);      }

还可以自定义您的适配器。MySimpleArrayAdapter arrayAdapter = new MySimpleArrayAdapter(this, list); listView.setAdapter(arrayAdapter);

private class MySimpleArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final Arraylist< String[]> list;
public MySimpleArrayAdapter(Context context,Arraylist< String[]> list) 
{         
super(context, R.layout.rotarowlayout,list);
        this.context = context;           this.list= list;
      }

      @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);
String[] values= list.get(position);
       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;}}

在你的活动课上,替换

ArrayList array =  (ArrayList) bundle.get("rotaArray");
    for(int i = 0; i < array.size(); i++){

         Log.e(TAG, "array pos " + i + " = " +  array.get(i));

    }
    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);

 ArrayList<String[]> list = new ArrayList<String[]>();

 ArrayList array =  (ArrayList) bundle.get("rotaArray");

    for(int i = 0; i < array.size(); i++){

        String record = array.get(i).toString();
 String[] itemsInRecord = record.split(",");
    list.add(itemsInRecord);  

    }

 MySimpleArrayAdapter arrayAdapter = new MySimpleArrayAdapter(this, list);  
    listView.setAdapter(arrayAdapter);
于 2012-09-20T14:48:13.417 回答