如何刷新我的GridView的? [英] how to refresh my gridView?
问题描述
我有一个的ListView
。当我点击列表视图的列的按钮,一个按钮将在的main.xml
,我具有 onclicklistener $创建C $ C>自定义的ListView适配器按钮。我保存该按钮的点击在SQLite和由sqlite的,我更新gridview的适配器。
所以我的问题是,当我点击该按钮时, GridView控件
不更新自己,当我关闭应用程序,然后再次打开它就会被更新。
我试着用 notifydatasetchanged()
,但没有奏效。
这是code自定义列表视图适配器
holder.checkbox.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
字符串文本= holder.tvName.getText()的toString()。
。字符串图像= holder.tvimageurl.getText()的toString();
数据库处理器DB =新的数据库处理器(活动);
db.addContact(新联系(文字,图片));
db.close();
而这主要活动
GridView控件的ListView =(GridView控件)findViewById(R.id.gridview1);
imgAdapter =新ImageAdapter(本);
listView.setAdapter(imgAdapter);
imgAdapter.notifyDataSetChanged();
ImageAdapter code。
公共类ImageAdapter延伸BaseAdapter { 活动活动;
清单<联系与GT;项目;
LayoutInflater吹气;
ImageAdapter imgAdapter;
私人DisplayImageOptions选择;
私人DisplayImageOptions选项2;
ImageLoader的ImageLoader的;
私人联系objBean;
字符串的getURL,的getId;
ObjectOutputStream的OOS;
FileOutputStream中FOS; 公共ImageAdapter(活动行为){
this.activity =行为; 充气= act.getLayoutInflater();
数据库处理器DB =新的数据库处理器(活动);
项目= db.getAllContacts();
db.close();
} @覆盖
公众诠释的getCount(){
返回item.size();
} @覆盖
公众联系的getItem(INT位置){
返回null;
} @覆盖
众长getItemId(INT位置){
返回的位置;
} @覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
最终的LinearLayout ImageView的;
如果(convertView == NULL){
ImageView的=(的LinearLayout)inflater.inflate(R.layout.item_grid_image,父母,假);
}其他{
ImageView的=(的LinearLayout)convertView;
}
您需要在调用之前notifyDataSetChanged添加数据先ImageAdapter。或重新创建,然后重新设置你的GridView
UPD:
好了,然后把加载code到单独的方法你ImageAdapter内调用刷新你添加新的数据后,
公共ImageAdapter(活动行为){
this.activity =行为; 充气= act.getLayoutInflater();
刷新();
}无效刷新()
{
数据库处理器DB =新的数据库处理器(活动);
项目= db.getAllContacts();
db.close();
notifyDatasetChanged();
}
不过还好手动添加新的联系人到您ImageAdapter当你点击你的按钮
公共无效的onClick(视图v){
字符串文本= holder.tvName.getText()的toString()。
。字符串图像= holder.tvimageurl.getText()的toString();
数据库处理器DB =新的数据库处理器(活动);
联系方式联系我们=新的联系人(文字,图片)
db.addContact(接触);
db.close();
imgAdapter.addItem(接触)
imgAdapter.notifyDatasetChanged();
}
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;
}
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();
}
这篇关于如何刷新我的GridView的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!