【问题标题】:Implement onClick listener in Listview adapter class在 Listview 适配器类中实现 onClick 监听器
【发布时间】:2019-07-30 04:30:19
【问题描述】:

我有一个显示 json 的 Activity,它的对象设置为 namelink。使用 getName() 向用户显示名称。现在我想实现 onClicklistener 以便新活动将根据位置和等效 url 打开,即 getLink() 相同位置并将其作为附加内容发送到下一个活动。

这是我的列表视图适配器

public class ListViewAdapter extends ArrayAdapter<Hero> {

    //the hero list that will be displayed
    private List<Hero> heroList;

    //the context object
    private Context mCtx;

    //here we are getting the herolist and context
    //so while creating the object of this adapter class we need to give herolist and context
    public ListViewAdapter(List<Hero> heroList, Context mCtx) {
        super(mCtx, R.layout.vault_list, heroList);
        this.heroList = heroList;
        this.mCtx = mCtx;
    }

    //this method will return the list item
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //getting the layoutinflater
        LayoutInflater inflater = LayoutInflater.from(mCtx);

        //creating a view with our xml layout
        View listViewItem = inflater.inflate(R.layout.vault_list, null, true);

        //getting text views
        Button btn_one = listViewItem.findViewById(R.id.btn_one);
     

        //Getting the hero for the specified position
        Hero hero = heroList.get(position);

        //setting hero values to textviews
        btn_one.setText(hero.getName());
       

        //returning the listitem
        return listViewItem;
    }

【问题讨论】:

标签: java android android-listview onclick


【解决方案1】:

如果我理解正确,你想让使用点击列表视图的项目并通过相关链接转到下一个活动。你可以实现 onItemclick 方法来实现你的功能。

【讨论】:

  • 自 startActivity(intent) 以来我如何打开新的活动;上面的适配器似乎不起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-20
  • 1970-01-01
  • 2015-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多