如何向gridview添加进度条

发布于 2024-12-06 10:41:39 字数 3378 浏览 1 评论 0原文

我是android编程的初学者所以也许这是一个愚蠢的问题...... 我读了很多关于异步任务添加进度条的内容,但我找不到我需要的解决方案:

我必须显示网格视图,但我需要搜索图像和文本上的数据以在其中显示。 因此,如果我在异步任务中将相关数据加载到数组中,则网格适配器没有数据并崩溃。 否则,如果我将进度对话框放在 gridview 的 oncreate 方法中,它会等待数据加载,但不显示进度条(它只是在加载数据结束时显示,而不是在加载数据期间显示)。 所以我不知道正确的方法。

我想从之前的活动开始,只是为了显示进度对话框,加载数据,最后启动网格视图,但在我看来,这不是一种经济的方法......

你觉得怎么样?

那么:如何将数据从 asynck 任务附加到适配器? 不起作用

我尝试使用 notyfydatasetchange 但它在我的 onCreate 方法中 : mAdapter = new ImageAdapter(this); mGrid.setAdapter(mAdapter);

但相同的适配器构造函数需要数据...(至少是对象的数量...)

,这是我的适配器:

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);
                startActivity(intent);

            }
        });
        return convertView;
    }
}

这是我的异步任务

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();
      }
}

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);
                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();
      }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不回头走下去 2024-12-13 10:41:39

AsyncTask 绝对是你的朋友。我发现在 AsyncTask 类中创建 ProgressDialog 的成员实例非常有效。假设您有一个名为 MyActivity 的 Activity 类。创建一个名为 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();
    }

}

然后在 onCreate() 方法中,调用 new DownloadTask().execute();

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();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文