在列表视图上设置Radiobutton true onItemclick

时间:2016-07-12 06:29:00

标签: java android listview

我想在radio button listview中标记onItemClick为真,所以我正在做的是

@Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                LinearLayout item_view = (LinearLayout) view;
                final RadioButton itemcheck = (RadioButton) item_view
                        .findViewById(R.id.listview_radiobutton);

                if (itemcheck.isChecked()) {
                    itemcheck.setChecked(true);
                } else {
                    itemcheck.setChecked(false);
                }

                itemcheck.setChecked(true);

            }

我的listview

<ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_marginLeft="@dimen/view_margin_15"
        android:layout_marginRight="@dimen/view_margin_15"
        android:layout_marginTop="@dimen/view_margin_20"
        android:layout_weight="1"
        android:choiceMode="singleChoice"
        android:divider="@drawable/list_divider"
        android:dividerHeight="@dimen/padding_2"
        android:fastScrollEnabled="true"
        android:footerDividersEnabled="true"
        android:listSelector="@null"
        android:scrollbars="none"
        android:textFilterEnabled="true"
        android:textStyle="normal" />

修改: -

我的适配器代码: -

public class Adapter extends ArrayAdapter<Details> {

    private Context mContext;
    private List<Details> transList;
    private LayoutInflater infalInflater;
    private OnCheckedRadioButon onCheckedRadioButon;
    private Typeface mTypeface, mEditTypeface, mPasswdTypeface;
    private int mSelectedPosition = -1;
    private RadioButton mSelectedRB;
    private PromoViewHolder viewHolder;
    public Adapter(Context context, List<Details> mtransList, OnCheckedRadioButon onCheckedRadioButon) {
        super(context, R.layout.dialog_listview, mtransList);
        this.mContext = context;
        this.transList = mtransList;
        this.onCheckedRadioButon = onCheckedRadioButon;
        this.infalInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

         }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        View row = convertView;

        try {
             viewHolder = null;

            row = infalInflater.inflate(R.layout.dialog_listview_code, parent, false);

            viewHolder = new ViewHolder();

            viewHolder.radiobutton = (RadioButton) row.findViewById(R.id.radiobutton);
            viewHolder.listview_name = (TextView) row.findViewById(R.id.listview_name);

            setValueText(viewHolder, position);
            viewHolder.radiobutton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
                    if (checked) {
                        onCheckedRadioButon.onCheckedButton(transList.get(position));
                    }
                }
            });

            viewHolder.radiobutton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    if (position != mSelectedPosition && mSelectedRB != null) {
                        mSelectedRB.setChecked(false);
                    }

                    mSelectedPosition = position;
                    mSelectedRB = (RadioButton) v;
                }
            });


            if (mSelectedPosition != position) {
                viewHolder.radiobutton.setChecked(false);
            } else {
                viewHolder.radiobutton.setChecked(true);
                if (mSelectedRB != null && viewHolder.radiobutton != mSelectedRB) {
                    mSelectedRB = viewHolder.radiobutton;
                }
            }


            row.setTag(viewHolder);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return row;

    }

    private void setValueText(ViewHolder viewHolder, final int position) {
        viewHolder.listview_name.setText(transList.get(position).getName());
    }

    public interface OnCheckedRadioButon {
        void onCheckedButton(Details pr);
    }

    class ViewHolder {
        RadioButton radiobutton;
        TextView listview_name;
    }

}

它正常工作,但如果我点击listview的其他位置,则不会取消选中之前的radiobutton位置。我想取消选中之前的所有位置,并且一次只标记一个。 任何帮助将不胜感激。

4 个答案:

答案 0 :(得分:4)

使用POJO类(Setter或Getter)来管理这种类型的条件。在该类中使用boolean变量,并根据位置true或false更改其值。

POJO班赞:

public class CheckListSource {       
    public boolean isSelected;

    public boolean isSelected() {
            return isSelected;
    }

    public void setSelected(boolean selected) {
            isSelected = selected;
    }        

}

在适配器中:

private ArrayList<CheckListSource > itemsData;

public ChildListAdapter(Activity activity, ArrayList<ChildListResponse> baseResponse) {
    this.itemsData = baseResponse;
    this.activity = activity;
}

在BindViewHolder中赞:

viewHolder.checkParentView.setTag(itemsData.get(position));
viewHolder.checkParentView.setOnClickListener(checkedListener);

    private View.OnClickListener checkedListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        CheckListSource childListResponse = (CheckListSource ) v.getTag();
        if (childListResponse.isSelected())
            childListResponse.setSelected(false);
        else
            childListResponse.setSelected(true);
        notifyDataSetChanged();
    }
};

答案 1 :(得分:0)

boolean中使用Activity数组。每个boolean值对应一个RadioButton

