如何使用自定义适配器更改列表视图行上的按钮名称

时间:2015-04-21 05:28:20

标签: android android-activity android-listview

我有ListView。我在其中显示Button的列表,每行中有3个。我可以对Button上的点击做出反应就好了,但现在我需要在点击它后将Button的文本从“应用”更改为“已应用”。

我已在OnClickListener中实现了此功能,但现在当我点击Button时,它不仅会设置我点击的Button的文字,而且还会更改文字其他Button的。我不知道出了什么问题。

这是我的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;
    }

} 

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

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

} 

你可以像这样维护一个ArrayList:

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