gpt4 book ai didi

java - ListView中的多个setAdapter

转载 作者:太空宇宙 更新时间:2023-11-04 13:21:32 25 4
gpt4 key购买 nike

这是有问题的代码:

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

您不能为一个 ListView 设置多个适配器。

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

  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>

这是从 Activity 中调用的方法:
适配器 = new MyCustomAdapter(CarrierSummaryActivity.this,nomi,R.layout.item); mainListView.setAdapter(适配器);

关于java - ListView中的多个setAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33012338/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com