2015-11-14 115 views
-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); 
} 

回答

0

试试这个 更改获取数据函数解析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 
    } 
相关问题