0

I created a ViewBinder, to hold my items in a ListView, which get its contents from a SimpleCursorAdapter and in this there's a ImageButton. I successfully get the list but the ImageButton just won't respond to my onclick event to bring a string from the data base to another Activity. That is the problem I have.

public class ChannelViewBinder implements SimpleCursorAdapter.ViewBinder {
    private ChannelDB mDB;
        public boolean setViewValue(View view, final Cursor cursor, int columnIndex) {
            final Context mContext = null;
                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(mDB.KEY_DBLINK));
                                Intent intent = new Intent();
                                intent.setClass(mContext, Doubanframe.class);
                                Bundle bunde = new Bundle();
                                bunde.putString("dblink",dblink);
                                intent.putExtras(bunde);
                                }
                            });

                }
                return false;
        }
}

and below is my MainActivity class:

private Button likebutton;
    private ImageButton about;
    private ChannelDB mDB;
    private ListView channellist;
    private Cursor c;


    @Override    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        likebutton=(Button) findViewById(R.id.share);
        about =(ImageButton)findViewById(R.id.about);
        channellist = (ListView) findViewById(R.id.Channel);

        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;

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

        channellist.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                c.moveToPosition(position);
                Intent intent = new Intent();
                intent.setClass(HDtvs.this,Showlist.class);
                intent .putExtra("path",mDB.KEY_PATH);
                intent .putExtra("cname",mDB.KEY_CHANNEL);
                intent .putExtra("dblink",mDB.KEY_DBLINK);
                startActivity(intent);
            }
        }); 
}
user
  • 86,916
  • 18
  • 197
  • 190
oratis
  • 818
  • 1
  • 9
  • 29

1 Answers1

2

In the onClickListener on your ImageButton you are creating a new Intent, but you don't seem to call startActivity? You will need to have a reference to some sort of context in order to be able to do so. Your mContext variable appears to be a good candidate for that, although I don't see it being set to anything but null in the code snippet.

You can easily add a reference to a valid Context by creating a constructor for your ChannelViewBinder class.

public class ChannelViewBinder implements SimpleCursorAdapter.ViewBinder {
    private Context mContext = null;

    public ChannelViewBinder(Context context) {
        mContext = context;
    }
...
}

You can then later on use it to call mContext.startActivity(intent) in the ImageButton's onClick. Obviously you will also need to change instantiating the object: adapter.setViewBinder(new ChannelViewBinder(this)), where this will reference your MainActivity class.

MH.
  • 45,303
  • 10
  • 103
  • 116
  • The answer is quite inspiring, I got the idea but don't know how to do it. how should I define that mContext to another activity,and how should I write the startActivity method in this non-activity class? Can you write me some sample plz?? – oratis Dec 02 '11 at 07:24
  • I added a code snippet illustrating how to get a Context reference in your custom ViewBinder. Hope that helps! – MH. Dec 02 '11 at 07:37
  • Thx,but that didn't work out as expected,I tapped on the button, nothing happened,I guess it's coz the cursor in ChannelViewBinder didn't get the right string but null, can you help me with that too? thx – oratis Dec 02 '11 at 09:00
  • You should first tidy up your code a bit and then breakpoint it to see until where things work as expected. Also keep an eye out for relevant information in LogCat. For starters I would add another `return true;` in the setValueView's second `if`. You go from there. :) – MH. Dec 02 '11 at 09:21