I have made a gridview in which i have tried to add next feature in that grid. When someone clicks on "next", the grid is loaded with new bunch of images.
I have made a logic and somehow it is working but it is not stable. App gets stopped sometimes and sometime on clicking "next" the next page get skipped (Suppose currently i am on page 0 and when i click on "next" grid gets refreshed and page 2 is shown instead of page 1)
What i am doing is I have made a asynctask which send http request to the php server for new urls of images. then i refresh the gridview and display the new images from new image urls.
Code
public class UserPage extends Activity {
ImageAdapter ia = new ImageAdapter(this);
GetResponse task = new GetResponse(0);
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.userpage);
final GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(ia);
gridview.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
Button nxt = (Button) findViewById(R.id.nextbuttongrid);
nxt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// next page
int i = Integer.parseInt(GetResponse.counter);
i=i+1;
GetResponse.counter = Integer.toString(i);
String x = GetResponse.response;
task.execute();
//this is done so that the thread sleeps until the response variable value is updated
while(x.equals(GetResponse.response)==true){
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("Here in sleep..incre");
} }
change(gridview);
}
});
Button pre = (Button) findViewById(R.id.previousbuttongrid);
pre.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// previous page
int i = Integer.parseInt(GetResponse.counter);
i=i-1;
GetResponse.counter = Integer.toString(i);
String x = GetResponse.response;
task.execute();
//this is done so that the thread sleeps until the response variable value is updated
while(x.equals(GetResponse.response)==true){
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("Here in sleep.. decre");
} }
change(gridview);
}
});
/**
* On Click event for Single Gridview Item
* */
gridview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// Sending image id to FullImage.java
Intent i = new Intent(getApplicationContext(), FullImage.class);
i.putExtra("id", position);
startActivity(i);
}
});
}
void change(GridView gridview) {
ia = new ImageAdapter(this);
gridview.invalidateViews();
gridview.setAdapter(ia);
}
}
public class GetResponse extends AsyncTask<Void, Void, Void> {
public static String counter = "0";
HttpPost httppost;
StringBuffer buffer;
HttpResponse response1;
HttpClient httpclient;
List<NameValuePair> nameValuePairs;
public static String response;
@Override
protected Void doInBackground(Void... params) {
try {
Log.d("response's Value == >", "Response previous " + response);
httpclient=new DefaultHttpClient();
httppost= new HttpPost(ServerAdressHere);
//add your data
nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("username",AndroidPHPConnectionDemo.userName));
nameValuePairs.add(new BasicNameValuePair("password",AndroidPHPConnectionDemo.passWord));
nameValuePairs.add(new BasicNameValuePair("count",counter));
Log.d("Counter's Value == >", "Counter " + counter);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//Execute HTTP Post Request
response1=httpclient.execute(httppost);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
response = httpclient.execute(httppost, responseHandler);
Log.d("response's Value == >", "Response after " + response);
}
catch(Exception e){
System.out.println("Exception Here"+e);
}
return null;
}
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
private TextView txtUrl;
private String response;
public ImageView imageView;
public static String[] mThumbIds;
public ImageAdapter(Context c) {
mContext = c;
this.response = GetResponse.response.trim();
mThumbIds = response.split(",");
}
private final Downloader imageDownloader = new Downloader(mContext);
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return mThumbIds[position];
}
public long getItemId(int position) {
return mThumbIds[position].hashCode();
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(LayoutParams.MATCH_PARENT, 195));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(10, 10, 10, 10);
}
else {
imageView = (ImageView) convertView;
}
imageDownloader.DisplayImage(mThumbIds[position], (ImageView) imageView);
return imageView;
}
public Downloader getImageDownloader() {
return imageDownloader;
}
}
"counter" variable(static and String type) is sent in the request which tells the page number of the next gridview to php server. So the server will send the image urls tied to that page number.
The problem i am faciing is that the grid gets refreshed before "response" variable is updated by the asynctask class. So thats why i have used System.sleep() so as to get time gap b/w execution of two lines of code.
"response" variable(Static and integer type) contains the image urls.
Please help ! and if some more details are required please ask.