2

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.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
  • 1
    Have you tried to use it? Just call that method when your data changes. – codeMagic Jul 03 '14 at 16:13
  • I tried it but my values are not updated. I have put my problem as a question with more detail [here](http://stackoverflow.com/questions/24415292/android-list-view-current-position-doesnt-work-properly) – Syed Sehu Mujammil A Jul 03 '14 at 16:29

1 Answers1

2

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();


        }


    });
}
Manian Rezaee
  • 1,012
  • 12
  • 26
  • I 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 Jul 03 '14 at 17:56
  • Also 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. – Syed Sehu Mujammil A Jul 03 '14 at 18:00