0
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View itemView = inflater.inflate(R.layout.joinlistviewitem, parent, false);
    resultp = data.get(position);
    name = (TextView) itemView.findViewById(R.id.personname);
    name.setText(resultp.get(Joinlistview.RANK));   
    Button deletejoin=(Button)itemView.findViewById(R.id.delete);
    deletejoin.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            //resultp.get("userId");            
            // TODO Auto-generated method stub
            Log.e("delete clicked", "delete clicked");
            Toast.makeText(context,"clicked"+resultp.get(Joinlistview.RANK), Toast.LENGTH_SHORT).show();            
        }
    });

    return itemView;
}

I used the above code for button click on listview.If i click a button it sows the user name with its position. But it displays the same name in all position.??How to solve this??

Prasanna
  • 195
  • 2
  • 16

1 Answers1

1

Use getTag/setTag methods for getting current row value inside onClick as:

Button deletejoin=(Button)itemView.findViewById(R.id.delete);
deletejoin.setTag(resultp.get(Joinlistview.RANK));

And on Button Click:

Toast.makeText(context,"clicked "+ v.getTag().toString(),
                                   Toast.LENGTH_SHORT).show();
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • Joinlistview is a class and am accesing the string from that class..But it displays the name in the list correctly.when i click button it displays the same name – Prasanna Nov 13 '15 at 05:33
  • @Prasanna: See my answer – ρяσѕρєя K Nov 13 '15 at 05:38
  • @prosper Thanks for the answer. can u pls explain about this"deletejoin.setTag(resultp.get(Joinlistview.RANK));"? – Prasanna Nov 13 '15 at 05:45
  • @ρяσѕρєя K Thanks for the answer. can u pls explain about this"deletejoin.setTag(resultp.get(Joinlistview.RANK));"? – Prasanna Nov 13 '15 at 05:52
  • 1
    @ρяσѕρєяK I was the downvote because your initial answer was wrong. I've retracted it. Your current answer is how it should be done, and I've deleted my shortcut answer. – Mike M. Nov 13 '15 at 05:54
  • @Prasanna: See following post for more details [What is the main purpose of setTag() getTag() methods of View?](http://stackoverflow.com/questions/5291726/what-is-the-main-purpose-of-settag-gettag-methods-of-view) – ρяσѕρєя K Nov 13 '15 at 06:31