Java 在android中解析json到baseadapter的值?
我正在构建一个android应用程序,其中我正在将原始数组解析为listview的baseAdapter。 比如:Java 在android中解析json到baseadapter的值?,java,android,json,listview,baseadapter,Java,Android,Json,Listview,Baseadapter,我正在构建一个android应用程序,其中我正在将原始数组解析为listview的baseAdapter。 比如: 我取全局变量 初始列表视图 使用baseAdapter类 这只适用于静态数据。现在,我正在使用类似Json的方法解析该值 试试看{ jsonObj=新的JSONObject(结果); array=jsonObj.getJSONArray(“列表”); 对于(int i=0;i
试试看{
jsonObj=新的JSONObject(结果);
array=jsonObj.getJSONArray(“列表”);
对于(int i=0;i
现在,我需要解析这个Json值来代替baseAdapter的静态数据
如何执行此操作?您的
BaseAdapter
实现不正确。它应该维护某种类型的数据集合。相反,您存储的是单个项目,无论标题中有多少个字符,该项目都会重复显示。此外,您没有使用每个适配器都必须使用的ViewHolder
。我强烈建议订阅以学习Android。下面是他的一篇文章,解释了适配器和AdapterViews
。注意,它来自一个相当旧的版本
否则,您应该在谷歌上搜索更多信息,了解制作自己的BaseAdapter
的细节。这里有一个很好的例子
至于你的JSON数据。您需要存储和读取适配器本身的数据,或者将JSON数据转换为适配器支持的现有数据结构更可用的数据。考虑到您仍在努力学习如何构建一个简单的自定义适配器,我将暂缓创建您自己的JSON适配器。取而代之的是,使用一种能为你解决大部分问题的方法
ListView l1;
String[] t1={"video1","video2"};
String[] d1={"lesson1","lesson2"};
int[] i1 ={R.drawable.ic_launcher,R.drawable.ic_launcher};
l1=(ListView)findViewById(R.id.list);
l1.setAdapter(new dataListAdapter(t1,d1,i1));
class dataListAdapter extends BaseAdapter {
String[] Title, Detail;
int[] imge;
dataListAdapter() {
Title = null;
Detail = null;
imge=null;
}
public dataListAdapter(String[] text, String[] text1,int[] text3) {
Title = text;
Detail = text1;
imge = text3;
}
public int getCount() {
// TODO Auto-generated method stub
return Title.length;
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row;
row = inflater.inflate(R.layout.custom, parent, false);
TextView title, detail;
ImageView i1;
title = (TextView) row.findViewById(R.id.title);
detail = (TextView) row.findViewById(R.id.detail);
i1=(ImageView)row.findViewById(R.id.img);
title.setText(Title[position]);
detail.setText(Detail[position]);
i1.setImageResource(imge[position]);
return (row);
}
}
try {
jsonObj = new JSONObject(result);
array = jsonObj.getJSONArray("list");
for (int i = 0; i < array.length(); i++) {
JSONObject obj = array.getJSONObject(i);
profilePic= ""+obj.getString(TAG_PROFILEPIC);
firstName= ""+obj.getString(TAG_FIRSTNAME);
lastName= ""+obj.getString(TAB_LASTNAME);
// status= ""+obj.getInt("status");
HashMap<String, String> contact = new HashMap<String, String>();
contact.put(TAG_PROFILEPIC, profilePic);
contact.put(TAG_FIRSTNAME, firstName);
contact.put(TAB_LASTNAME, lastName);
// do code for adding these values to adapter.
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }