Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listview and how to control items inside it with a button

Tags:

android

I am trying to make a list and inside it I put a button.

This list consists of the button and two textviews.

I use this button to share the two textviews but when I click it shows me different data for different items on the list.

Here's the code:

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

the lnkModel class-->

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

`

like image 772
Anwar Elsayed Avatar asked Jan 26 '26 00:01

Anwar Elsayed


1 Answers

I believe your problem is going to be the use of OnClickListener. From what I see it looks like you need to be using OnItemSelected Instead. If that isn't your problem you may need to create a custom adapter that will implement the OnClickListener for the button. This Answers how to do that nicely. One of these solutions should fix your problem

like image 115
jmodine Avatar answered Jan 28 '26 13:01

jmodine



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!