如何在Listview

时间:2016-04-18 12:47:52

标签: java android

我有lisview它包含radioButton和6 textView。我想一次只检查一个项目单选按钮。页面打开时检查默认,第一项单选按钮。如果我选择任何项目,则自动取消选中之前选中的项目.. 请有人帮帮我!

以下代码:

AdapterClass:

public class AddressCustomAdapter extends BaseAdapter implements ListAdapter {
    private final Context context;
    private final ArrayList<AddressResult> itemsArrayList;

    public AddressCustomAdapter(Context context, ArrayList<AddressResult> itemsArrayList) {
        this.context = context;
        this.itemsArrayList = itemsArrayList;

    }

    @Override
    public int getCount() {
        return itemsArrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return itemsArrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = convertView;
        final ViewHolder holder;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) ((Activity)context).getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.address_list,null);
            holder = new ViewHolder();

            holder.addrTypeTxt = (TextView)view.findViewById(R.id.addr);
            holder.line1Txt = (TextView)view.findViewById(R.id.addrLine1Txt);
            holder.line2Txt = (TextView)view.findViewById(R.id.addrLine2Txt);
            holder.areaTxt = (TextView)view.findViewById(R.id.addrAreaTxt);
            holder.cityTxt = (TextView)view.findViewById(R.id.addrCityTxt);
            holder.pincodeTxt = (TextView)view.findViewById(R.id.addrPincodeTxt);
           holder.radioButton = (RadioButton) view.findViewById(R.id.radioBtn);
            view.setTag(holder);
        }else{
            holder=(ViewHolder)view.getTag();
        }
        if(Utility.isNotNull(itemsArrayList.get(position).type)){
            holder.addrTypeTxt.setText(itemsArrayList.get(position).type);
        }else{
            Utility.textViewDisable(holder.addrTypeTxt);
        }

        if(Utility.isNotNull(itemsArrayList.get(position).line1)){
            holder.line1Txt.setText(itemsArrayList.get(position).line1);
        }else{
            Utility.textViewDisable(holder.line1Txt);
        }

        if(Utility.isNotNull(itemsArrayList.get(position).line2)){
            holder.line2Txt.setText(itemsArrayList.get(position).line2);
        }else{
            Utility.textViewDisable(holder.line2Txt);
        }

        if(Utility.isNotNull(itemsArrayList.get(position).area)){
            holder.areaTxt.setText(itemsArrayList.get(position).area);
        }else{
            Utility.textViewDisable(holder.areaTxt);
        }

        if(Utility.isNotNull(itemsArrayList.get(position).city)){
            holder.cityTxt.setText(itemsArrayList.get(position).city+"-");
        }else{
            Utility.textViewDisable(holder.cityTxt);
        }

        if(Utility.isNotNull(itemsArrayList.get(position).pincode)){
            holder.pincodeTxt.setText(itemsArrayList.get(position).pincode);
        }else{
            Utility.textViewDisable(holder.pincodeTxt);
        }

       return view;
    }

    public static class ViewHolder{
        public TextView addrTypeTxt;
        public TextView line1Txt;
        public TextView line2Txt;
        public TextView areaTxt;
        public TextView cityTxt;
        public TextView pincodeTxt;
        public RadioButton radioButton;
    }
}

提前致谢!!!

1 个答案:

答案 0 :(得分:0)

由于你的radiobutton不属于一个radiogroup,它们不是作为radiobutton工作,而是作为一个选择器。它违背了radiobutto的目的,所以我建议你重新审视一下这个设计。但是,作为实现所需功能的解决方法,您可能需要一些方法来跟踪当前选定的单选按钮,并在选择任何其他单选按钮后切换当前选定的按钮

因此,在您的活动或片段中定义一个名为currentradioselected

的变量
Circle

在您的radiobuttonclick听众

private RadioButton currentRadioButtonSelected

希望这有帮助