这是我的ArrayAdapter类,
public class HAAR extends ArrayAdapter<HAM> {
Context context;
ArrayList<HAM> selectedhospital;
ViewHolder viewHolder = null;
public HAAR(Context context, int resourceId,
ArrayList<HAM> selectedhospital) {
super(context, resourceId, selectedhospital);
this.context = context;
this.selectedhospital = new ArrayList<HAM>();
this.selectedhospital.addAll(selectedhospital);
}
// View lookup cache
private static class ViewHolder {
TextView name;
TextView home;
CheckBox chk_hospitallist;
RadioButton rbt_primarylocation;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ArrayList<String> hospitalid = new ArrayList<>();
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
// If there's no view to re-use, inflate a brand new view for row
convertView = mInflater.inflate(R.layout.ha_list_item_name, null);
viewHolder = new ViewHolder();
viewHolder.name = (TextView) convertView.findViewById(R.id.txt_ha_name);
viewHolder.home = (TextView) convertView.findViewById(R.id.txt_ha_town);
viewHolder.chk_hospitallist = (CheckBox) convertView.findViewById(R.id.chk_box_ha_list);
final View finalConvertView = convertView;
viewHolder.chk_hospitallist.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
CheckBox cb = (CheckBox) v;
viewHolder.rbt_primarylocation = (RadioButton) finalConvertView.findViewById(R.id.rbtn_pl);
HAM _state = (HAM) cb.getTag();
if(cb.isChecked())
{
_state.setSelected(cb.isChecked());
hospitalid.add( _state.getId().toString());
viewHolder.rbt_primarylocation.setVisibility(View.VISIBLE);
Toast.makeText(
v.getContext(),
"Clicked on If: " + _state.getId().toString() + " is "
+ cb.isChecked(), Toast.LENGTH_LONG).show();
}
else
{
_state.setSelected(false);
hospitalid.remove( _state.getId().toString());
viewHolder.rbt_primarylocation.setVisibility(View.INVISIBLE);
Toast.makeText(
v.getContext(),
"Clicked on Else: " + _state.getId().toString() + " is "
+ cb.isChecked(), Toast.LENGTH_LONG).show();
}
}
});
// Cache the viewHolder object inside the fresh view
convertView.setTag(viewHolder);
} else {
// View is being recycled, retrieve the viewHolder object from tag
viewHolder = (ViewHolder) convertView.getTag();
}
HAM state = selectedhospital.get(position);
viewHolder.name.setText(state.getName());
viewHolder.home.setText(state.getAddress1()+"\r\n"+ state.getCity()+", "+state.getState()+" "+state.getZip());
viewHolder.chk_hospitallist.setChecked(state.isSelected());
viewHolder.chk_hospitallist.setTag(state);
return convertView;
}
}
在这段代码中,我试图在选中或取消选中Checkbox时隐藏并显示RadioButton。时间我也想在数组中添加或删除选中/未选中的复选框值。但是,当我运行此代码时,它无法正常工作。当我选择一个复选框(从第一个视图)时,另一个视图的单选按钮可见而不是第一个视图。还有一件事我还想要只选择一个单选按钮(当我选择了两个以上的复选框时)。
答案 0 :(得分:1)
请试试这个:
public class HAAR extends ArrayAdapter<HAM> {
Context context;
ArrayList<HAM> selectedhospital;
ArrayList<String> hospitalid = new ArrayList<String>();
int clickedRadioButtonPosition = -1;
ViewHolder viewHolder = null;
public HAAR(Context context, int resourceId, ArrayList<HAM> selectedhospital) {
super(context, resourceId, selectedhospital);
this.context = context;
this.selectedhospital = new ArrayList<HAM>();
this.selectedhospital.addAll(selectedhospital);
}
// View lookup cache
private static class ViewHolder {
TextView name;
TextView home;
CheckBox chk_hospitallist;
RadioButton rbt_primarylocation;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
// If there's no view to re-use, inflate a brand new view for row
convertView = mInflater.inflate(R.layout.ha_list_item_name, null);
viewHolder = new ViewHolder();
viewHolder.name = (TextView) convertView.findViewById(R.id.txt_ha_name);
viewHolder.home = (TextView) convertView.findViewById(R.id.txt_ha_town);
viewHolder.chk_hospitallist = (CheckBox) convertView.findViewById(R.id.chk_box_ha_list);
viewHolder.rbt_primarylocation = (RadioButton) convertView.findViewById(R.id.rbtn_pl);
viewHolder.chk_hospitallist.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
CheckBox cb = (CheckBox) v;
View finalConvertView = (View) v.getParent();
RadioButton rbt_primarylocation = (RadioButton) finalConvertView.findViewById(R.id.rbtn_pl);
int pos = (Integer) v.getTag();
HAM _state = (HAM) selectedhospital.get(pos);
if(cb.isChecked())
{
_state.setSelected(cb.isChecked());
// Update ArrayList
selectedhospital.remove(pos);
selectedhospital.add(pos, _state);
hospitalid.add( _state.getId().toString());
rbt_primarylocation.setVisibility(View.VISIBLE);
Toast.makeText(
v.getContext(),
"Clicked on If: " + _state.getId().toString() + " is "
+ cb.isChecked(), Toast.LENGTH_LONG).show();
}
else
{
_state.setSelected(false);
hospitalid.remove( _state.getId().toString());
// When unchecked the one with radio button selected
if(rbt_primarylocation.isChecked()){
if(hospitalid.size() > 0){
Toast.makeText(
v.getContext(),
"Please select an radio button", Toast.LENGTH_LONG).show();
}
}
rbt_primarylocation.setVisibility(View.INVISIBLE);
Toast.makeText(
v.getContext(),
"Clicked on Else: " + _state.getId().toString() + " is "
+ cb.isChecked(), Toast.LENGTH_LONG).show();
}
}
});
viewHolder.rbt_primarylocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickedRadioButtonPosition = (Integer) v.getTag();
notifyDataSetChanged();
}
});
// Cache the viewHolder object inside the fresh view
convertView.setTag(viewHolder);
} else {
// View is being recycled, retrieve the viewHolder object from tag
viewHolder = (ViewHolder) convertView.getTag();
}
HAM state = selectedhospital.get(position);
viewHolder.name.setText(state.getName());
viewHolder.home.setText(state.getAddress1()+"\r\n"+ state.getCity()+", "+state.getState()+" "+state.getZip());
viewHolder.chk_hospitallist.setChecked(state.isSelected());
// Set views for view recycling on scrolling
if(viewHolder.chk_hospitallist.isChecked()){
viewHolder.rbt_primarylocation.setVisibility(View.VISIBLE);
}else{
viewHolder.rbt_primarylocation.setVisibility(View.INVISIBLE);
}
if(position == clickedRadioButtonPosition){
viewHolder.rbt_primarylocation.setChecked(true);
}else{
viewHolder.rbt_primarylocation.setChecked(false);
}
viewHolder.chk_hospitallist.setTag(position);
viewHolder.rbt_primarylocation.setTag(position);
return convertView;
}
public String getRBClickedItem(){
// Not sure your list will change or not (e.g. from server)
// Instead of saving position, save something unique (e.g. Id).
return selectedhospital.get(clickedRadioButtonPosition).getId();
}
public ArrayList<String> getCheckedList(){
return hospitalid;
}
public void updateWithSavedList(ArrayList<String> savedList, String savedRBItemId){
hospitalid.clear();
hospitalid.addAll(savedList);
// Not sure your list will change or not (e.g. from server)
// Saved Id, so now search for its position.
if(!savedRBItemId.equals("")){
int tmpSearchPosition = -1;
for(int i=0; i<selectedhospital.size(); i++){
if(selectedhospital.get(i).getId().equals(savedRBItemId)){
tmpSearchPosition = i;
}
}
if(tmpSearchPosition != -1) clickedRadioButtonPosition = tmpSearchPosition;
}
notifyDataSetChanged();
}
}
希望这有帮助!
答案 1 :(得分:0)
我认为其中一个问题是由您在onClick()
中设置 viewHolder.rbt_primarylocation 引起的修复此问题应该使正确的radioButton可见