Reputation: 2088
I have a case where I load a set of 10 images via WebService and and on further scrolling, I call upon the second WebService which load the next 10 images. I am able to load all the images from WebService but I am doing something silly that removes the first 10 images and re-assigns it with the next 10 images while calling the 2nd web service. I have tried notifyDataSetChanged() but it has no effect. The code is as follows :
CODE :
MainActivty :
new WebServicesClass().generateSampleData(); -->1st WebService
mGridView.setOnScrollListener(this);
mGridView.setOnItemClickListener(this);
@Override
public void onScroll(final AbsListView view, final int firstVisibleItem, final int visibleItemCount, final int totalItemCount) {
Log.d(TAG, "onScroll firstVisibleItem:" + firstVisibleItem +
" visibleItemCount:" + visibleItemCount +
" totalItemCount:" + totalItemCount);
// our handling
if (!mHasRequestedMore) {
System.out.println("Inside the requested more");
int lastInScreen = firstVisibleItem + visibleItemCount;
if (lastInScreen >= totalItemCount) {
Log.d(TAG, "onScroll lastInScreen - so load more");
mHasRequestedMore = true;
new WebServicesClass().onLoadMoreItems(); --> 2nd WebServiceCall
mHasRequestedMore = false;
}
}
}
WebServicesClass :
1st WebService :
onDoInBackGround :
@Override
protected String doInBackground(Void... urls) {
// TODO Auto-generated method stub
try {
HttpClient httpclient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet(
"http://demo.bsetec.com/fancyclone/android/users/products?user_id=2&limit=0");
HttpResponse response = httpclient.execute(httpGet,
localContext);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, "iso-8859-1"), 8);
System.out.println("Buffered Reader " + reader.toString());
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag",
"Error converting sms response result " + e.toString());
}
System.out.println("Result: " + result);
try {
limit_for = 0;
OpenHttpConnection(image_url);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
OpenHttpConnection :
public InputStream OpenHttpConnection(String image_url)
throws IOException {
int response = -1;
JSONObject jsonresponse;
String first_image = null;
try {
jsonresponse = new JSONObject(result);
Log.i("Inside OpenHttp", result);
if (result != null) {
try {
JSONObject status = jsonresponse
.getJSONObject("status");
// looping through All Contacts
if (status != null) {
products = status.getJSONArray("products");
dreamt_product_list = status
.getJSONArray("dreamit_products");
System.out.println("Dreamt Products list are "
+ dreamt_product_list.getJSONObject(0)
.names());
System.out.println("The value of string limit is "
+ limit);
System.out.println("The limit_for value is "
+ limit_for);
for (int p = limit_for; p < load_limit; p++) {
System.out.println("Products names: "
+ products.getJSONObject(p).names());
System.out.println("Item Name "
+ products.getJSONObject(p).getString(
"name"));
product_name = products.getJSONObject(p)
.getString("name").toString();
cost = products.getJSONObject(p).getString("saleprice").toString();
product_id = products.getJSONObject(p)
.getString("id");
username = products.getJSONObject(p).getString("username").toString();
System.out.println("Getstring: "
+ products.getJSONObject(p).getString(
"images"));
String images_list = products.getJSONObject(p)
.getString("images");
images_list = images_list.replaceAll("\"", "");
String regex = images_list.replaceAll(
"\\[|\\]", "");
System.out
.println("The images without bracket are "
+ regex);
for (String comma_token : regex.split(",")) {
for (int i = 0; i < 1; i++) {
System.out
.println("First Image name is "
+ comma_token);
first_image = comma_token;
System.out
.println("Image in first image is "
+ first_image);
}
break;
}
System.out.println("I am here");
image_url = "http://demo.bsetec.com/fancyclone/uploads/approved_items/"
+ first_image;
URL url = new URL(image_url);
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection))
throw new IOException(
"Not an HTTP connection");
try {
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
compressed_image = image;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPurgeable = true;
options.inJustDecodeBounds = true;
options.inSampleSize = 1;
options.inJustDecodeBounds = false;
image = BitmapFactory.decodeStream(in,
null, options);
// in.reset();
}
} catch (Exception ex) {
throw new IOException("Error connecting");
}
item = new RowItem(image, product_name, cost,
product_id, dream_status,username);
rowItems.add(item);
}
System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%" + rowItems.size());
}
} catch (JSONException e) {
// TODO Auto-generated catch block
System.out
.println("Caught Exception in the 2nd try block");
e.printStackTrace();
}
}
} catch (Exception e) {
System.out.println("Caught exception");
}
System.out.println("Ending OpenHttpConnection");
return in;
}
onPostExecuteMethod :
mAdapter = newDynamicHeightAdapter(MainActivity.getContext(),R.layout.repeat_items,rowItems);
System.out.println("ADapter size: "+mAdapter.getCount());
MainActivity.mGridView.setAdapter(mAdapter);
2nd WebService :
onDoInBackGround :
The code is SAME as the first doInBackGround().OpenHttpConnection is also the same.
onPostExecuteMethod :
mAdapter.notifyDataSetChanged(); -->Not working
When I call the WebService initially, it retrieves the first 10 images as it is supposed to do. But when the 2nd WebService is called at onScroll, then it REPLACES the initial 10 images with the 10 images obtained FROM 2nd WEBSERVICE. All I want to know is, how do I UPDATE it WITHOUT REPLACING ? Any help will be much appreciated guys. I am happy to help you with any queries.
UPDATE :
rowItems.addAll(rowItems);
Is the above code valid ?
NOTE : I am using a external library named StaggeredGridView.
Upvotes: 0
Views: 172
Reputation: 4701
You pass a collection
of objects to the adapter, in your case it is rowItems
.
OnScroll
you hit a Web service and receives and parse the contents. These new content should be put in separate new arraylist. Say it is newRowContents
.
Now, you need to add newRowContent to original row content.
rowItems.addAll(newRowContent);
Your backing datasource is updated now, and your listview needs to be refresh now.
mAdapter.notifyDataSetChanged();
Upvotes: 1
Reputation: 2002
First get the adapter.
YourAdapter adapter=(YourAdapter) mGridView.getAdapter();
adapter.addAll(rowItems);
adapter.notifyDataSetChanged();
Hope this works fine
Upvotes: 1