Android 如何在Recyclerview中显示json数据?
如何在数据列表中添加json数据?现在recycleview显示数据来自getdata()方法Android 如何在Recyclerview中显示json数据?,android,json,android-recyclerview,Android,Json,Android Recyclerview,如何在数据列表中添加json数据?现在recycleview显示数据来自getdata()方法 public View onCreateView(布局、充气机、视图组容器、, Bundle savedInstanceState){ //为该碎片膨胀布局 视图布局=充气机。充气(R.layout.fragment\u one,容器,假); rv=(RecyclerView)布局。findViewById(R.id.listViewCountry); getJSON(JSON_URL); vivzA
public View onCreateView(布局、充气机、视图组容器、,
Bundle savedInstanceState){
//为该碎片膨胀布局
视图布局=充气机。充气(R.layout.fragment\u one,容器,假);
rv=(RecyclerView)布局。findViewById(R.id.listViewCountry);
getJSON(JSON_URL);
vivzAdapter=新的vivzAdapter(getActivity(),getData());
//vivzAdapter=新的vivzAdapter(getActivity(),getData());
rv.设置适配器(VivzaAdapter);
rv.setLayoutManager(新的LinearLayoutManager(getActivity());
/*lv=(ListView)layout.findViewById(R.id.listViewCountry);
adapter=newarrayadapter(getContext(),android.R.layout.simple\u list\u item\u 1,country);
低压设置适配器(适配器)*/
返回布局;
}
公共静态列表getData()
{
列表数据=新的ArrayList();
int[]icon={R.drawable.photo,R.drawable.photo,R.drawable.photo,R.drawable.photo,
R.drawable.photo,R.drawable.photo,R.drawable.photo,R.drawable.photo,
R.drawable.photo,R.drawable.photo,R.drawable.photo,R.drawable.photo,
R.drawable.photo,R.drawable.photo,R.drawable.photo,R.drawable.photo};
字符串[]标题={“嗨”、“嗨”、“嗨”、“嗨”、“嗨”、“嗨”、“嗨”、“嗨”、“嗨”、“嗨”、“嗨”、“嗨”、“11”、“12”、“13”、“14”、“15”、“16”};
对于(inti=0;i试试这个
更改get data函数以解析json数据
{
"obj":
[
{
"name": "name1",
"url": "http://www.someweb.com/path"
},
{
"name": "name1",
"url": "http://www.someweb.com/path"
},
{
"name": "name1",
"url": "http://www.someweb.com/path"
}
]
}
循环使用json创建对象(本例中的信息)并将其添加到列表中
public static List<Information> getData(JSONObject json)
{
List<Information> data = new ArrayList<>();
JSONArray ary = json.getJSONArray("obj");
for(int i =0; i<ary.length;i++)
{
Information current = new Information();
JSONObject j = leaders.JSONObject(i);
current.name = j.getString("name");
current.web = j.getString("url");
data.add(current);
}
return data;
}
public static List<Information> getData(JSONObject json)
{
List<Information> data = new ArrayList<>();
JSONArray ary = json.getJSONArray("obj");
for(int i =0; i<ary.length;i++)
{
Information current = new Information();
JSONObject j = leaders.JSONObject(i);
current.name = j.getString("name");
current.web = j.getString("url");
data.add(current);
}
return data;
}
@Override
protected void onPostExecute(JSONObject o) {
super.onPostExecute(o);
vivzAdapter = new VivzAdapter(getActivity(),getData(o));//create the adapter
rv.setAdapter(vivzAdapter); // set adapter for recycler view
rv.setLayoutManager(new LinearLayoutManager(yourActivity.this));// set layout manager for recycler view
}