Android 如何从包含集合的json中获取值<;数据>;在pojo类中,并设置为listview适配器
从2天开始,我面临一个问题:如何在adapter中将Android 如何从包含集合的json中获取值<;数据>;在pojo类中,并设置为listview适配器,android,listview,android-arrayadapter,Android,Listview,Android Arrayadapter,从2天开始,我面临一个问题:如何在adapter中将collectionbean类设置为文本视图的值,首先让类为aaa public class aaa { String name=""; public String getname() { return name; } public String setname(String name) { this.name=name; } } 您可以这样将此类发
collection
bean类设置为文本视图的值,首先让类为aaa
public class aaa
{
String name="";
public String getname()
{
return name;
}
public String setname(String name)
{
this.name=name;
}
}
您可以这样将此类发送到适配器:
YourAdapter youradapter=new YourAdapter(context,Collection<aaa>);
yourlistview.setAdapter(youradapter);
YourAdapter YourAdapter=newyourAdapter(上下文、集合);
设置适配器(yourlistview.setAdapter);
适配器类可能如下所示:
public class YourAdapter extends BaseAdapter {
Context context;
Collection<aaa> collection;
public TestAdapter(Context context,Collection<aaa> collection)
{
this.context=context;
this.collection=collection;
}
@Override
public int getCount() {
return collection.size();
}
@Override
public Object getItem(int position) {
return collection.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView= LayoutInflater.from(context).inflate(R.layout.row_item,parent,false);
//view binding code
//and now if you have a textview to set name from pojo class you can do is:
textview.setText(collection.get(position).getname);
return convertView;
}
}
公共类YourAdapter扩展BaseAdapter{
语境;
收藏;
公共测试适配器(上下文、集合)
{
this.context=context;
this.collection=collection;
}
@凌驾
public int getCount(){
返回collection.size();
}
@凌驾
公共对象getItem(int位置){
返回集合。获取(位置);
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
convertView=LayoutFlater.from(上下文)。充气(R.layout.row_项,父项,false);
//查看绑定代码
//现在,如果您有一个textview来设置pojo类的名称,您可以做的是:
setText(collection.get(position.getname);
返回视图;
}
}
请提供一些代码来解释您的问题Private Collection a1=new ArrayList();它在pojo类中,对于aaa pojo,如何在适配器中获取值仍然没有帮助。请通过提供pojo和适配器的完整代码来更新您的问题。发布一些相关代码。在pojo类中,我使用的是private Collection address=new ArrayList();如何调用该地址并设置为adpater并在listview中显示,先生。。我试过了,但它没有让我知道如何在listview json中将值设置为collectionYourAdapter youradapter=new youradapter(上下文,集合);设置适配器(yourlistview.setAdapter);这里如何将json数据设置为集合您需要解析json数据,然后将解析后的数据添加到集合中。如果您将提供json数据,我将向您展示如何解析它并将其放入集合中!