2011-12-02 67 views
0

我创建了一个ViewBinder,将我的物品放在ListView中,该物品的内容来自SimpleCursorAdapter,并且在此处有ImageButton。我成功地获得了该列表,但ImageButton只是不会响应我的onclick事件,从数据库中将字符串带到另一个Activity。这是我的问题。ViewBinder中的ImageButton不起作用

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

以下是我MainActivity类:

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

回答

2

在onClickListener您ImageButton要创建一个新的Intent,但你似乎并不叫startActivity?为了能够这样做,您需要参考某种上下文。你的mContext变量似乎是一个很好的候选人,虽然我没有看到它被设置为代码片段中的null

通过为您的ChannelViewBinder类创建构造函数,您可以轻松地添加对有效上下文的引用。

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

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

您可以再后来就用它打电话mContext.startActivity(intent)中的ImageButton的onClick。很明显,您还需要更改实例化对象:adapter.setViewBinder(new ChannelViewBinder(this)),其中this将引用您的MainActivity类。

+0

答案很鼓舞人心,我明白了,但不知道该怎么做。我应该如何将mContext定义到另一个活动,以及如何在这个非活动类中编写startActivity方法?你能写一些样品吗? – oratis

+0

我添加了一段代码片段,说明如何在自定义ViewBinder中获取上下文引用。希望有所帮助! –

+0

Thx,但没有达到预期的效果,我点击了按钮,没有发生任何事情,我猜这是因为ChannelViewBinder中的光标没有得到正确的字符串,但为空,你能帮我解决吗? thx – oratis