-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
  • 48,336
  • 11
  • 94
  • 85

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
  • 735
  • 10
  • 31