如果选中RadioButton,请将其boolean值设置为true,并将数组中的所有其他boolean值设置为false

getView()的{​​{1}}中,拨打Adapter

修改your_radio_button.setChecked(your_boolean_array[position])数组后,请致电boolean

答案 2 :(得分:0)

Checkout this它对我有用..

public class ProgramAdapter extends ArrayAdapter<KeyInformation> {
    private final String TAG = "ProgramAdapter";
    private List<KeyInformation> mList;
    private Context mContext;
    private LayoutInflater mInflater;
    private int mSelectedPosition = -1;
    private RadioButton mSelectedRB;
    private String mUserApllication = "";

    public ProgramAdapter(Context context, List<KeyInformation> objects) {
        super(context, R.layout.program_item, objects);
        mContext = context;
        mList = objects;
        mInflater = ( LayoutInflater ) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mUserApllication = Settings.System.getString(mContext.getContentResolver(),
                Settings.System.PROGRAMMABLE_KEY_ACTION);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.program_item, parent, false);
            holder.mImageView = (ImageView) convertView.findViewById(R.id.icon);
            holder.mTextView = (TextView) convertView.findViewById(R.id.text);
            holder.mSeparator = (TextView) convertView.findViewById(R.id.title_separator);
            holder.mRadioButton = (RadioButton) convertView.findViewById(R.id.radioBtn);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.mRadioButton.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(position != mSelectedPosition && mSelectedRB != null){
                    mSelectedRB.setChecked(false);
                }
                mUserApllication ="";
                mSelectedPosition = position;
                mSelectedRB = (RadioButton) view;
            }
        });
        String userApp = mList.get(position).packageName;
        if(mUserApllication.equals(userApp)) {
            mSelectedPosition = position;
        }

        if (mSelectedPosition != position) {
            holder.mRadioButton.setChecked(false);
        } else {
            holder.mRadioButton.setChecked(true);
            mSelectedRB = holder.mRadioButton;
        }
        holder.mImageView.setImageDrawable(mList.get(position).icon);
        holder.mTextView.setText(mList.get(position).lable);
        if (position == 5) {
            holder.mSeparator.setVisibility(View.VISIBLE);
        } else {
            holder.mSeparator.setVisibility(View.GONE);
        }

        return convertView;
    }

    public int getmSelectedPosition () {
        return mSelectedPosition;
    }
    private static class ViewHolder {
        ImageView mImageView;
        TextView mTextView;
        TextView mSeparator;
        RadioButton mRadioButton;
    }
}

答案 3 :(得分:0)

请在下面查看,它会有效。

    class Details{

    public boolean isSelect=false;
    }
     public class Adapter extends ArrayAdapter<Details> {

    private Context mContext;
    private List<Details> transList;
    private LayoutInflater infalInflater;
    private OnCheckedRadioButon onCheckedRadioButon;
    private Typeface mTypeface, mEditTypeface, mPasswdTypeface;
    private int mSelectedPosition = -1;
    private RadioButton mSelectedRB;
    private PromoViewHolder viewHolder;
    public Adapter(Context context, List<Details> mtransList, OnCheckedRadioButon onCheckedRadioButon) {
        super(context, R.layout.dialog_listview, mtransList);
        this.mContext = context;
        this.transList = mtransList;
        this.onCheckedRadioButon = onCheckedRadioButon;
        this.infalInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

         }
    public void updateList(){
        viewHolder.radiobutton.setChecked(false);
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        View row = convertView;

        try {
             viewHolder = null;

            row = infalInflater.inflate(R.layout.dialog_listview_code, parent, false);

            viewHolder = new ViewHolder();

            viewHolder.radiobutton = (RadioButton) row.findViewById(R.id.radiobutton);
            viewHolder.listview_name = (TextView) row.findViewById(R.id.listview_name);

            setValueText(viewHolder, position);
            viewHolder.radiobutton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
                    if (checked) {
                        onCheckedRadioButon.onCheckedButton(transList.get(position));
                    }
                }
            });

            viewHolder.radiobutton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                Details detail=transList.get(position);
            for(int i=0;i<transList.size;i++){
                Detail b=transList.get(i);
                b.isSelect=false;
            }
            detail.isSelect=true;
            adapter.notifydatasetchange();
                }
            });

        Details detail=transList.get(position);
            if (detail.isSelect) {
                viewHolder.radiobutton.setChecked(true);
            } else {
                viewHolder.radiobutton.setChecked(false);
            }


            row.setTag(viewHolder);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return row;

    }

    private void setValueText(ViewHolder viewHolder, final int position) {
        viewHolder.listview_name.setText(transList.get(position).getName());
    }

    public interface OnCheckedRadioButon {
        void onCheckedButton(PromoDetails promoDetails);
    }

    class ViewHolder {
        RadioButton radiobutton;
        TextView listview_name;
    }

}