How to set onClick listener for ImageButton in ViewBinder?

I made a ListView with a SimpleCursorAdapter and a ViewBinder to set the views for it, and I want to put the ImageButton in the ViewBinder , but I donโ€™t know how to set the onClick event. Should I create a MySimpleCursorAdapter and put it there, or should I write it in the ViewBinder class?

Here is my code:

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

and ChannelPoster.java representing an entry in 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; } } 

and ChannelDB.java in the database, I publish only part:

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

and finally the list, 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); 

Here's how I add an entry if it helps:

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

If you need more code, just tell me.

+4
source share
2 answers

Edit:

Gather my previous answer. There was an error when scrolling down after switching your favorite star in a row. I suppose this is due to the way that views are revised or something like that.

Instead, I still pass the SQLite favorite column to from and the resource id of the ImageView star to . Then I extend SimpleCursorAdapter and @Override bindView . I call super and then get the ImageView descriptor with view.findViewById , where view is one of the parameters passed to bindView . Using this descriptor, I was able to conditionally set a suitable drawable (star filled or unfilled) and set clickListener .

Original answer:

My case is simpler, but it seems, so I will post what I did. I needed a star that would allow the user to like the string, so I used ImageView . In my from I pass in the SQLite favorite column, and in my to I pass in the resource id ImageView .

In my SimpleCursorAdapter.ViewBinder() , which I added using SimpleCursorAdapter.setViewBinder , I overloaded setViewValue . Then I use cursor.getColumnIndex("favorite") to check the index value passed to setViewValue . If equal, I set the click listener parameter to the view parameter passed to setViewValue . Depending on the value of favorite in my database, I correctly switch ImageView using ((ImageView) view).setImageResource() . Then I still in the listener update the value in the database (personally using OrmLite).

Not quite the way I wanted to do this, but easier than distributing the CursorAdapter and processing everything myself, and it seems to work.

0
source

You do not need to distribute the SimpleCursorAdapter, you can place the onClick event inside the ViewBinder class. Here is how I did it:

 private class MyViewBinder implements ViewBinder { @Override public boolean setViewValue(View view, Cursor cursor, int columnIndex) { if (columnIndex == cursor.getColumnIndex(COLUMN_NAME_CUSTOM)) { // If the column is COLUMN_NAME_CUSTOM then we use custom view. // The following two lines are needed so that you can still click // elsewhere in the list row to select it view.setFocusable(false); view.setFocusableInTouchMode(false); // Set your onClickListener view.setOnClickListener(new MyListener(someArgument)); return true; } // For other columns, simply return false so that the default binding happens. return false; } // Define your onclicklistener private class MyListener implements OnClickListener { private String someArg = null; // Constructor that lets you pass an argument to the listener public MyListener(String someArg){ this.someArg = someArg; } @Override public void onClick(View v) { //Handle your click event here } } } 
0
source

Source: https://habr.com/ru/post/1382116/


All Articles