1
jsonからカスタムリストビューで10000のデータを取得していますが、最初に100個の値を取得し、リストビューをスクロールした後に100個の値を取得します。これを行う方法。listviewのjsonから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();
}
}
私は引数を取るようにgetJsonを変更するとうまくいくかもしれないと思うループ
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());
}
}
リストビュー
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;
}
}