2015-11-14 165 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 
    } 
相關問題