如何使用setTag获取Adapter中的String
我现在使用自定义 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论