要使用ProgressDialog到GridView控件会从web服务加载 [英] To use ProgressDialog till GridView gets loaded from webservice
问题描述
我获取的图像和文本的GridView控件从Web服务,所以它需要一些时间来显示GridView控件。我想说明一个ProgressDialog到电网得到满载。我做了什么至今是如下:
I am fetching Image and Text for GridView from a webservice, so its takes some time to display the GridView. I want to show a ProgressDialog till Grid gets fully loaded. What I have done till now is as below:
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();
}
}
}
请告诉我,什么code已被放置在什么确切位置,每当我做了一些改动,它要么显示没有影响或应用程序关闭,由于一些问题。
Please tell me that what code has to be placed at what exact location, whenever I do some changes, it either shows no effect or App closes due to some issue.
谢谢
高空平台。
推荐答案
尝试把所有的渲染部分来自服务器doInBackground(),并设置适配器onPostExecute()。甚至开始progressdialog在上preExecute(),并驳回对onPostExecute(),但不是在的onCreate()。我认为这将解决乌尔问题....
Try to put all rendering part from server in doInBackground() and set the adapter in onPostExecute() . And even start the progressdialog in onPreExecute() in and dismiss it on onPostExecute() but not in onCreate(). I think it will solve ur problem....
这篇关于要使用ProgressDialog到GridView控件会从web服务加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!