我做了一个gridview,我试图在该网格中添加下一个功能。当有人点击“下一步”时,网格会加载新的图像。
我已经制定了一个逻辑,但它确实有效,但它并不稳定。应用程序有时会停止,有时单击“下一步”会跳过下一页(假设当前我在第0页,当我点击“下一步”网格时会刷新并显示第2页而不是第1页)
我在做什么 我做了一个asynctask,它向php服务器发送http请求以获取新的图片网址。 然后我刷新gridview并显示新图像网址中的新图像。
代码
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”变量(静态和字符串类型)在请求中发送,该请求告诉php服务器的下一个gridview的页码。因此,服务器将发送与该页码绑定的图像URL。
我所面临的问题是,在asynctask类更新“response”变量之前,网格会被刷新。这就是为什么我使用System.sleep()以便获得两行代码的时间间隔b / w执行。
“response”变量(静态和整数类型)包含图片网址。
请帮忙!如果需要更多细节,请询问。
答案 0 :(得分:0)
首先,onClick方法循环中的Thread.sleep不是这样做的正确方法。 asynctask的重点是避免锁定主线程,而Thread.sleep正在这样做。
改为使用asynctask的onPostExecute。
然而,到目前为止这显然是有效的(你的服务器和连接都是快速的,否则你会有ANR),即使你的ui可能会冻结几秒钟。
之后,有图像下载的问题。调用setAdapter(新的Adapter ......)似乎是一个很好的方法。
你一步一步地调试了多远?
Log.d("response's Value == >", "Response after " + response);
中的图片ID列表?出于好奇,为什么要两次执行请求? (response1=httpclient.execute(httppost);
)
我真的不相信你没有,虽然提到你的应用实际上是崩溃是有趣的,这与'网格没有得到刷新'完全不同。
你必须
A /读取堆栈跟踪。
如果不清楚,请谷歌吧。我/我不知道还有什么可以添加到(a task can be executed only once)