如何使用setTag在Adapter中获取String

时间:2011-12-07 03:50:15

标签: android listview button adapter

我现在使用自定义simplecursoradapter将设置数据设置为listview,在listview的每个条目中都有一个按钮,我想为每个条目分配一个url,所以当你单击它时,跳转到另一个webview活动使用网址,但我不知道如何获取适配器中的网址,任何人都可以解释它是如何工作的?使用示例代码片段将是完美的

我的适配器:

public class ChannelAdapter extends SimpleCursorAdapter{

    private LayoutInflater mInflater;
    private List<ChannelPoster> items;
    private Context mContext;
    private String dblink;

    public ChannelAdapter(Context context, int layout, Cursor c, String[] from,
            int[] to) {
        super(context, layout, c, from, to);
        // TODO Auto-generated constructor stub
    }

    @Override
    public View getView(int position,View convertView,ViewGroup par)
    {
      ViewHolder holder;

      if(convertView == null)
      {

        convertView = mInflater.inflate(R.layout.channelview, null);

        holder = new ViewHolder();
        holder.image = (ImageView) convertView.findViewById(R.id.poster);
        holder.text = (TextView) convertView.findViewById(R.id.channel); 
        holder.button = (ImageButton) convertView.findViewById(R.id.douban);
        holder.button.setTag(curItem.url);
        holder.button.setOnClickListener(new OnClickListener() {

         @Override

         public void onClick(View view) {
          String url = (String) view.getTag();
          Intent myIntent = new Intent(view.getContext(), Doubanframe.class);
          myIntent.putExtra("dblink", dblink);
          view.getContext().startActivity(myIntent);
         }
        });
        convertView.setTag(holder);

      }
      else
      {
        holder = (ViewHolder) convertView.getTag();
      }

      ChannelPoster tmpN=(ChannelPoster)items.get(position);

      holder.text.setText(tmpN.getChannel());
      holder.image.setImageResource(tmpN.getPoster());
      holder.button.setTag(curItem.url);
      holder.button.setOnClickListener(new OnClickListener() {

       @Override

       public void onClick(View view) {
        String url = (String) view.getTag();
        Intent myIntent = new Intent(view.getContext(), Doubanframe.class);
        myIntent.putExtra("dblink", dblink);
        view.getContext().startActivity(myIntent);
       }
      });

      return convertView;
    }


    private class ViewHolder
    {
    ImageView image;
    TextView text;
    ImageButton button;
    }
  }

这是我使用它的方式

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

0 个答案:

没有答案