android - 如何在 ViewBinder 中为 ImageButton 设置 onClick 监听器?

标签 android android-listview simplecursoradapter android-viewbinder

我用 SimpleCursorAdapterViewBinder 制作了一个 ListView 来为它设置 View ,我想放一个 ImageButton ViewBinder 但不知道如何设置 onClick 事件。我应该创建一个 MySimpleCursorAdapter 并将它放在那里,还是应该将它写在 ViewBinder 类中?

这是我的代码:

ViewBinder.java:

public class ChannelViewBinder implements SimpleCursorAdapter.ViewBinder {
        public boolean setViewValue(View view, final Cursor cursor, int columnIndex) {

                if(view instanceof ImageView) {
                        ImageView iv = (ImageView) view;
                        byte[] img = cursor.getBlob(columnIndex);
                        iv.setImageBitmap(BitmapFactory.decodeByteArray(img, 0, img.length));
                        return true;
                }

                if(view instanceof ImageButton) {
                        ImageButton ib = (ImageButton) view;
                        ib.setOnClickListener(new  View.OnClickListener() {     
                            @Override
                            public void onClick(View v) { 
                                String dblink = cursor.getString(cursor.getColumnIndex(ChannelDB.KEY_DBLINK));
                                Intent intent = new Intent();

                                Bundle bundle = new Bundle();
                                bundle.putString("dblink",dblink);
                                intent.putExtras(bundle);
                                }
                            });

                }
                return false;
        }
}

ChannelPoster.java 表示 ListView 中的条目:

public class ChannelPoster {
    private Bitmap poster;
    private String channel;
    private String path;
    private String dblink;

    public ChannelPoster(Bitmap pi, String c, String p, String d) {
        poster = pi;
        channel = c;
        path = p;
        dblink = d;
    }

    public Bitmap getPoster() { return poster; }
    public String getChannel() { return channel; }
    public String getPath() { return path; }
    public String getDBlink() { return dblink; }
}

ChannelDB.java数据库一,我只贴相关部分:

public void createchannelEntry(ChannelPoster channel) {
        openDB();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        channel.getPoster().compress(Bitmap.CompressFormat.PNG, 100, out);
        ContentValues cv = new ContentValues();
        cv.put(KEY_POSTER, out.toByteArray());            
        cv.put(KEY_CHANNEL, channel.getChannel());
        cv.put(KEY_DBLINK, channel.getDBlink());
        cv.put(KEY_PATH, channel.getPath());
        mDb.insert(channelS_TABLE, null, cv);
        closeDB();
    }

最后是列表,Tv.java:

ListView channellist = (ListView) findViewById(android.R.id.list);
        mDB = new ChannelDB(this);

        String[] columns = {mDB.KEY_ID, mDB.KEY_POSTER, mDB.KEY_CHANNEL, mDB.KEY_PATH, mDB.KEY_DBLINK};
        String   table   = mDB.channelS_TABLE;

        Cursor c = mDB.getHandle().query(table, columns, null, null, null, null, null);

        startManagingCursor(c);

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                R.layout.channelview,
                c,
                new String[] {mDB.KEY_POSTER, mDB.KEY_CHANNEL, mDB.KEY_DBLINK},
                new int[] {R.id.poster, R.id.channel, R.id.douban});

        adapter.setViewBinder(new ChannelViewBinder());

        channellist.setAdapter(adapter);

这是我添加条目的方式,如果有帮助的话:

  mDB.createchannelEntry(new ChannelPoster(image, "name" ,"link"  ,"link" ));

如果您需要更多代码,请告诉我。

最佳答案

编辑:

划掉我之前的回答。在连续切换最喜欢的星星后向下滚动时出现错误。我想这与 View 的回收方式有关。

相反,我仍然传递我的 SQLite 列 favoritefrom , 和 resource id之星ImageViewto .然后我extend SimpleCursorAdapter , 和 @Override bindView .我调用super ,然后获取 ImageView 的句柄使用 view.findViewById , 与 view作为传递给 bindView 的参数之一.使用该句柄,我能够有条件地设置适当的可绘制对象(星形填充或未填充),并设置 clickListener .

原答案:

我的案例比较简单,但也很相似,所以我会发布我所做的。我需要一个星标来让用户收藏一行,所以我使用了 ImageView。 .在我的 from我通过了 SQLite 列 favorite , 在我的 to我通过 resource IdImageView .

在我的 SimpleCursorAdapter.ViewBinder() ,这是我使用 SimpleCursorAdapter.setViewBinder 添加的, 我覆盖了 setViewValue .然后我使用 cursor.getColumnIndex("favorite")针对 index 进行测试传递给 setViewValue 的值.如果相等,我设置一个 click listenerview传递给 setViewValue 的参数.取决于 favorite 的值在我的数据库中,我切换 ImageView适本地,使用 ((ImageView) view).setImageResource() .然后,我仍然在监听器中更新数据库中的值(个人使用 OrmLite)。

这不是我想要的,但比扩展 CursorAdapter 和自己处理所有事情更容易,而且它似乎有效。

关于android - 如何在 ViewBinder 中为 ImageButton 设置 onClick 监听器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8199550/

相关文章:

java - 在我的 WebView 中显示网站

java - 如何在我的 GridView 中包含 SearchView?

java - 具有多个文本和图像的 ListView

android - 如何使用 SimpleCursorAdapter 使 ListView 跳过 ""文本?

java - android.database.Cursor IndexOutOfBounds : Exception Index -1 requested, 大小为 6

Android:无限滚动 - ListView 和 Cursor

安卓声音合成

android - 在 Task<T> 完成之前暂停协程的正确方法

java - 在从android数据库加载的 ListView 中搜索

android - 如何在 android 中制作像 flipkart 这样的过滤器选项卡?