我从web服务中获取GridView的图像和文本,因此显示GridView需要一些时间。我想显示一个ProgressDialog,直到Grid完全加载。我到现在所做的如下:
public class PCGridMain extends Activity
{
WebService web = new WebService();
private GridView gridView;
ProgressDialog dialog;
Bitmap icon;
int i, total;
URL url= null;
List<GridItem> list;
@Override
public void onCreate(Bundle grid)
{
super.onCreate(grid);
dialog = ProgressDialog.show(PCGridMain.this, "Loading...", "Loading App, Please wait.", true);
DialogWork dWork = new DialogWork();
dWork.execute();
setContentView(R.layout.main);
gridView = (GridView)findViewById(R.id.gridView1);
web.WebService1();
total = web.totalService;
list = new ArrayList<GridItem>();
for(i=0; i<total; i++)
{
Log.v("Try Block", "See what we get:-");
try
{
Log.v("GridMain", "try url" + Integer.toString(i));
url = new URL(web.arr[i][2]);
}
catch (MalformedURLException e)
{
Log.v("GridMain", "catch MalformedURLException" + Integer.toString(i));
e.printStackTrace();
}
try
{
Log.v("GridMain", "try BitmapFactory" + Integer.toString(i));
icon = BitmapFactory.decodeStream(url.openConnection().getInputStream());
}
catch (IOException e)
{
Log.v("GridMain", "catch IOException" + Integer.toString(i));
e.printStackTrace();
}
list.add(new GridItem(icon, web.arr[i][1])); // Adding Icon & LAbel
}
gridView.setAdapter(new GridAdapter(this, list));
gridView.setOnItemClickListener(Itemlistener);
}
private OnItemClickListener Itemlistener = new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long id)
{
ViewHolder holder = (ViewHolder)view.getTag();
if(holder == null)
{
return;
}
Toast.makeText(PCGridMain.this, holder.label.getText(), Toast.LENGTH_SHORT).show();
Log.v("GridMain", "Intent Creation");
Intent intent = new Intent(view.getContext(), ShowService.class); Log.v("GridMain", "Intent Created");
intent.putExtra("ServiceId", web.arr[position][0]); Log.v("GridMain", "ValueAdded Sid");
intent.putExtra("SName", holder.label.getText()); Log.v("GridMain", "ValueAdded SName");
startActivity(intent);
}
};
class DialogWork extends AsyncTask<URL, Integer, Long>
{
protected Long doInBackground(URL... params)
{
try
{
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
protected void onProgressUpdate(Integer... progress)
{
}
protected void onPostExecute(Long result)
{
try
{
//setContentView(R.layout.main);
//gridView.setAdapter(new GridAdapter(PCGridMain.this, list));
dialog.dismiss();
}
catch (Exception e)
{
e.printStackTrace();
dialog.dismiss();
}
}
}
请告诉我,必须将代码放在哪个确切位置,每当我做一些更改时,它都会显示无效或App因某些问题而关闭。
谢谢,
HAPS。
答案 0 :(得分:5)
尝试将所有渲染部件从服务器放入doInBackground()并在onPostExecute()中设置适配器。甚至在onPreExecute()中启动progressdialog并在onPostExecute()上关闭它,但不在onCreate()中关闭它。我认为它会解决你的问题......
答案 1 :(得分:4)
这应该是你的内部AsyncTask
类,根据需要更改参数。
private class yourTask extends AsyncTask<Void, Void, ArrayList> {
String message;
ProgressDialog dialog;
public refreshTask(String message) {
this.message = message;
this.dialog = new ProgressDialog(PCGridMain.this);
}
@Override
protected void onPreExecute() {
dialog.setMessage(message);
dialog.setIndeterminate(true);
dialog.setCancelable(true);
dialog.show();
}
@Override
protected ArrayList doInBackground(String... params) {
// Some work
}
@Override
protected void onPostExecute(ArrayList result) {
if(dialog.isShowing())
dialog.dismiss();
}
}
所以你可以称这个类为:
new yourTask('Dialog message').execute();
我希望它能解决你的问题。
答案 2 :(得分:3)
我在这里给出了我的问题的完整答案,这样可以帮助其他人轻松完成......
public class PCGridMain extends Activity
{
WebService web = new WebService();
private GridView gridView;
ProgressDialog dialog;
Bitmap icon;
int i, total;
URL url= null;
List<GridItem> list;
@Override
public void onCreate(Bundle grid)
{
super.onCreate(grid);
Log.v("GridMain", "setContent");
setContentView(R.layout.main);
gridView = (GridView)findViewById(R.id.gridView1);
DialogWork dWork = new DialogWork();
dWork.execute();
}
private void ForLoop()
{
for(i=0; i<total; i++)
{
Log.v("Try Block", "See what we get:-");
try
{
Log.v("GridMain", "try url" + Integer.toString(i));
url = new URL(web.arr[i][2]);
}
catch (MalformedURLException e)
{
Log.v("GridMain", "catch MalformedURLException" + Integer.toString(i));
e.printStackTrace();
}
try
{
Log.v("GridMain", "try BitmapFactory" + Integer.toString(i));
icon = BitmapFactory.decodeStream(url.openConnection().getInputStream());
}
catch (IOException e)
{
Log.v("GridMain", "catch IOException" + Integer.toString(i));
e.printStackTrace();
}
list.add(new GridItem(icon, web.arr[i][1])); // Adding Icon & LAbel
}
}
private OnItemClickListener Itemlistener = new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long id)
{
ViewHolder holder = (ViewHolder)view.getTag();
if(holder == null)
{
return;
}
Toast.makeText(PCGridMain.this, holder.label.getText(), Toast.LENGTH_SHORT).show();
Log.v("GridMain", "Intent Creation");
Intent intent = new Intent(view.getContext(), ShowService.class); Log.v("GridMain", "Intent Created");
intent.putExtra("ServiceId", web.arr[position][0]); Log.v("GridMain", "ValueAdded Sid");
intent.putExtra("SName", holder.label.getText()); Log.v("GridMain", "ValueAdded SName");
startActivity(intent);
}
};
class DialogWork extends AsyncTask<URL, Integer, String>
{
protected void onPreExecute()
{
Log.v("GridMain", "PreExecute()");
dialog = ProgressDialog.show(PCGridMain.this, "Loading...", "Loading App, Please wait.", false, true);
}
protected Long doInBackground(URL... params)
{
String response = "";
try
{
Log.v("GridMain", "doInBackground");
response = web.WebService1();
total = web.totalService;
}
catch (InterruptedException e)
{
Log.v("GridMain", "InterruptedException");
e.printStackTrace();
}
return response;
}
protected void onPostExecute(String result)
{
try
{
// Response is in RESULT_VAR
Log.v("GridMain", "onPostExecute");
list = new ArrayList<GridItem>();
ForLoop();
gridView.setAdapter(new GridAdapter(PCGridMain.this, list));
gridView.setOnItemClickListener(Itemlistener);
dialog.dismiss();
}
catch (Exception e)
{
Log.v("GridMain", "Exception e");
e.printStackTrace();
dialog.dismiss();
}
}
}
}
我根据自己的需要使用它,只是为了帮助,完整的代码可能会给你带来麻烦。所以只需将其作为参考。
谢谢&amp;的问候,
HAPS。
答案 3 :(得分:2)
尝试使用此代码对话,否则代码似乎正常工作
dialog= new ProgressDialog(this);
dialog.setMessage("Loading");
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.setCancelable(false);
dialog.show();