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
But better add new contact manually to your ImageAdapter when you click on your button
You can force the
Adapter
to update itself within youronClick
method e.g.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 inImageAdapter
like this: