我有一个listview
动态添加单选按钮。 但它表现得很怪异。在滚动后,radio buttons
显示在不同的行中,有些正在消失等。我在这里发布了适配器。请就如何做到这一点提出宝贵的建议。ListView与RadioButtons行为错误
public class CheckTentativePlanNSaveProductLoanEmiListAdapter extends ArrayAdapter<AddSanctionListRowDS> {
private final Activity mContext;
private final int mResource;
private final ArrayList<CheckTentativePlanNsaveListRowDs> mAddSanctionListRowDS;
private EditText mAddSanctionRowValueET;
private Calendar mMyCalendar;
private ArrayList<String> mProductDetailsScreenEnteredDetails;
public CheckTentativePlanNSaveProductLoanEmiListAdapter(Activity context, int resource, ArrayList<CheckTentativePlanNsaveListRowDs> iAddSanctionListRowObj, ArrayList<String> iAddSanctionEnteredValues)
{
super(context, resource);
this.mContext = context;
this.mResource = resource;
this.mAddSanctionListRowDS = iAddSanctionListRowObj;
//This depicts the values when entered or changed...
this.mProductDetailsScreenEnteredDetails = iAddSanctionEnteredValues;
}
@Override
public int getCount() {
return mAddSanctionListRowDS.size();
}
public View getView(final int position,View convertView,ViewGroup parent) {
SaveProductListViewHolder saveProductListViewHolderObj;
//View is creating...
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(mResource, parent, false);
saveProductListViewHolderObj = new SaveProductListViewHolder();
saveProductListViewHolderObj.key_tv = (TextView) convertView.findViewById(R.id.id_check_tentative_plan_n_save_product_non_loan_row_key_tv);
saveProductListViewHolderObj.value_et = (EditText) convertView.findViewById(R.id.id_check_tentative_plan_n_save_product_non_loan_value_et);
LinearLayout etParentLl = (LinearLayout) convertView.findViewById(R.id.id_check_tentative_plan_n_save_product_et_non_loan_rb_ll);
saveProductListViewHolderObj.et_parent_ll = etParentLl;
RadioGroup.OnCheckedChangeListener onCheckedChangeListener = new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
for (int i = 0; i < ((LinearLayout) group.getParent()).getChildCount(); i++) {
RadioGroup sisterRadioGroup = (RadioGroup) ((LinearLayout) group.getParent()).getChildAt(i);
if (group != sisterRadioGroup) {
sisterRadioGroup.setOnCheckedChangeListener(null);
sisterRadioGroup.clearCheck();
sisterRadioGroup.setOnCheckedChangeListener(this);
}
}
}
};
final CheckTentativePlanNsaveListRowDs currentRowDsObj = mAddSanctionListRowDS.get(position);
if (currentRowDsObj.getmRadioBtnData().size() > 0) {
//Set layout width = fill parent
//Layout height = wrap content
//Orientation will be vertical
LinearLayout radioGrpParentLl = new LinearLayout(mContext);
LinearLayout.LayoutParams paramsRadioGrpParentLl = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
radioGrpParentLl.setLayoutParams(paramsRadioGrpParentLl);
radioGrpParentLl.setOrientation(LinearLayout.VERTICAL);
float halfOfRadioCount = (float) currentRowDsObj.getmRadioBtnData().size()/2;
int noOfRadioGroupsNeeded = (int) Math.ceil(halfOfRadioCount);
for (int i = 0; i < noOfRadioGroupsNeeded; i++) {
final RadioGroup radioGroup = new RadioGroup(mContext);
RadioGroup.LayoutParams paramsRadio = new RadioGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
radioGroup.setOrientation(RadioGroup.HORIZONTAL);
radioGroup.setLayoutParams(paramsRadio);
//Add 2 Radio Buttons...
//Left Radio Button...
RadioButton leftRadioButtonInCurrentRadioGroup = new RadioButton(mContext);
RadioGroup.LayoutParams paramsLeftRadioBtnInCurrentRadioGroup = new RadioGroup.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT);
leftRadioButtonInCurrentRadioGroup.setLayoutParams(paramsLeftRadioBtnInCurrentRadioGroup);
paramsLeftRadioBtnInCurrentRadioGroup.weight = 1f;
leftRadioButtonInCurrentRadioGroup.setTypeface(Typeface.DEFAULT_BOLD);
leftRadioButtonInCurrentRadioGroup.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
//If checked, set the value...
int currentIndex = 0;
for (int i = 0; i < position; i++) {
if (currentRowDsObj.ismIsInputTypeNumeric() || currentRowDsObj.ismIsInputTypeText() || currentRowDsObj.ismIsDatePickerToBeShown()) {
currentIndex++;
}
if (currentRowDsObj.getmRadioBtnData().size() != 0) {
//A space allocated for unit...
currentIndex++;
}
}
if (currentRowDsObj.ismIsInputTypeText() || currentRowDsObj.ismIsInputTypeNumeric() || currentRowDsObj.ismIsDatePickerToBeShown()) {
//If current position has an allocation for entering value..
//Increment a space for that as well..
currentIndex++;
}
mProductDetailsScreenEnteredDetails.remove(currentIndex);
mProductDetailsScreenEnteredDetails.add(currentIndex, buttonView.getText().toString());
} else {
//Do nothing..
}
}
});
String leftRadionBtnLabel = currentRowDsObj.getmRadioBtnData().get(2 * i).getLabel();
leftRadioButtonInCurrentRadioGroup.setText(leftRadionBtnLabel);
radioGroup.addView(leftRadioButtonInCurrentRadioGroup);
if (((2 * i) + 1) < currentRowDsObj.getmRadioBtnData().size()) {
//At the end there is a chance that only one radio button has to be shown..
//The above condition avoids trying to add..
//Right Radio Button...
RadioButton rightRadioButtonInCurrentRadioGroup = new RadioButton(mContext);
RadioGroup.LayoutParams paramsRightRadioBtnInCurrentRadioGroup = new RadioGroup.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT);
rightRadioButtonInCurrentRadioGroup.setLayoutParams(paramsLeftRadioBtnInCurrentRadioGroup);
paramsRightRadioBtnInCurrentRadioGroup.weight = 1f;
rightRadioButtonInCurrentRadioGroup.setTypeface(Typeface.DEFAULT_BOLD);
String rightRadioBtnLabel = currentRowDsObj.getmRadioBtnData().get((2 * i) + 1).getLabel();
rightRadioButtonInCurrentRadioGroup.setText(rightRadioBtnLabel);
rightRadioButtonInCurrentRadioGroup.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
//If checked, set the value...
int currentIndex = 0;
for (int i = 0; i < position; i++) {
if (currentRowDsObj.ismIsInputTypeNumeric() || currentRowDsObj.ismIsInputTypeText() || currentRowDsObj.ismIsDatePickerToBeShown()) {
currentIndex++;
}
if (currentRowDsObj.getmRadioBtnData().size() != 0) {
//A space allocated for unit...
currentIndex++;
}
}
if (currentRowDsObj.ismIsInputTypeText() || currentRowDsObj.ismIsInputTypeNumeric() || currentRowDsObj.ismIsDatePickerToBeShown()) {
//If current position has an allocation for entering value..
//Increment a space for that as well..
currentIndex++;
}
mProductDetailsScreenEnteredDetails.remove(currentIndex);
mProductDetailsScreenEnteredDetails.add(currentIndex, buttonView.getText().toString());
} else {
//Do nothing..
}
}
});
radioGroup.addView(rightRadioButtonInCurrentRadioGroup);
radioGroup.setOnCheckedChangeListener(onCheckedChangeListener);
}
radioGrpParentLl.addView(radioGroup);
}
//Add RadioButton Group to the Parent Linear Layout..
saveProductListViewHolderObj.et_parent_ll.addView(radioGrpParentLl);
}
convertView.setTag(saveProductListViewHolderObj);
}else{
saveProductListViewHolderObj = (SaveProductListViewHolder)convertView.getTag();
}
final CheckTentativePlanNsaveListRowDs currentRowDsObj = mAddSanctionListRowDS.get(position);
saveProductListViewHolderObj.key_tv.setText(currentRowDsObj.getmEnterDetailsKey());
saveProductListViewHolderObj.value_et.setHint(currentRowDsObj.getmEnterDetailsValue());
saveProductListViewHolderObj.value_et.setFocusable(true);
//Based on the input type...
//Set the text change listener...
if(currentRowDsObj.ismIsInputTypeNumeric()){
saveProductListViewHolderObj.value_et.setInputType(InputType.TYPE_CLASS_NUMBER);
saveProductListViewHolderObj.value_et.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
int currentIndex = 0;
for(int i=0;i<position;i++){
if(currentRowDsObj.ismIsInputTypeNumeric()||currentRowDsObj.ismIsInputTypeText()||currentRowDsObj.ismIsDatePickerToBeShown()) {
currentIndex++;
}
if(currentRowDsObj.getmRadioBtnData().size()!=0) {
//A space allocated for unit...
currentIndex++;
}
}
mProductDetailsScreenEnteredDetails.remove(currentIndex);
mProductDetailsScreenEnteredDetails.add(currentIndex,s.toString());
}
});
}else if(currentRowDsObj.ismIsInputTypeText()){
saveProductListViewHolderObj.value_et.setInputType(InputType.TYPE_CLASS_TEXT);
saveProductListViewHolderObj.value_et.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
int currentIndex = 0;
for(int i=0;i<position;i++){
if(currentRowDsObj.ismIsInputTypeNumeric()||currentRowDsObj.ismIsInputTypeText()||currentRowDsObj.ismIsDatePickerToBeShown()) {
currentIndex++;
}
if(currentRowDsObj.getmRadioBtnData().size()!=0){
//A space allocated for unit...
currentIndex++;
}
}
mProductDetailsScreenEnteredDetails.remove(currentIndex);
mProductDetailsScreenEnteredDetails.add(currentIndex,s.toString());
}
});
}else if(currentRowDsObj.ismIsDatePickerToBeShown()){
//Here whether number or text...
//Doesnot matter...
saveProductListViewHolderObj.value_et.setInputType(InputType.TYPE_CLASS_NUMBER);
saveProductListViewHolderObj.value_et.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
//Calculate position dynamically here...
int currentIndex = 0;
for(int i=0;i<position;i++){
if(currentRowDsObj.ismIsInputTypeNumeric()||currentRowDsObj.ismIsInputTypeText()||currentRowDsObj.ismIsDatePickerToBeShown()) {
currentIndex++;
}
if(currentRowDsObj.getmRadioBtnData().size()!=0){
//A space allocated for unit...
currentIndex++;
}
}
mProductDetailsScreenEnteredDetails.remove(currentIndex);
mProductDetailsScreenEnteredDetails.add(currentIndex,s.toString());
}
});
}else{
saveProductListViewHolderObj.value_et.setVisibility(View.GONE);
}
return convertView;
}
static class SaveProductListViewHolder {
public TextView key_tv;
public EditText value_et;
public LinearLayout et_parent_ll;
}
}
'getView'方法太难遵循,因为它是...我强烈建议做一些重构(也许你自己在找它时发现错误) –
唯一的问题是我应该在哪里放置动态单选按钮到视图?应该在(convertView == null)内部还是外部? –