Android 加载转换列表<;字符串>;进入listview
我在android应用程序中有一个字符串类型的填充列表(ServicesList)、自定义类(ServiceRecord.java)、自定义适配器()和自定义列表视图(lvS) 我已经能够通过以下代码将列表变量转换为自定义列表:Android 加载转换列表<;字符串>;进入listview,android,list,listview,Android,List,Listview,我在android应用程序中有一个字符串类型的填充列表(ServicesList)、自定义类(ServiceRecord.java)、自定义适配器()和自定义列表视图(lvS) 我已经能够通过以下代码将列表变量转换为自定义列表: List<ServiceRecord> objectList = (List)servicesList; ServicesCustomAdapter adapter = new ServicesCustomAdapter(this, obje
List<ServiceRecord> objectList = (List)servicesList;
ServicesCustomAdapter adapter = new ServicesCustomAdapter(this, objectList);
lvS.setAdapter(adapter);
和我的自定义适配器:
public class ServicesCustomAdapter extends ArrayAdapter<ServiceRecord>{
private List<ServiceRecord> items;
public ServicesCustomAdapter(Context context, List<ServiceRecord> items) {
super(context, R.layout.list_service, items);
this.items = items;
}
@Override
public int getCount() {
return items.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null) {
LayoutInflater li = LayoutInflater.from(getContext());
v = li.inflate(R.layout.list_service, null);
}
ServiceRecord bbs = items.get(position);
if(bbs != null) {
//TextView un = (TextView)v.findViewById(R.id.list);
TextView tvservice = (TextView)v.findViewById(R.id.tvservice);
if(tvservice != null) tvservice.setText(bbs.getService());
}
return v;
}
}
public类ServicesCustomAdapter扩展了ArrayAdapter{
私人清单项目;
公共服务适配器(上下文、列表项){
super(上下文、R.layout.list\u服务、项目);
这个项目=项目;
}
@凌驾
public int getCount(){
返回items.size();
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图v=转换视图;
如果(v==null){
LayoutInflater li=LayoutInflater.from(getContext());
v=li.充气(R.layout.list\u服务,空);
}
ServiceRecord bbs=items.get(位置);
如果(bbs!=null){
//TextView un=(TextView)v.findViewById(R.id.list);
TextView-tvservice=(TextView)v.findViewById(R.id.tvservice);
if(tvservice!=null)tvservice.setText(bbs.getService());
}
返回v;
}
}
您必须创建一个包含服务记录
类对象的列表。请尝试下面的代码
List<ServiceRecord> objectList = new ArrayList<>();
for (String item: servicesList){
ServiceRecord sr=new ServiceRecord();
sr.setService(item);
objectList.add(sr);
}
ServicesCustomAdapter adapter = new ServicesCustomAdapter(this, objectList);
lvS.setAdapter(adapter);
List objectList=new ArrayList();
for(字符串项:servicesList){
ServiceRecord sr=新的ServiceRecord();
高级设置服务(项目);
objectList.add(sr);
}
ServicesCustomAdapter=新ServicesCustomAdapter(此为objectList);
lvS.setAdapter(适配器);
PS:而您的ServiceRecord
类只有一个属性。因此,为什么不直接使用列表
来完成列表适配器呢。您必须创建一个列表
,其中包含服务记录
类对象。请尝试下面的代码
List<ServiceRecord> objectList = new ArrayList<>();
for (String item: servicesList){
ServiceRecord sr=new ServiceRecord();
sr.setService(item);
objectList.add(sr);
}
ServicesCustomAdapter adapter = new ServicesCustomAdapter(this, objectList);
lvS.setAdapter(adapter);
List objectList=new ArrayList();
for(字符串项:servicesList){
ServiceRecord sr=新的ServiceRecord();
高级设置服务(项目);
objectList.add(sr);
}
ServicesCustomAdapter=新ServicesCustomAdapter(此为objectList);
lvS.setAdapter(适配器);
PS:而您的ServiceRecord
类只有一个属性。那么,为什么不使用列表来完成列表适配器呢。错误是不言自明的。向下投射期间对象类型不匹配。如何解决此问题?请告诉我怎么做。错误是不言自明的。向下投射期间对象类型不匹配。如何解决此问题?请告诉我如何。因此,objectList
=0的大小?仅供参考,我将在ServiceRecord类中添加更多属性。这就是为什么它需要这种方法。非常感谢你,我的朋友是的,我就是这么想的。Thx.因此,objectList
=0?仅供参考,我将在ServiceRecord类中添加更多属性。这就是为什么它需要这种方法。非常感谢你,我的朋友是的,我就是这么想的。谢谢。