我的Android应用程序是关于显示图像。可能有成千上万的图像。一次加载那么多的图像可能会崩溃app.So我一次加载前100个图像,然后在完成前100个图像后,它将调用web服务并再次加载下100个图像。每次响应来自服务器时我都在洗牌。但是在洗牌之后,屏幕上的图像没有更新。这意味着当我点击单张图片打开然后打开不同于我们看到的图像时网格视图单行。我得到了问题,因为我在收到服务器的响应后洗了整个数组。但是如何管理这个问题?请帮帮我。谢谢你提前。 这是我的Tablet_Photos.java
/************************************ Call webservice For loading all Photos ***********************************************************************/
private void CallWebservice(String cid, String scid,int ShuffleFlag) {
newarray.clear();
/*Log.e("cid","--->"+cid);
Log.e("scid","--->"+scid);*/
arrayList.clear();
String url = Constant.getphotoscid;
SharedPreferences sharedPreferences3 = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
username = sharedPreferences3.getString("username", null);
String timessstamp = sharedPreferences3.getString("timess", null);
Log.e("app", ".,....subsciption end date is....username..." + username);
arrayList.add(new BasicNameValuePair("uid", ""));
arrayList.add(new BasicNameValuePair("uname", username));
arrayList.add(new BasicNameValuePair("cid", cid));
// arrayList.add(new BasicNameValuePair("scid",scid));
arrayList.add(new BasicNameValuePair("scid", scid));
arrayList.add(new BasicNameValuePair("timestamp", timessstamp));
arrayList
.add(new BasicNameValuePair("page", String.valueOf(pagecount)));
//Log.e("fav", "....url" + url + "...arraylist" + arrayList.toString());
new WebConnectionAsync(url, arrayList, Tablet_Photos.this,ShuffleFlag).execute();
}
public class WebConnectionAsync extends AsyncTask<String, Object, Object> {
String val = null;
int ShuffleFlag=0;
ProgressDialog pd = null;
Context context = null;
List<NameValuePair> argList = null;
String url = null;
ProgressDialog dialog;
public WebConnectionAsync(String url, List<NameValuePair> argList,
Context ctx,int ShuffleFlag) {
this.url = url;
this.ShuffleFlag=ShuffleFlag;
this.argList = argList;
context = ctx;
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
photos_pb.setVisibility(View.VISIBLE);
dialog = ProgressDialog.show(Tablet_Photos.this, "",
"Please Wait...");
if(ShuffleFlag==1)
{
mAdapter.notifyDataSetChanged();
}
}
@Override
protected Object doInBackground(String... params) {
try {
val = HttpConnection.postData(url, argList);
val = val.toString();
GetPhotos(val);
Log.e("response", "we" + val);
} catch (Exception e) {
e.printStackTrace();
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(context,
"Problem with server. Please try again later.",
Toast.LENGTH_LONG).show();
}
});
}
return val;
}
private void GetPhotos(String val2) {
//ArrayList<DataSet> tempArray=new ArrayList<DataSet>();
try {
ArrayList<DataSet> new_array = new ArrayList<DataSet>();
ArrayList<DataSet> mostdownload_array = new ArrayList<DataSet>();
ArrayList<DataSet> featured_array = new ArrayList<DataSet>();
ArrayList<DataSet> old_array = new ArrayList<DataSet>();
//array.clear();
//mAdapter.notifyDataSetChanged();
JSONObject jsonObj = new JSONObject(val2);
JSONObject messageActivityResult = jsonObj
.getJSONObject("PhotoResult");
if (messageActivityResult.getString("status").equals("Ok")) {
totalpagecount = messageActivityResult
.getString("totalPages");
// *******************Saving timestamp into shared
// Perfernces*********************************
String tstimestamp = messageActivityResult
.getString("currentTStamp");
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("timess", tstimestamp);
editor.commit();
Log.e("scroll", "...total page count is..."
+ totalpagecount);
if (pagecount <= Integer.parseInt(totalpagecount)) {
pagecount++;
Log.e("scroll", "...page count if conditon");
}
if (GlobalVariables.isSuscribed.equals("0")) {
// ****************************** array for new Images
// *****************************************************************
JSONArray message = messageActivityResult
.getJSONArray("JSImages");
Log.e("yg", ".....message activity res..new,...."
+ messageActivityResult
.getJSONArray("JSImages").length());
Log.e("yg", ".....message activity ,...."
+ messageActivityResult.toString());
// ******************** NEW PHOTOS
// ARRAY***********************************************
for (int i = 0; i < message.length(); i++) {
DataSet d = new DataSet();
JSONObject jsoObj = message.getJSONObject(i);
JSONObject messageobject = jsoObj
.getJSONObject("JSImage");
if (messageobject.getString("photoSortType")
.equals("new")) {
d.setPid((messageobject.getString("pid")));
d.setPname((messageobject.getString("pname")));
d.setSort(messageobject
.getString("photoSortType"));
d.setTags(messageobject.getString("tags"));
d.setPhotoname(messageobject
.getString("photoname"));
d.setPhotodeatils((messageobject
.getString("photodetails")));
d.setPhotoresoultion((messageobject
.getString("photoresolution")));
d.setIshighresoltion((messageobject
.getString("ishighResol")));
d.setHighurl((messageobject
.getString("highurl")));
d.setUrl((messageobject.getString("thumbUrl")));
d.setIsfav((messageobject
.getString("isFavorite")));
d.setPhotographer((messageobject
.getString("photograpgher")));
d.setWidth(350);
d.setHeight(350);
d.setIsPhotographer((messageobject
.getString("isPhotographer")));
new_array.add(d);
}
}
for(int i=0;i<new_array.size();i++)
{
for(int j=i+1;j<new_array.size();j++)
{
if(new_array.get(i).getPid().equals(new_array.get(j).getPid()))
{
new_array.remove(j);
j--;
}
}
}
/*long seed = System.nanoTime();
Collections.shuffle(new_array, new Random(seed));*/
Log.e("array", "new array..." + new_array.size());
// ******************** MOST DOWNLOADED PHOTOS
// ARRAY***********************************************
for (int i = 0; i < message.length(); i++) {
DataSet d = new DataSet();
JSONObject jsoObj = message.getJSONObject(i);
JSONObject messageobject = jsoObj
.getJSONObject("JSImage");
if (messageobject.getString("photoSortType")
.equals("mostDownloaded")) {
d.setPid((messageobject.getString("pid")));
d.setPname((messageobject.getString("pname")));
d.setSort(messageobject
.getString("photoSortType"));
d.setTags(messageobject.getString("tags"));
d.setPhotoname(messageobject
.getString("photoname"));
d.setPhotodeatils((messageobject
.getString("photodetails")));
d.setPhotoresoultion((messageobject
.getString("photoresolution")));
d.setIshighresoltion((messageobject
.getString("ishighResol")));
d.setHighurl((messageobject
.getString("highurl")));
d.setUrl((messageobject.getString("thumbUrl")));
d.setIsfav((messageobject
.getString("isFavorite")));
d.setPhotographer((messageobject
.getString("photograpgher")));
d.setWidth(350);
d.setHeight(350);
d.setIsPhotographer((messageobject
.getString("isPhotographer")));
mostdownload_array.add(d);
}
}
for(int i=0;i<mostdownload_array.size();i++)
{
for(int j=i+1;j<mostdownload_array.size();j++)
{
if(mostdownload_array.get(i).getPid().equals(mostdownload_array.get(j).getPid()))
{
mostdownload_array.remove(j);
j--;
}
}
}
/*seed = System.nanoTime();
Collections.shuffle(mostdownload_array,
new Random(seed));*/
Log.e("array", "mostDownloaded array..."
+ mostdownload_array.size());
// ******************** FEATUIRED PHOTOS
// ARRAY***********************************************
for (int i = 0; i < message.length(); i++) {
DataSet d = new DataSet();
JSONObject jsoObj = message.getJSONObject(i);
JSONObject messageobject = jsoObj
.getJSONObject("JSImage");
if (messageobject.getString("photoSortType")
.equals("featured")) {
d.setPid((messageobject.getString("pid")));
d.setPname((messageobject.getString("pname")));
d.setSort(messageobject
.getString("photoSortType"));
d.setTags(messageobject.getString("tags"));
d.setPhotoname(messageobject
.getString("photoname"));
d.setPhotodeatils((messageobject
.getString("photodetails")));
d.setPhotoresoultion((messageobject
.getString("photoresolution")));
d.setIshighresoltion((messageobject
.getString("ishighResol")));
d.setHighurl((messageobject
.getString("highurl")));
d.setUrl((messageobject.getString("thumbUrl")));
d.setIsfav((messageobject
.getString("isFavorite")));
d.setPhotographer((messageobject
.getString("photograpgher")));
d.setWidth(350);
d.setHeight(350);
d.setIsPhotographer((messageobject
.getString("isPhotographer")));
featured_array.add(d);
}
}
for(int i=0;i<featured_array.size();i++)
{
for(int j=i+1;j<featured_array.size();j++)
{
if(featured_array.get(i).getPid().equals(featured_array.get(j).getPid()))
{
featured_array.remove(j);
j--;
}
}
}
/*seed = System.nanoTime();
Collections.shuffle(featured_array, new Random(seed));*/
Log.e("array",
"featured array..." + featured_array.size());
// ******************** OLD PHOTOS
// ARRAY***********************************************
for (int i = 0; i < message.length(); i++) {
DataSet d = new DataSet();
JSONObject jsoObj = message.getJSONObject(i);
JSONObject messageobject = jsoObj
.getJSONObject("JSImage");
if (messageobject.getString("photoSortType")
.equals("old")) {
d.setPid((messageobject.getString("pid")));
d.setPname((messageobject.getString("pname")));
d.setSort(messageobject
.getString("photoSortType"));
d.setTags(messageobject.getString("tags"));
d.setPhotoname(messageobject
.getString("photoname"));
d.setPhotodeatils((messageobject
.getString("photodetails")));
d.setPhotoresoultion((messageobject
.getString("photoresolution")));
d.setIshighresoltion((messageobject
.getString("ishighResol")));
d.setHighurl((messageobject
.getString("highurl")));
d.setUrl((messageobject.getString("thumbUrl")));
d.setIsfav((messageobject
.getString("isFavorite")));
d.setPhotographer((messageobject
.getString("photograpgher")));
d.setWidth(350);
d.setHeight(350);
d.setIsPhotographer((messageobject
.getString("isPhotographer")));
old_array.add(d);
}
}
for(int i=0;i<old_array.size();i++)
{
for(int j=i+1;j<old_array.size();j++)
{
if(old_array.get(i).getPid().equals(old_array.get(j).getPid()))
{
old_array.remove(j);
j--;
}
}
}
/*seed = System.nanoTime();
Collections.shuffle(old_array, new Random(seed));*/
Log.e("array", "old array..." + old_array.size());
array.addAll(new_array);
array.addAll(featured_array);
array.addAll(mostdownload_array);
array.addAll(old_array);
/*tempArray.addAll(new_array);
tempArray.addAll(featured_array);
tempArray.addAll(mostdownload_array);
tempArray.addAll(old_array);*/
Log.e("array",
"..for UNSubscribed user...data in array..."
+ array.toString());
}
// *****************************for Subscribe
// user*********************************************
else if (GlobalVariables.isSuscribed.equals("1")) {
ArrayList<DataSet> new_array1 = new ArrayList<DataSet>();
ArrayList<DataSet> mostdownload_array1 = new ArrayList<DataSet>();
ArrayList<DataSet> featured_array1 = new ArrayList<DataSet>();
ArrayList<DataSet> old_array1 = new ArrayList<DataSet>();
// ****************************** array for new Images
// *****************************************************************
JSONArray message = messageActivityResult
.getJSONArray("JSImages");
Log.e("yg", ".....message activity res..new,...."
+ messageActivityResult
.getJSONArray("JSImages").length());
Log.e("yg", ".....message activity ,...."
+ messageActivityResult.toString());
// ******************** NEW PHOTOS
// ARRAY***********************************************
for (int i = 0; i < message.length(); i++) {
DataSet d = new DataSet();
JSONObject jsoObj = message.getJSONObject(i);
JSONObject messageobject = jsoObj
.getJSONObject("JSImage");
if (messageobject.getString("photoSortType")
.equals("new")) {
d.setPid((messageobject.getString("pid")));
d.setPname((messageobject.getString("pname")));
d.setSort(messageobject
.getString("photoSortType"));
d.setTags(messageobject.getString("tags"));
d.setPhotoname(messageobject
.getString("photoname"));
d.setPhotodeatils((messageobject
.getString("photodetails")));
d.setPhotoresoultion((messageobject
.getString("photoresolution")));
d.setIshighresoltion((messageobject
.getString("ishighResol")));
d.setHighurl((messageobject
.getString("highurl")));
d.setUrl((messageobject.getString("thumbUrl")));
d.setIsfav((messageobject
.getString("isFavorite")));
d.setPhotographer((messageobject
.getString("photograpgher")));
d.setWidth(350);
d.setHeight(350);
d.setIsPhotographer((messageobject
.getString("isPhotographer")));
new_array1.add(d);
}
}
/*long seed = System.nanoTime();
Collections.shuffle(new_array1, new Random(seed));*/
for(int i=0;i<new_array1.size();i++)
{
for(int j=i+1;j<new_array1.size();j++)
{
if(new_array1.get(i).getPid().equals(new_array1.get(j).getPid()))
{
new_array1.remove(j);
j--;
}
}
}
Log.e("array", "new array..." + new_array1.size());
// ******************** MOST DOWNLOADED PHOTOS
// ARRAY***********************************************
for (int i = 0; i < message.length(); i++) {
DataSet d = new DataSet();
JSONObject jsoObj = message.getJSONObject(i);
JSONObject messageobject = jsoObj
.getJSONObject("JSImage");
if (messageobject.getString("photoSortType")
.equals("mostDownloaded")) {
d.setPid((messageobject.getString("pid")));
d.setPname((messageobject.getString("pname")));
d.setSort(messageobject
.getString("photoSortType"));
d.setTags(messageobject.getString("tags"));
d.setPhotoname(messageobject
.getString("photoname"));
d.setPhotodeatils((messageobject
.getString("photodetails")));
d.setPhotoresoultion((messageobject
.getString("photoresolution")));
d.setIshighresoltion((messageobject
.getString("ishighResol")));
d.setHighurl((messageobject
.getString("highurl")));
d.setUrl((messageobject.getString("thumbUrl")));
d.setIsfav((messageobject
.getString("isFavorite")));
d.setPhotographer((messageobject
.getString("photograpgher")));
d.setWidth(350);
d.setHeight(350);
d.setIsPhotographer((messageobject
.getString("isPhotographer")));
mostdownload_array1.add(d);
}
}
/*seed = System.nanoTime();
Collections.shuffle(mostdownload_array1, new Random(
seed));*/
for(int i=0;i<mostdownload_array1.size();i++)
{
for(int j=i+1;j<mostdownload_array1.size();j++)
{
if(mostdownload_array1.get(i).getPid().equals(mostdownload_array1.get(j).getPid()))
{
mostdownload_array1.remove(j);
j--;
}
}
}
Log.e("array", "mostDownloaded array..."
+ mostdownload_array1.size());
// ******************** FEATUIRED PHOTOS
// ARRAY***********************************************
for (int i = 0; i < message.length(); i++) {
DataSet d = new DataSet();
JSONObject jsoObj = message.getJSONObject(i);
JSONObject messageobject = jsoObj
.getJSONObject("JSImage");
if (messageobject.getString("photoSortType")
.equals("featured")) {
d.setPid((messageobject.getString("pid")));
d.setPname((messageobject.getString("pname")));
d.setSort(messageobject
.getString("photoSortType"));
d.setTags(messageobject.getString("tags"));
d.setPhotoname(messageobject
.getString("photoname"));
d.setPhotodeatils((messageobject
.getString("photodetails")));
d.setPhotoresoultion((messageobject
.getString("photoresolution")));
d.setIshighresoltion((messageobject
.getString("ishighResol")));
d.setHighurl((messageobject
.getString("highurl")));
d.setUrl((messageobject.getString("thumbUrl")));
d.setIsfav((messageobject
.getString("isFavorite")));
d.setPhotographer((messageobject
.getString("photograpgher")));
d.setWidth(350);
d.setHeight(350);
d.setIsPhotographer((messageobject
.getString("isPhotographer")));
featured_array1.add(d);
}
}
for(int i=0;i<featured_array1.size();i++)
{
for(int j=i+1;j<featured_array1.size();j++)
{
if(featured_array1.get(i).getPid().equals(featured_array1.get(j).getPid()))
{
featured_array1.remove(j);
j--;
}
}
}
Log.e("array",
"featured array..." + featured_array1.size());
// ******************** OLD PHOTOS
// ARRAY***********************************************
for (int i = 0; i < message.length(); i++) {
DataSet d = new DataSet();
JSONObject jsoObj = message.getJSONObject(i);
JSONObject messageobject = jsoObj
.getJSONObject("JSImage");
if (messageobject.getString("photoSortType")
.equals("old")) {
d.setPid((messageobject.getString("pid")));
d.setPname((messageobject.getString("pname")));
d.setSort(messageobject
.getString("photoSortType"));
d.setTags(messageobject.getString("tags"));
d.setPhotoname(messageobject
.getString("photoname"));
d.setPhotodeatils((messageobject
.getString("photodetails")));
d.setPhotoresoultion((messageobject
.getString("photoresolution")));
d.setIshighresoltion((messageobject
.getString("ishighResol")));
d.setHighurl((messageobject
.getString("highurl")));
d.setUrl((messageobject.getString("thumbUrl")));
d.setIsfav((messageobject
.getString("isFavorite")));
d.setPhotographer((messageobject
.getString("photograpgher")));
d.setWidth(350);
d.setHeight(350);
d.setIsPhotographer((messageobject
.getString("isPhotographer")));
old_array1.add(d);
}
}
//featured_array1.addAll(old_array1);
/*seed = System.nanoTime();
Collections.shuffle(old_array1, new Random(seed));*/
for(int i=0;i<old_array1.size();i++)
{
for(int j=i+1;j<old_array1.size();j++)
{
if(old_array1.get(i).getPid().equals(old_array1.get(j).getPid()))
{
old_array1.remove(j);
j--;
}
}
}
Log.e("array", "old array..." + old_array1.size());
array.addAll(new_array1);
array.addAll(featured_array1);
array.addAll(mostdownload_array1);
array.addAll(old_array1);
/*tempArray.addAll(new_array1);
tempArray.addAll(featured_array1);
tempArray.addAll(mostdownload_array1);
tempArray.addAll(old_array1);*/
Log.e("array",
"...for Subscribed user.data in array..."
+ array.toString());
}
//Collections.shuffle(array,new Random(System.nanoTime()));
}
} catch (Exception e) {
e.printStackTrace();
}
//Collections.shuffle(tempArray,new Random(System.nanoTime()));
for(int i=0;i<array.size();i++)
{
for(int j=i+1;j<array.size();j++)
{
if(array.get(i).getPid().equals(array.get(j).getPid()))
{
array.remove(j);
j--;
}
}
}
//array.addAll(tempArray);
}
@Override
protected void onPostExecute(Object result) {
super.onPostExecute(result);
Log.e("ShuffleFlag","--->"+String.valueOf(ShuffleFlag));
if(ShuffleFlag==1)
{
Collections.shuffle(array);
mAdapter.notifyDataSetChanged();
}
photos_pb.setVisibility(View.GONE);
for (int ii = 0; ii < array.size(); ii++) {
newarray.add(array.get(ii));
}
Log.e("new array", "..new array..." + newarray + "........");
/*
* mAdapter = new STGVAdapter(Tablet_Photos.this,
* getApplication(),array);
*
* ptrstgv.setAdapter(mAdapter); mAdapter.getMoreItem();
*/
if (count1 == 0) {
mAdapter = new STGVAdapter(Tablet_Photos.this,
getApplication(), array);
ptrstgv.setAdapter(mAdapter);
count1++;
}
/*else
{
mAdapter11=new STGVAdapter(Tablet_Photos.this, getApplication(),array);
ptrstgv.setAdapter(mAdapter11);
}*/
mAdapter.notifyDataSetChanged();
try {
dialog.dismiss();
} catch (Exception e) {
Log.e("exce", "" + e.toString());
}
}
}