我有ListView
。当我单击listview行的按钮时,将在main.xml
中创建一个按钮,我在自定义listView适配器中拥有该按钮的onclicklistener
。我在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();
并在主Activity
中 GridView listView = (GridView) findViewById(R.id.gridview1);
imgAdapter = new ImageAdapter(this);
listView.setAdapter(imgAdapter);
imgAdapter.notifyDataSetChanged();
ImageAdapter代码。
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;
}
答案 0 :(得分: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();
}
答案 1 :(得分: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();
}