2

我有一个ListView. 当我单击 listview 行的按钮时,将在 中创建一个按钮main.xml,我onclicklistener在自定义 listView 适配器中拥有该按钮。我在 sqlite 中保存了对该按钮的单击,并通过该 sqlite 更新了 gridview 适配器。

所以我的问题是,当我单击按钮时,GridView它不会自行更新,当我关闭应用程序并再次打开它时,它会更新。

我尝试使用notifydatasetchanged()但没有工作..

这是自定义列表视图适配器中的代码

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

这在主要活动中

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

图像适配器代码。

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

2 回答 2

3

您可以强制Adapter在您的方法中自行更新,onClick例如

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
 }

编辑

当您在重新启动时获取数据时,这意味着它肯定已保存在数据库中,这表明当您使其无效时,Adapter它不会重新加载数据库。尝试像这样的自定义方法ImageAdapter

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();
}
于 2013-05-01T10:12:53.690 回答
2

在调用 notifyDataSetChanged 之前,您需要先将数据添加到 ImageAdapter。或者重新创建它并再次设置为您的 GridView

UPD:好的,然后将加载代码放入 ImageAdapter 中的单独方法并在添加新数据后调用刷新

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

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

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

但是,当您单击按钮时,最好手动将新联系人添加到您的 ImageAdapter

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();
}
于 2013-05-01T10:10:38.883 回答