I am writing an android application to list the item from database, and mark some of those item as favorite and keep update the database according to favorite.
My Aim is
:
When I mark particular item of listView as favorite, the image of that particular list item will be changed and the corresponding column of table should be updated as favorite(yes) or not favorite(no).
It was working fine to list the item from database, change the image source according to favorite, update the database also.
My Problem is
:
1.When I press an image of particular item, the image would be changed once
, and corresponding database also would be changed. But, Clicking on the same image another time
(last time clicked image) does not change anything
(not change image and not update).
2.By scrolling the listView keep the listview as before changing the listView
.
I refer this link, before
I use database
. It was working fine.
After I use Database, I changed my code as below:
public class EpisodeCursorAdapter extends SimpleCursorAdapter {
public final String TAG = "EpisodeCursorAdapter";
//private String strurl = "http://timesofindia.feedsportal.com/c/33039/f/533916/index.rss";
public int favourite[];
private Cursor c,temp_cursor;
private Context context;
AABDatabaseManager db;
public EpisodeCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
this.c = c;
temp_cursor=c;
this.context = context;
Log.d(TAG, "Object size is:" +c.getCount());
// favourite = new int[c.getCount()];
// for (int i = 0; i < c.getCount(); i++) {
// favourite[i] = 0;
// }
}
static class EpisodeHolder {
TextView title;
TextView desc;
ImageView thumbnail, favourite;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
Log.d(TAG,"NEw view in Cursor adapater"+" "+cursor.getCount());
Cursor cc=getCursor();
Log.d(TAG,"NEw view in Cursor adapater"+" "+cc.getCount()+ " "+cc.getInt(0));
// TODO Auto-generated method stub
return super.newView(context, cursor, parent);
}
public View getView(final int pos, View inView, ViewGroup parent) {
Log.d(TAG,"getView view in Cursor adapater " + pos);
View v = inView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.episode_row_view, null);
final EpisodeHolder holder = new EpisodeHolder();
db = new AABDatabaseManager(context);
holder.title = (TextView) v.findViewById(R.id.title);
holder.desc = (TextView) v.findViewById(R.id.description);
holder.favourite = (ImageView) v.findViewById(R.id.imageView1);
holder.thumbnail = (ImageView) v.findViewById(R.id.image);
v.setTag(holder);
Log.d(TAG, "Row tag is:" + v.getTag());
holder.favourite.setTag(pos);
holder.favourite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int posi = (Integer) v.getTag();
//Log.d(TAG, "Cursor total count" + c.getCount() + " position:" + holder.favourite.getTag() + " view position:" + pos + " " + posi);
Log.d(TAG,"****************************** INFO REGARDING CLICKING ON FAVOURITE ************************");
Log.d(TAG,"Position of image clicked : " + posi);
temp_cursor.moveToPosition(posi);
int id = temp_cursor.getInt(temp_cursor.getColumnIndex("_id"));
String markedAsFav = temp_cursor.getString(temp_cursor.getColumnIndex("favourite"));
Log.d(TAG, "Id of clicked item is : " + id + " Favourite : " + markedAsFav);
if(markedAsFav.equalsIgnoreCase("no")) {
Log.d(TAG, "****In favourite onClick*** Was not as favourite... Mark as favourite...");
holder.favourite.setImageResource(R.drawable.ic_favourite_2);
db.updateRowAsFav(id, temp_cursor.getString(temp_cursor.getColumnIndex("favourite")));
} else if(markedAsFav.equalsIgnoreCase("yes")) {
Log.d(TAG, "****In favourite onClick*** Was as favourite... Mark it as not favourite...");
holder.favourite.setImageResource(R.drawable.ic_favourite_1);
db.updateRowAsFav(id, temp_cursor.getString(temp_cursor.getColumnIndex("favourite")));
}
}
});
} else {
((EpisodeHolder) v.getTag()).favourite.setTag(pos);
}
final EpisodeHolder holder = (EpisodeHolder) v.getTag();
this.c.moveToPosition(pos);
String imageUrl = this.c.getString(this.c
.getColumnIndex("table_epi_image"));
String titleStr = this.c.getString(this.c
.getColumnIndex("table_epi_title"));
String descStr = this.c.getString(this.c
.getColumnIndex("table_epi_description"));
if(c.getString(c.getColumnIndex("favourite")).equalsIgnoreCase("yes"))
{
holder.favourite.setImageResource(R.drawable.ic_favourite_2);
}
else
{
holder.favourite.setImageResource(R.drawable.ic_favourite_1);
}
holder.thumbnail.setTag(imageUrl);
new DownloadImagesTask().execute(holder.thumbnail);
//TextView title = (TextView) v.findViewById(R.id.title);
holder.title.setText(titleStr);
//TextView desc = (TextView) v.findViewById(R.id.description);
holder.desc.setText(descStr);
return (v);
}
}
I know that i am missing something. But I couldn't find out what is that.
Please help me to solve this.
Thank you in Advance!
As Yume117 said, I have called notifyDataSetChanged() method. I have updated my answer as below:
I have added this line refreshCursorAdapter();
next to below codes in onclick()
if(markedAsFav.equalsIgnoreCase("no")) {
Log.d(TAG, "****In favourite onClick*** Was not as favourite... Mark as favourite...");
holder.favourite.setImageResource(R.drawable.ic_favourite_2);
db.updateRowAsFav(id, temp_cursor.getString(temp_cursor.getColumnIndex("favourite")));
} else if(markedAsFav.equalsIgnoreCase("yes")) {
Log.d(TAG, "****In favourite onClick*** Was as favourite... Mark it as not favourite...");
holder.favourite.setImageResource(R.drawable.ic_favourite_1);
db.updateRowAsFav(id, temp_cursor.getString(temp_cursor.getColumnIndex("favourite")));
}
refreshCursorAdapter is:
public void refreshCursorAdapter() {
// TODO Auto-generated method stub
Log.d(TAG,"Refresh adapter is called....***************");
dbAdapter = new DBAdapter();
Cursor cursor = null;
try {
EpisodeCursorAdapter adapter;
dbAdapter.open();
cursor = dbAdapter.fetchLimitRowsFromEpisode(
Common.currentListCountForEpi, EpisodeActivity.refId);
String[] columns = new String[] { DatabaseHelper.SUB_ID,
DatabaseHelper.EPI_FILEIMAGE_ID,
DatabaseHelper.EPI_TITLE, DatabaseHelper.EPI_DESC };
// the XML defined views which the data will be bound to
int[] to = new int[] { R.id.title, R.id.description };
adapter = new EpisodeCursorAdapter(
EpisodeActivity.context, R.layout.episode_row_view,
cursor, columns, to, Common.REFRESH);
adapter.changeCursor(cursor);
adapter.notifyDataSetChanged();
dbAdapter.close();
} catch (Exception e) {
e.printStackTrace();
}
}