Android notifyDataSetChanged() example
10,044
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();
}
});
}
Author by
Syed Sehu Mujammil A
I'm a UI developer working in a private company in Chennai, India. I also love coding and I've been working with php development for quite sometime. Currently I'm working in android development and also android designing.
Updated on June 04, 2022Comments
-
Syed Sehu Mujammil A almost 2 years
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. -
Syed Sehu Mujammil A almost 10 yearsI just checked the tutorial. I also checked other similar tutorials like this. All tutorials are explaining Notifydatasetchanged() with an array of some strings from inside java code. In my case I'm getting data from database and that's where I have problems in using Notifydatasetchanged(). Any such tutorials using database??
-
Syed Sehu Mujammil A almost 10 yearsAlso in all tutorials Notifydatasetchanged() is used when a new item is added to the list. But in my case I'm updating a current list's existing data and trying to refresh list but it wouldn't happen for some reason.