• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Json(LoadMore Items)

Baggio1990

Lurker
Dec 11, 2013
3
0
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
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones