1

我正在编写一个 android 应用程序来列出数据库中的项目,并将其中一些项目标记为收藏,并根据收藏更新数据库。

My Aim is

当我将 listView 的特定项目标记为收藏时,该特定列表项的图像将被更改,并且表的相应列应更新为收藏(是)或不收藏(否)。

从数据库中列出项目,根据收藏更改图像源,更新数据库也可以正常工作。

My Problem is

1.当我按下特定项目的图像时,图像会改变once,相应的数据库也会改变。但是,单击同一图像another time(上次单击图像)does not change anything(不更改图像也不更新)。

2 By scrolling the listView keep the listview as before changing the listView..

我参考这个链接beforeuse database。它工作正常。

使用数据库后,我将代码更改如下:

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

我知道我错过了一些东西。但我找不到那是什么。

请帮我解决这个问题。

先感谢您!

4

1 回答 1

0

正如 Yume117 所说,我调用了 notifyDataSetChanged() 方法。我已将我的答案更新如下:

refreshCursorAdapter();在下面的代码旁边添加了这一行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是:

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

    }
于 2013-06-21T13:12:47.303 回答