这是有问题的代码:

//declarations:
private ArrayAdapter<String> listAdapter;
private ArrayAdapter<String> listAdapter2;
String [] nomi=null;
String[] famiglia=null;
private ListView mainListView;

// other code bla bla bla...

mainListView = (ListView) findViewById(R.id.listView);

// other code bla bla bla...


nomi = new String[res.size()];
for (int i = 0; i < res.size(); i++) {
    nomi[i] = res.get(i).getNomignolo();
}


famiglia= new String[res.size()];
for(int i=0; i<res.size();i++){
    famiglia[i] = res.get(i).getFamiglia();
}


ArrayList<String> listaNomi = new ArrayList<String>();
listaNomi.addAll(Arrays.asList(nomi));
ArrayList<String> listaFamiglie = new ArrayList<String>();
listaFamiglie.addAll(Arrays.asList(famiglia));





listAdapter = new ArrayAdapter<String>(HomeActivity.this, R.layout.row, R.id.button3, listaNomi);
listAdapter2 = new ArrayAdapter<String>(HomeActivity.this, R.layout.row, R.id.button6, listaFamiglie);
mainListView.setAdapter(listAdapter);
mainListView.setAdapter(listAdapter2);


它有效,但仅部分起作用,因为当我启动应用程序时,我只能找到第二个setAdapter方法的结果。我如何还能获得所有setAdapter方法的结果?谢谢。

最佳答案

尝试在第一个列表中添加第二个列表的对象

   nomi = new String[res.size()];
   for (int i = 0; i < res.size(); i++) {
     nomi[i] = res.get(i).getNomignolo() + " " +res.get(i).getFamiglia();
    }

  //list of object with name and family


创建适配器

listAdapter = new ArrayAdapter<String>(HomeActivity.this, R.layout.row, R.id.button3, listaNomi);
 mainListView.setAdapter(listAdapter);


您不能为一个列表视图设置多个适配器。

如何创建自定义阵列适配器:

  public class CustomAdapter extends ArrayAdapter<Person>{
  private final Activity context;

  private ArrayList<Person> Items;
  public CustomAdapter (Activity context, int layout,ArrayList<Person> persons) {
  super(context, layout, carriers);
 // TODO Auto-generated constructor stub

 this.context = context;
 this.Items = persons;

 }

 static class ViewHolder {
  public Button name;
  public Button family;


   }

@Override
 public View getView(int position, View convertView, ViewGroup parent) {
  final ViewHolder holder;
  // Recycle existing view if passed as parameter
  // This will save memory and time on Android
  // This only works if the base layout for all classes are the same
  View rowView = convertView;
  if (rowView == null) {
    LayoutInflater inflater = context.getLayoutInflater();
    rowView = inflater.inflate(R.layout.item, null, true);

    holder = new ViewHolder();
    holder.name= (TextView) rowView.findViewById(R.id.name);
     holder.family= (TextView) rowView.findViewById(R.id.family);
    rowView.setTag(holder);
   } else {
    holder = (ViewHolder) rowView.getTag();
   }

   holder.name.setText(Items.get(position).getName());
  holder.family.setText(Items.get(position).getFamily());


  return rowView;

 }

}


并创建PErson对象:

public class PErson{ public String name; public String family;
                   public Person();
                  public String getName(){ return name;}
                  public String getFamily(){return family;}
                }


这是项目xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<Button
    android:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="wkjdhk"
    android:textColor="@color/green"
    android:textSize="12sp" />

<Button
    android:id="@+id/family"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:text="wkjdhk"
    android:textColor="@color/green"
    android:textSize="12sp" />
  </RelativeLayout>


这是从活动中调用的方式:
        adapter = new MyCustomAdapter(CarrierSummaryActivity.this,nomi,R.layout.item);
        mainListView.setAdapter(adapter);

07-24 19:49