3

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

`

jmodine
  • 76
  • 5
Anwar Elsayed
  • 493
  • 3
  • 11

1 Answers1

1

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

Community
  • 1
  • 1
jmodine
  • 76
  • 5
  • but i can not use `OnItemClickListener()` inside my adapter for the share button i – Anwar Elsayed Jan 12 '16 at 15:30
  • ok then you will need a custom adapter in which you can implement the OnClickListneners this explains the process very well [custom adapter for buttons](http://stackoverflow.com/questions/16076985/listview-row-buttons-how-do-i-create-a-custom-adapter-that-connects-a-view-oncl) – jmodine Jan 12 '16 at 16:14
  • in may adapter i am all ready implement OnClickListneners() `public class ListViewAdapter extends ArrayAdapter implements OnClickListener { // Declare Variables Context context; LayoutInflater inflater; ` – Anwar Elsayed Jan 12 '16 at 16:32
  • Can you post your lnkModelList class so I can see how it is performing its actions? if it is giving wrong data, and you have a custom adapter then the problem is probably in how that class is executing its methods. Maybe how it interacts with your custom ListView. – jmodine Jan 12 '16 at 18:04
  • if lnkModelList is an implementation of your custom adapter then please post that class so we can see how it is implementing its methods – jmodine Jan 12 '16 at 18:11
  • i have updated my code see the lnkModel class now thanks for helping – Anwar Elsayed Jan 12 '16 at 18:42
  • Well the answer was probably in front of us the whole time. This is probably it. change this lnkModelList.get(position).getLink_desc()+"\n"+ lnkModelList.get(possition).getLink() to this lnkModelList.get(position).getLink_desc()+"\n"+ lnkModelList.get(position).getLink() It appears to be just a typo in position. Sorry about the late response I had to run out for a while – jmodine Jan 13 '16 at 00:24
  • thanks alot, but the problem is not solved after changing possition to position – Anwar Elsayed Jan 13 '16 at 10:15
  • what about in your constructor. you have position declared as a final int that may be holding bad data. you might try initializing that as being accessible in the method. – jmodine Jan 13 '16 at 10:26
  • public View getView( int position, View view, ViewGroup parent){ this.position=position; – jmodine Jan 13 '16 at 10:29
  • but if i remove final i notice that position access from inner class so it should be final and also if i implement OnClickListener and remove int the problem is exist for example i have ten list item on my screen when i click on the button of every item give me the right position but i scroll up and new ten item come again start to count from zero again – Anwar Elsayed Jan 13 '16 at 12:47
  • OK I understand but you are using position as a place holder not performing mutilating operations on the Int. Also I can't think of any reason a variable passed in should be final. even ones I didn't want to change because it prevents accessing them from inner tasks. When I see a final in a constructor like that its usually because I forgot something. Try this – jmodine Jan 13 '16 at 15:11
  • public Class something Extends something{ //define variables used by most of the methods // values they will share or will be constant through out // Int position // String value public something(int position, String value) this.value=value; this.position=position; – jmodine Jan 13 '16 at 15:14