我有一種情況,我通過WebService加載一組10幅圖像,並且在進一步滾動時,我調用加載下10幅圖像的第二個WebService。我能夠從WebService加載所有圖像,但是我正在做一些愚蠢的事情,即刪除前10張圖像,並在調用第二個Web服務時將其重新分配給接下來的10張圖像。我試過notifyDataSetChanged(),但它沒有效果。代碼如下:無法在Android中設置notifyDataSetChanged
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:
第一web服務:
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);
第二WebService的:
onDoInBackGround:
該代碼是SAME作爲第一doInBackGround()OpenHttpConnection也是相同的。
onPostExecuteMethod:
mAdapter.notifyDataSetChanged(); -->Not working
當我最初調用WebService的,它,因爲它是應該做檢索第10張圖像。但是當在onScroll上調用第二個WebService時,它會從第二個WEBSERVICE中獲取最初的10張圖像,其中包含012個圖像,其中爲。所有我想知道的是,我如何更新它沒有更換?任何幫助將不勝感激球員。我很樂意爲您提供任何疑問。
UPDATE:
rowItems.addAll(rowItems);
是上面的代碼合法嗎?
注意:我正在使用名爲StaggeredGridView的外部庫。
後在potExecute和doInBackground – Triode
使用rowItems.addAll(rowItemReceivedFromWS2)的完整代碼; 這會將從WS2收到的新項目添加到'rowItem'中,然後調用'notifyDataSetChanged()',它將告訴適配器刷新列表視圖。 – guptakvgaurav
@GauravGupta,你的意思是從WebService 1獲得的? – San