如何从listview

时间:2016-04-18 12:13:58

标签: listview

我在自定义列表视图中从json获得10000个数据,但我想第一次只有100个值,在滚动listview之后再获得另外100个值。怎么做。

     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 使用for循环从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;
    }
  }

1 个答案:

答案 0 :(得分: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
        }
    [...]    
}