1. Are you ready for the Galaxy S20? Here is everything we know so far!

Json(LoadMore Items)

Discussion in 'Android Development' started by Baggio1990, Dec 12, 2013.

  1. Baggio1990

    Baggio1990 Lurker
    Thread Starter

    Hello, I worked JSON. I have two classes (private class LoadDataToServer extends AsyncTask and class loadMoreListView extends AsyncTask)
    in the first grade, I parsing JSON and show item in the ListView and the second class, I'm trying to load multiple items.
    my problem is when I call the new loadMoreListView (). execute (); .. on listView.setOnScrollListener there is an error.
    my problem is adapter :(
    this is my code
    [HIGH]private class loadMoreListView extends
    AsyncTask<Void, Integer, ArrayList<HashMap<String, String>>> {

    @Override
    protected void onPreExecute() {
    pd.show();
    }

    @Override
    protected ArrayList<HashMap<String, String>> doInBackground(
    Void... params) {

    jsonparser = new JSONParser();

    URL = "http://bri.ge/api/getList.aspx?count=2&time=" + dateTime;
    jsonarray = new JSONArray();

    JSONObject jsonobject = jsonparser.getJSONfromURL(URL);

    try {

    jsonarray = jsonobject.getJSONArray("data");

    for (int i = 0; i < jsonarray.length(); i++) {

    jsonobject = jsonarray.getJSONObject(i);

    HashMap<String, String> map = new HashMap<String, String>();

    map.put("journal", jsonobject.getString(KEY_journal));
    map.put("image", jsonobject.getString(KEY_image));
    map.put("title", jsonobject.getString(KEY_title));
    map.put("description",
    jsonobject.getString(KEY_description));
    map.put("JournalID", KEY_JournalID);

    map.put("pubDate", jsonobject.getString(KEY_pubDate));

    Content cont = new Content(jsonobject.getString("journal"),
    jsonobject.getString("image"),
    jsonobject.getString("title"),
    jsonobject.getString("pubDate"),
    jsonobject.getString("description"),
    jsonobject.getInt("JournalID"));

    contents.add(cont);

    itemList.add(map);

    }
    // adapter.notifyDataSetChanged();
    } catch (JSONException e) {
    Log.e("Error", e.getMessage());
    e.printStackTrace();

    }
    dateTime = itemList.get(itemList.size() - 1).get(KEY_pubDate);
    return itemList;
    }

    @Override
    protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
    super.onPostExecute(result);
    if (pd.isShowing()) {
    pd.dismiss();
    try {
    // itemList.clear();

    int currentPosition = list.getFirstVisiblePosition();

    // Appending new data to menuItems ArrayList

    adapter = new LazyAdapter(MainActivity.this, itemList);
    adapter.notifyDataSetChanged();
    list.setAdapter(adapter);
    // list.add

    // Setting new scroll position
    list.setSelectionFromTop(currentPosition + 1, 0);

    } catch (NullPointerException e) {
    e.printStackTrace();
    }

    }
    }
    }[/HIGH]
    [HIGH]private class LoadDataToServer extends
    AsyncTask<Void, Integer, ArrayList<HashMap<String, String>>> {

    @Override
    protected void onPreExecute() {
    super.onPreExecute();
    pd.show();

    }

    @Override
    protected ArrayList<HashMap<String, String>> doInBackground(
    Void... params) {

    jsonparser = new JSONParser();

    @SuppressWarnings("static-access")
    JSONObject jsonobject = jsonparser.getJSONfromURL(URL);
    try {

    jsonarray = jsonobject.getJSONArray("data");

    for (int i = 0; i < jsonarray.length(); i++) {

    jsonobject = jsonarray.getJSONObject(i);

    HashMap<String, String> map = new HashMap<String, String>();

    map.put("journal", jsonobject.getString(KEY_journal));
    map.put("image", jsonobject.getString(KEY_image));
    map.put("title", jsonobject.getString(KEY_title));
    map.put("description",
    jsonobject.getString(KEY_description));
    map.put("JournalID", KEY_JournalID);
    map.put("pubDate", jsonobject.getString(KEY_pubDate));

    // contents = new ArrayList<Content>();

    Content cont = new Content(jsonobject.getString("journal"),
    jsonobject.getString("image"),
    jsonobject.getString("title"),
    jsonobject.getString("pubDate"),
    jsonobject.getString("description"),
    jsonobject.getInt("JournalID"));

    contents.add(cont);

    itemList.add(map);
    }
    } catch (JSONException e) {
    Log.e("Error", e.getMessage());
    e.printStackTrace();
    }
    dateTime = itemList.get(itemList.size() - 1).get(KEY_pubDate);
    return itemList;

    }

    @Override
    protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
    super.onPostExecute(result);

    pd.dismiss();

    // itemList.clear();

    // Appending new data to menuItems ArrayList

    adapter = new LazyAdapter(MainActivity.this, itemList);
    adapter.notifyDataSetChanged();
    list.setAdapter(adapter);
    // list.add

    // Setting new scroll position

    }

    }
    [/HIGH]
    [HIGH]list.setOnScrollListener(new OnScrollListener() {

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
    if (scrollState == 0) {
    // new loadMoreListView().execute();

    }

    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
    int visibleItemCount, int totalItemCount) {
    final int lastItem = firstVisibleItem + visibleItemCount;
    if (lastItem == totalItemCount) {
    new loadMoreListView().execute();
    }

    }
    });[/HIGH]
    this is a my LogCat error
    http://b.pix.ge:81/z/toct2.jpg
     



    1. Download the Forums for Android™ app!


      Download

       
  2. Petrah

    Petrah Psychotic Female

    Hi there!

    I've moved your thread to Application Development where other programmers can better assist you. ;)
     
  3. Baggio1990

    Baggio1990 Lurker
    Thread Starter

    thanks . i'm waiting... :(
     
Loading...

Share This Page

Loading...