如何在我的datalist中添加json数据?现在,recycleview显示数据来自getdata()方法。
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View layout = inflater.inflate(R.layout.fragment_one, container, false);
rv = (RecyclerView)layout.findViewById(R.id.listViewCountry);
getJSON(JSON_URL);
vivzAdapter = new VivzAdapter(getActivity(),getData());
// vivzAdapter = new VivzAdapter(getActivity(),getData());
rv.setAdapter(vivzAdapter);
rv.setLayoutManager(new LinearLayoutManager(getActivity()));
/*lv = (ListView) layout.findViewById(R.id.listViewCountry);
adapter = new ArrayAdapter<String>(getContext(),android.R.layout.simple_list_item_1,country);
lv.setAdapter(adapter);*/
return layout;
}
public static List<Information> getData()
{
List<Information> data = new 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};
String[] title = {"Hi","Hi","Hi","Hi","Hi","Hi","Hi","Hi","Hi","Hi","11","12","13","14","15","16"};
for(int i =0; i<title.length;i++)
{
Information current = new Information();
current.iconId = icon[i];
current.title = title[i];
data.add(current);
}
return data;
}
//json class
private void getJSON(String url) {
class GetJSON extends AsyncTask<String, Void, String> {
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
//loading = ProgressDialog.show(getActivity(), "Please Wait...",null,true,true);
}
@Override
protected String doInBackground(String... params) {
String uri = params[0];
BufferedReader bufferedReader = null;
try {
URL url = new URL(uri);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
StringBuilder sb = new StringBuilder();
bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String json;
while((json = bufferedReader.readLine())!= null){
sb.append(json+"\n");
}
return sb.toString().trim();
}catch(Exception e){
return null;
}
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//loading.dismiss();
//vivzAdapter.notifyDataSetChanged();
//textViewJSON.setText(s);
json_string = s;
Toast.makeText(getActivity(), s, Toast.LENGTH_LONG).show();
}
}
GetJSON gj = new GetJSON();
gj.execute(url);
}
答案 0 :(得分:0)
试试这个 更改get数据函数以解析json数据。例如,如果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;
}
在onPostExecute
创建适配器,设置适配器并为回收站视图设置布局管理器
@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
}