how to add a progress bar to a gridview
i am a beginner in android programming so maybe it's a silly question... I read a lot about async task to add a progress bar but i can't find out the solution i need:
I have to show a grid view, but i need to search for data on images and texts to show in it. So if i load the relative data in an array in an asynctask the adapter of the grid doesn't have data and crashes. Otherwise, if i put the progress dialog in the oncreate method of the gridview, it waits the loading of data but don't show the progess bar(it just to at the end of loading data, not during it). So I don't know the correct approach.
I thought to start with a previous activity only to show the progress dialog, loading the data, and at the end starting the gridview, but it not seems to me an economic approach...
what do you think?
So: How can I attach data to the adapter from the asynk task? I tried with notyfydatasetchange but it doesn't work
in my onCreate Method i have: mAdapter = new ImageAdapter(this); mGrid.setAdapter(mAdapter);
but the same adapter constructor needs data... (al least the number of objects...)
and this is my adapter:
public class ImageAdapter extends BaseAdapter{
Context mContext;
public final int ACTIVITY_CREATE = dirObjList.size()*2;
public ImageAdapter(Context c){
mContext = c;
map = new HashMap<Integer,SoftReference<Bitmap>>();
}
public Map<Integer,SoftReference<Bitmap>> map;
@Override
public int getCount() {
return dirObjList.size();
}
public RowData getItem(int position) {
return dirObjList.get(position);
}
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
RowData currDirObj= getItem(position);
if(convertView==null){
LayoutInflater li = getLayoutInflater();
convertView = li.inflate(R.layout.icon, null);
holder = new ViewHolder();
holder.tv = (TextView)convertView.findViewById(R.id.icon_text);
holder.iv = (ImageView)convertView.findViewById(R.id.icon_image);
convertView.setTag(holder);
}
else{
holder = (ViewHolder)convertView.getTag();
}
holder.iv=setThumbs(holder.iv,position);
holder.tv.setText(currDirObj.mTitle);
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences indexPrefs = getSharedPreferences("currentIndex",
MODE_PRIVATE);
SharedPreferences.Editor indexEditor = indexPrefs.edit();
indexEditor.putInt("currentIndex", 0);
SharedPreferences drPrefs = getSharedPreferences("currentDir",
MODE_PRIVATE);
SharedPreferences.Editor drPath = drPrefs.edit();
drPath.putString("currentDir", dirObjList.get(position).mDetail);
drPath.commit();
final Intent intent = new Intent(ImageGrid.this, SpeakinGallery.class);
开发者_Python百科 startActivity(intent);
}
});
return convertView;
}
}
and this is my async task
public class MyTask extends AsyncTask<Void, Void, String> {
ProgressDialog progress;
public MyTask(ProgressDialog progress) {
this.progress = progress;
}
public void onPreExecute() {
progress.show();
}
public String doInBackground(Void...unused) {
getSubDirs(startDIRECTORY);
return "foo";
}
public void onPostExecute(Void unused) {
progress.dismiss();
//mAdapter.notifyDataSetChanged();
}
}
AsyncTask is most definitely your friend. I find it works really well to create a member instance of ProgressDialog inside of your AsyncTask class. Let's say you have an Activity class called MyActivity. Create an inner class called DownloadTask.
private class DownloadTask extends AsyncTask<Object, Void, Object> {
private ProgressDialog dialog;
@Override
protected void onPreExecute() {
dialog = ProgressDialog.show(MyActivity.this, "", "Loading...");
}
@Override
protected Object doInBackground(Object... params) {
// download information and return it
return dataObject;
}
@Override
protected void onPostExecute(Object result) {
// use result object from background task and
// attach data to adapter
dialog.dismiss();
}
}
And then in your onCreate() method, call new DownloadTask().execute();
精彩评论