-1
我如何添加JSON數據在我的datalist?現在recycleview顯示數據來自getdata()方法。如何在Recyclerview中顯示json數據?
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);
}