Android GridView - notifyDataSetChanged没有刷新GridView

时间:2016-11-01 20:24:53

标签: android gridview refresh android-gridview baseadapter

我正在编写一个应用程序,使用GridView显示存储在本地数据库中的图像网格。我实现了由按钮触发的deleteSelectedImage方法,该按钮删除数据库中网格的给定图像。删除有效,但我无法让我的GridView自行刷新...如果我关闭活动然后再次打开它,则可以看到更改。

这是我的代码:

活动:

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

public class GalleryShowActivity extends AppCompatActivity {

    private final String DEBUG_TAG = "GalleryShowActivity";

    private DatabaseHandler db;
    private Cursor c;

    private ImageAdapter mBaseAdapter;
    private TextView selectedImageText;
    private GridView mGridView;

    // currently selected position in the grid
    private int mSelectedPos;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gallery_show);

        db = new DatabaseHandler(this);
        c = db.getCursorOnAllImages(this);
        mBaseAdapter = new ImageAdapter(this, c, db);
        mSelectedPos = -1;

        selectedImageText = (TextView)             findViewById(R.id.selectedImageText);
        selectedImageText.setText("Selected image : NONE");

        mGridView = (GridView) findViewById(R.id.gridview);
        mGridView.setAdapter(mBaseAdapter);
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                                int position, long id) {

                Log.d(DEBUG_TAG, "IMAGE CLICKED");

                // click selects an image (can be changed later)
                mSelectedPos = position;
                selectedImageText.setText("Selected image : " + (position + 1));
            }
        });
     }


    public void deleteSelectedImage(View view) {
        boolean exists = c.moveToPosition(mSelectedPos);
        if (exists) {
            boolean success = db.deletePointedPicture(c, this);
            if (success) {
                Log.d(DEBUG_TAG, "DELETE SUCCESS");
                mBaseAdapter.notifyDataSetChanged();
                // display a success message...
            }
        } else {
            // display an error...
        }
    }
}

以下是ImageAdapter类的代码:

package ch.epfl.sweng.project;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;


class ImageAdapter extends BaseAdapter {
    private Context mContext;
    private Cursor mCursor;
    private DatabaseHandler mHandler;

    ImageAdapter(Context context, Cursor cursor, DatabaseHandler handler) {
        mContext = context;
        mCursor = cursor;
        mHandler = handler;
    }

    @Override
    public int getCount() {
        return mCursor.getCount();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int rowHeight = 200;
        int rowWidth = 300;

        ImageView imageView;
        if (convertView == null) {
            // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(rowWidth, rowHeight));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

        Bitmap bm = mHandler.getImageByIndex(mCursor, position);
        imageView.setImageBitmap(bm);
        imageView.setVisibility(View.VISIBLE);
        return imageView;
    }
}

我也试过这个:

mBaseAdapter = new ImageAdapter(this, c, db);
mGridView.setAdapter(mBaseAdapter);

而不是:

mBaseAdapter.notifyDataSetChanged();

但它不起作用。

1 个答案:

答案 0 :(得分:0)

你实施错了。即使删除光标中的数据也是一样的。光标中的数据仅在再次调用onCreate()时更新。删除数据后更新deleteSelectedImage方法上的光标,然后调用notifyDataSetChanged或尝试在构造函数中传递列表而不是游标,删除时,从列表和数据库中删除数据,然后调用notifyDataSetChanged。