如何使用setTag获取Adapter中的String

发布于 2024-12-19 16:33:40 字数 2960 浏览 2 评论 0原文

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

我的适配器:

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

I am now using a custom simplecursoradapter to get set data to a listview, in every entry of the listview there is a button and I want to assign a url to each of them so when you click it, jump to another webview activity with the url, but I don't know how to get the url in the adapter, can anyone explain how it works? with an example code snippet would be perfect

My adapter:

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

This is how I use it

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文