how to refresh my gridView?

2019-05-25 03:23发布

I have a ListView. When I click on the button of the row of listview, a button will be created in the main.xml, I am having the onclicklistener of the button in custom listView adapter. I am saving the click of that button in sqlite and by that sqlite I am updating the gridview adapter.

So my question is when I click on the button, the GridView is not updating itself and when I close the application and open it again it gets updated.

I tried using notifydatasetchanged() but didn't work..

This is the code in custom List View adapter

holder.checkbox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
             String text = holder.tvName.getText().toString();
             String image = holder.tvimageurl.getText().toString();
             DatabaseHandler db = new DatabaseHandler(activity);
             db.addContact(new Contact(text, image));  
             db.close();

and this in the main Activity

    GridView listView = (GridView) findViewById(R.id.gridview1);
    imgAdapter = new ImageAdapter(this);
    listView.setAdapter(imgAdapter);
    imgAdapter.notifyDataSetChanged();

ImageAdapter code.

public class ImageAdapter extends BaseAdapter {

    Activity activity;
    List<Contact> item;
    LayoutInflater inflater;
    ImageAdapter imgAdapter;
    private DisplayImageOptions options;
    private DisplayImageOptions options2;
    ImageLoader imageLoader;
    private Contact objBean;
    String getUrl,getId;
    ObjectOutputStream oos;
    FileOutputStream fos;



    public ImageAdapter(Activity act) {
        this.activity=act;

        inflater = act.getLayoutInflater();


        DatabaseHandler db = new DatabaseHandler(activity);
        item = db.getAllContacts();
        db.close();
}

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final LinearLayout imageView;
        if (convertView == null) {
            imageView = (LinearLayout)        inflater.inflate(R.layout.item_grid_image, parent, false);
        } else {
            imageView = (LinearLayout) convertView;
        }

2条回答
劳资没心,怎么记你
2楼-- · 2019-05-25 04:02

You need to add data to ImageAdapter first before calling notifyDataSetChanged. Or recreate it and set again to your GridView

UPD: Ok, then put loading code to separate method inside your ImageAdapter and call refresh after you add new data

public ImageAdapter(Activity act) {
        this.activity=act;

        inflater = act.getLayoutInflater();
        refresh();
}

void refresh()
{
        DatabaseHandler db = new DatabaseHandler(activity);
        item = db.getAllContacts();
        db.close();
        notifyDatasetChanged();
}

But better add new contact manually to your ImageAdapter when you click on your button

public void onClick(View v) {
    String text = holder.tvName.getText().toString();
    String image = holder.tvimageurl.getText().toString();
    DatabaseHandler db = new DatabaseHandler(activity);
    Contact contact = new Contact(text, image)
    db.addContact(contact);  
    db.close();
    imgAdapter.addItem(contact)
    imgAdapter.notifyDatasetChanged();
}
查看更多
爷的心禁止访问
3楼-- · 2019-05-25 04:02

You can force the Adapter to update itself within your onClick method e.g.

public void onClick(View v) {
    String text = holder.tvName.getText().toString();
    String image = holder.tvimageurl.getText().toString();
    DatabaseHandler db = new DatabaseHandler(activity);
    db.addContact(new Contact(text, image));
    db.close();
    notifyDataSetChanged(); // This will do the trick
 }

EDIT

As you are getting the data when you restart that means it is definitely being saved in the database OK, this suggests that when you invalidate the Adapter it isn't reloading the database. Try something like a custom method in ImageAdapter like this:

public void onClick(View v) {
    String text = holder.tvName.getText().toString();
    String image = holder.tvimageurl.getText().toString();
    DatabaseHandler db = new DatabaseHandler(activity);
    db.addContact(new Contact(text, image));
    db.close();
    reloadData();  // will refresh the data
    notifyDataSetChanged(); // will update the contents
 }

private void reloadData() {
    DatabaseHandler db = new DatabaseHandler(activity);
    item = db.getAllContacts();
    db.close();
}
查看更多
登录 后发表回答