2016-04-18 3 views
1

Я получаю 10000 данных из json в пользовательском listview, но я хочу только 100 значений в первый раз и после прокрутки listview, а затем получить еще 100 значений. как это сделать.Как получить 100, то 100 значений из json в listview

 class GetData extends AsyncTask<String, Void, String> 
    { 

     @Override 
     protected void onPreExecute() 
     { 

     } 

     @Override 
     protected String doInBackground(String... params) 
     { 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost(url); 

      InputStream inputStream = null; 
      String result = null; 
      try 
      { 
       HttpResponse response = httpclient.execute(httppost); 
       HttpEntity entity = response.getEntity(); 
       inputStream = entity.getContent(); 
       // json is UTF-8 by default 
       BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8); 
       StringBuilder sb = new StringBuilder(); 

       String line = null; 
       while ((line = reader.readLine()) != null) 
       { 
        sb.append(line + "\n"); 
       } 
       result = sb.toString(); 
      } 
      catch (Exception e) { 
      } 
      finally 
      { 
       try { 
        if (inputStream != null) 
         inputStream.close(); 
       } catch (Exception squish) { 
       } 
      } 
      return result; 
     } 

     @Override 
     protected void onPostExecute(String result) 
     { 
      myJSON = result; 
      getJson(); 

     } 
    } 

прибудет JSON из URL получать данные из jsonarray используя цикл

public void getJson() 
    { 
    try 
    { 
     JSONArray json = new JSONArray(myJSON); 
     Log.i("json", "" + json); 

     for (int i = 0;i < json.length(); i++) 
     { 
      JSONObject jsonObject = json.getJSONObject(i); 

      final String vehicle_name =      
        jsonObject.getString(TAG_VehicleName); 
      Log.i("vehicle_name", vehicle_name); 
      final String city = jsonObject.getString(TAG_City); 
      Log.i("city", city); 
      final String user_type = jsonObject.getString(TAG_UserType); 
      Log.i("user_type", user_type); 
      final String waitPrHr = jsonObject.getString(TAG_WaitPrHr); 
      Log.i("waitPrHr", waitPrHr); 

      HashMap<String, String> map = new HashMap<String, String>(); 
      map.put(TAG_VehicleName, vehicle_name); 
      map.put(TAG_City, city); 
      map.put(TAG_UserType, user_type); 
      map.put(TAG_WaitPrHr, waitPrHr); 
      Log.i("map", "" + map); 
      arrayList.add(map); 
      Log.i("TransferList", "" + arrayList); 

      final ListAdapter adapter1 = new CustomAdapter(getActivity(), 
      arrayList, R.layout.customer_transfer_list, new String[]{}, new 
        int[]{}); 
      transfer_list.setAdapter(adapter1); 
      Log.i("transfer_list", "" + transfer_list); 
      Log.e("pass 1", "connection success "); 
     } 
    } 
    catch(Exception e) 
    { 
     Log.e("Fail 1", e.toString()); 

    } 
} 

пользовательского адаптер для надувания макета в ListView

class CustomAdapter extends SimpleAdapter 
    { 
    String bookId; 
    private Context mContext; 
    public LayoutInflater inflater = null; 

    public CustomAdapter(Context context, List<? extends Map<String, ?>> 
    data, int resource, String[] from, int[] to) { 
     super(context, data, resource, from, to); 
     mContext = context; 
     inflater = (LayoutInflater) 
     mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View vi = convertView; 
     if (convertView == null) { 
      vi = inflater.inflate(R.layout.customer_transfer_list, null); 
     } 
     HashMap<String, Object> data = (HashMap<String, Object>) 
     getItem(position); 

     final int pos = position; 

     TextView txtVehiclename = (TextView) 
       vi.findViewById(R.id.cstm_transfer_vehiclename); 
     TextView txtcity = (TextView) 
       vi.findViewById(R.id.cstm_transfer_city); 
     TextView txtusertype = (TextView) 
       vi.findViewById(R.id.cstm_transfer_usertype); 
     TextView txtrate = (TextView) 
       vi.findViewById(R.id.cstm_transfer_rate); 

     String VehicleName = (String) data.get(TAG_VehicleName); 
     txtVehiclename.setText(VehicleName); 

     String City = (String) data.get(TAG_City); 
     txtcity.setText(City); 

     String UserType = (String) data.get(TAG_UserType); 
     txtusertype.setText(UserType); 

     String WaitPrHr = (String) data.get(TAG_WaitPrHr); 
     txtrate.setText(WaitPrHr); 

     return vi; 
    } 
    } 

ответ

0

Я думаю, что изменение getJson принимать аргументы могли бы работать.

public void getJson(int from, int to) 
{ 
    [...] 
     if(to > json.length() || i < 0 || i > to){ 
      throw new InvalidArgumentException(); 
     } 
     for (int i = from;i < to; i++) 
     { 
      //Get the object stuff 
     } 
    [...]  
} 
Смежные вопросы