在Listview适配器类中实现onClick侦听器

时间:2019-07-30 04:30:19

标签: java android android-listview onclick

我有一个活动,显示的json的对象设置为名称链接。使用 getName() 向用户显示名称。现在,我要实现onClicklistener,以便根据位置和等效网址(即相同位置的 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;
    }

1 个答案:

答案 0 :(得分:0)

如果我对您的理解正确,那么您想让我们单击列表视图中的项目,并转到具有相关链接的下一个活动。您可以实现onItemclick方法来实现您的功能。