Ich habe eine listview
, die Radio-Schaltflächen dynamisch hinzugefügt werden. Aber es verhält sich sehr fehlerhaft. Die radio buttons
werden nach dem Scrollen in verschiedenen Zeilen angezeigt, einige verschwinden usw. Ich habe den Adapter hier gepostet. Bitte geben Sie Ihre wertvollen Vorschläge dazu an.ListView mit RadioButtons Verhalten Buggy
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;
}
}
Diese 'getView'-Methode ist zu schwer zu folgen, wie es ist ... Ich empfehle dringend, einige Refactoring (vielleicht finden Sie den Fehler selbst dabei) –
Die einzige Frage ist, wo sollte ich den Zusatz von dynamischen platzieren Radio Buttons zur Ansicht? Sollte das innerhalb der (convertView == null) oder außerhalb sein? –