Listview以及如何使用按钮控制其中的项目

时间:2016-01-12 15:20:13

标签: android

我正在尝试制作一个列表,在里面我放了一个按钮。

此列表包含按钮和两个textview。

我使用此按钮分享两个文本视图,但是当我点击它时,会显示列表中不同项目的不同数据。

以下是代码:

 public View getView(final int position, View view, ViewGroup parent) {

    final ViewHolder holder;

    if (view == null) {
        view = inflater.inflate(R.layout.home_page_custom_layout ,parent,false);
        holder = new ViewHolder();
        // Locate the TextViews in listview_item.xml
        holder.user = (TextView) view.findViewById(R.id.User_txt);
        holder.link = (TextView) view.findViewById(R.id.lnk_txt);
        holder.time= (TextView) view.findViewById(R.id.time_txt);
        holder.desc= (TextView) view.findViewById(R.id.link_desc_textview);
        holder.like= (ImageView) view.findViewById(R.id.like_imageView);
        holder.share= (ImageView) view.findViewById(R.id.share_imageview);
        holder.like.setOnClickListener(this);
        //***shre item content
        view.setTag(holder);
        holder.like.setTag(Integer.valueOf(position));
        holder.share.setTag(Integer.valueOf(position));
        holder.link.setTag(Integer.valueOf(position));
        holder.desc.setTag(Integer.valueOf(position));
        holder.share.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent shareIntent = new Intent(Intent.ACTION_SEND);
                shareIntent.setType("text/plain");
                shareIntent.putExtra(Intent.EXTRA_TEXT,
                        lnkModelList.get(position).getLink_desc()+"\n"+
                                lnkModelList.get(possition).getLink()
                                +"\n"+"#LNKAPP");
                context.startActivity(Intent.createChooser(shareIntent,"Share LNK wite People"));
            }
        });

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

    // Set the results into TextViews
    holder.user.setText(lnkModelList.get(position).getUserName());
    holder.link.setText(lnkModelList.get(position).getLink());
    holder.time.setText(lnkModelList.get(position).getTime());
    holder.desc.setText(lnkModelList.get(position).getLink_desc());

    return view;
}

lnkModel类 - >

public class LnkModel {

       private String userName;
       private String link;
       private String time ;
       private String link_desc;

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public void setLink(String link) {
        this.link = link;
    }

    public String getLink_desc() {
        return link_desc;
    }

    public void setLink_desc(String link_desc) {
        this.link_desc = link_desc;
    }

    public String getUserName() {
        return userName;
    }

    public String getLink() {
        return link;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    } 
}

`

1 个答案:

答案 0 :(得分:1)

我相信你的问题将是使用OnClickListener。从我看来,你看起来需要使用OnItemSelected。如果这不是您的问题,您可能需要创建一个自定义适配器,该适配器将为按钮实现OnClickListener。 This Answers how to do that nicely。其中一个解决方案应该解决您的问题