how to refresh my gridView?

可紊 提交于 2019-12-22 11:28:02

问题


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

回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/16316208/how-to-refresh-my-gridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!