Can some one give me an example tutorial or guide me in using notifyDataSetChanged()
in my adapter? I'm getting my data from database and filling my listview. Also in my listview I have a button to like that particular content upon which my database value will be updated and the button text will change to "Liked". But i'm not sure how to refresh my listview again with the data from database.
EDIT:
Create custom class for array list in adapter
public class Entity {
int id;
variables ..........
boolean isLiked = false;
public Entity(some values){
// set the id;
variables = values ;
}
public void setLiked(boolean like){
this.isLiked = like;
// you must update database here
}
public boolean IsLiked(){ return this.isLiked; }
}
create custom adapter
public class EntityAdapter extends ArrayAdapter<Entity> {
.................................
........other methods............
.................................
public View getView(int position, View convertView, ViewGroup parent) {
final Entity entity = arrayList.get(position);
final ViewHolder holder;
View view = convertView;
if (view == null) {
int layoutCode=this.layoutcode;
view = ((Activity) context).getLayoutInflater().inflate(layoutCode, parent, false);
holder = new ViewHolder();
assert view != null;
holder.chkLike=(CheckBox) view.findViewById(R.id.chkLike);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.chkLike.setChecked(entity.IsLiked());
return view;
}
class ViewHolder {
CheckBox chkLike;
}
}
Main Activity
public class Main extends Activity{
EntityAdapter adapter;
GridView gridView = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<Entity> arraylist = EntityHelper.fetch("select * from entity",this);
adapter = new EntityAdapter(this, R.layout.item_grid_image, arraylist);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
adapter.getItem(pos).setLiked(true);
adapter.notifyDataSetChanged();
}
});
}