-1

I have a ListView. I am displaying a list of Buttons in it with 3 of them in each row. I can react to the clicks on the Buttons just fine, but now I need to change the text of a Button from "Apply" to "Applied" after clicking on it.

I have implemented this in the OnClickListener but now when I click on the Button it doesn't just set the text of the Button I clicked on, it also changes the text of other Buttons. I have no idea what's wrong.

This is my Adapter:

public class InteractiveFederalArrayAdapter extends
        ArrayAdapter<FederalProperties> {

    Activity context;
    List<FederalProperties> list;



    public InteractiveFederalArrayAdapter(Activity context,
            List<FederalProperties> list) {
        super(context, R.layout.federal_row, list);
        // TODO Auto-generated constructor stub

        this.context = context;
        this.list = list;
    }

    static class ViewHolder {

        protected TextView titleView;
        protected TextView subTitleView;
        protected Button phoneButton;
        protected Button emailButton;
        protected Button infoButton;

    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        View view = null;

        if (convertView == null) {

            LayoutInflater inflator = context.getLayoutInflater();
            view = inflator.inflate(R.layout.federal_row, null);

            final ViewHolder viewHolder = new ViewHolder();

viewHolder.titleView = (TextView)view.findViewById(R.id.titleId);
viewHolder.subTitleView = (TextView) view
                    .findViewById(R.id.subTitleId);
viewHolder.phoneButton = (Button) view
                    .findViewById(R.id.phoneButton);
viewHolder.emailButton = (Button) view
                    .findViewById(R.id.emailButton);
viewHolder.infoButton = (Button) view.findViewById(R.id.infoButton);
            view.setTag(viewHolder);

        } else {
            view = convertView;

        }

        final ViewHolder holder = (ViewHolder) view.getTag();

        holder.infoButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

           holder.infoButton.setText("Applied");
            }
        });

    holder.titleView.setText(list.get(position).getFederalTitle());
            holder.subTitleView.setText(list.get(position).getFederalSubTitle());

        return view;
    }

} 
Xaver Kapeller
  • 49,491
  • 11
  • 98
  • 86

1 Answers1

0

Try something like this:

    public class InteractiveFederalArrayAdapter extends
    ArrayAdapter<FederalProperties> {

Activity context;
List<FederalProperties> list;



public InteractiveFederalArrayAdapter(Activity context,
        List<FederalProperties> list) {
    super(context, R.layout.federal_row, list);
    // TODO Auto-generated constructor stub

    this.context = context;
    this.list = list;



}



    protected TextView titleView;
    protected TextView subTitleView;
    protected Button phoneButton;
    protected Button emailButton;
    protected Button infoButton;



@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    View view = null;



        LayoutInflater inflator = context.getLayoutInflater();
        view = inflator.inflate(R.layout.federal_row, null);



    titleView = (TextView)view.findViewById(R.id.titleId);
       subTitleView = (TextView) view
                .findViewById(R.id.subTitleId);
            phoneButton = (Button) view
                .findViewById(R.id.phoneButton);
               emailButton = (Button) view
                .findViewById(R.id.emailButton);
               infoButton = (Button) view.findViewById(R.id.infoButton);




     infoButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        infoButton.setText("Applied");
        }
    });

 titleView.setText(list.get(position).getFederalTitle());
        holder.subTitleView.setText(list.get(position).getFederalSubTitle());

    return view;
}

} 

you can maintain a ArrayList like this:

 infoButton.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    infoButton.setText("Applied");

    arraylist.set(position, "Applied");

    }
});



    btn.settext(arraylist.get(position));
Gagan
  • 745
  • 10
  • 31
  • but Holder class reduces the burden of creating each row every time right? – SANJEEV REDDY Apr 21 '15 at 05:34
  • you are right ,,,,,but it is more effective if your list view contains Images too.. – Gagan Apr 21 '15 at 05:35
  • ok let me try .but is it effective – SANJEEV REDDY Apr 21 '15 at 05:36
  • yes ,i got same problem ....without holder it works fine ,,as by using holder it was not creating every time ,so it not refresh row always,,but without holder it refresh each row – Gagan Apr 21 '15 at 05:38
  • no it is not changing when i click on first my last row button is changing – SANJEEV REDDY Apr 21 '15 at 05:41
  • you may need to maintain a arrayList which will tell weather a button is applied or apply...............it will contain "Apply" at very first time ......and a "if" condition in getview() method which will set text on button(apply or applied) and change arrayList value at that position. – Gagan Apr 21 '15 at 05:43
  • Can show me how did you write – SANJEEV REDDY Apr 21 '15 at 05:53