vendredi 22 mai 2015

i can't figure out where i am missing in code

I am new in Android development. This is my code actually what it does it generates a list and with the Edit text box it search specific record in the list. But when i search in the list it gives me the record but when i change its values from Replace to Repair or Repair to Replace it successfully change in Array at back end but user interface doesn't changes.

Code:

public class listViewAdapter_clmform_prtLst extends
    ArrayAdapter<PartList_Holder> {

private ArrayList<PartList_Holder> originalList;
private ArrayList<PartList_Holder> countryList;
private CountryFilter filter;
int index;
Context mcontext;
ViewHolder holder;
String search = "0";

public listViewAdapter_clmform_prtLst(Activity context,
        int rowQuoteapprovel, ArrayList<PartList_Holder> countryList) {
    super(context, rowQuoteapprovel, countryList);

    this.mcontext = context;
    this.countryList = new ArrayList<PartList_Holder>();
    this.countryList.addAll(countryList);
    this.originalList = new ArrayList<PartList_Holder>();
    this.originalList.addAll(countryList);

}

@Override
public Filter getFilter() {
    if (filter == null) {
        filter = new CountryFilter();
    }
    return filter;
}

class ViewHolder {
    TextView partname;
    CheckBox repair;
    CheckBox replace;
}

@SuppressLint("CutPasteId")
@Override
public View getView(final int position, View convertView,
        final ViewGroup parent) {

    holder = null;
    if (convertView == null) {

        LayoutInflater vi = (LayoutInflater) mcontext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = vi.inflate(R.layout.row_quoteapprovel, null);

        holder = new ViewHolder();
        holder.partname = (TextView) convertView
                .findViewById(R.id.tv_partname);
        holder.repair = (CheckBox) convertView
                .findViewById(R.id.CheckBox01);
        holder.replace = (CheckBox) convertView
                .findViewById(R.id.CheckBox02);

        convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    final PartList_Holder country = countryList.get(position);

    final CheckBox repair = (CheckBox) convertView
            .findViewById(R.id.CheckBox01);
    final CheckBox replace = (CheckBox) convertView
            .findViewById(R.id.CheckBox02);

    repair.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            ind(country.getPartName());
            holder.repair.setChecked(true);
            Constants.repair[index] = "true";
            holder.replace.setChecked(false);
            Constants.replace[index] = "false";
            // notifyDataSetChanged();

        }
    });
    replace.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            ind(country.getPartName());
            holder.replace.setChecked(true);
            Constants.replace[index] = "true";
            holder.repair.setChecked(false);
            Constants.repair[index] = "false";
            // notifyDataSetChanged();
        }
    });

    holder.partname.setText(country.getPartName());

    if (country.getRepair().equals("true")) {
        holder.repair.setChecked(true);
    } else {
        holder.repair.setChecked(false);
    }
    if (country.getReplace().equals("true")) {
        holder.replace.setChecked(true);
    } else {
        holder.replace.setChecked(false);
    }

    return convertView;
}

public void ind(String text) {
    index = -1;
    for (int i = 0; i < Constants.parts.length; i++) {
        if (Constants.parts[i].equals(text)) {
            index = i;
            break;
        }
    }

}

private class CountryFilter extends Filter {
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {

        constraint = constraint.toString().toLowerCase();
        search = (String) constraint;
        FilterResults result = new FilterResults();
        if (constraint != null && constraint.toString().length() > 0) {
            ArrayList<PartList_Holder> filteredItems = new ArrayList<PartList_Holder>();

            for (int i = 0, l = originalList.size(); i < l; i++) {
                PartList_Holder country = originalList.get(i);
                if (country.toString().toLowerCase().contains(constraint))
                    filteredItems.add(country);
            }
            result.count = filteredItems.size();
            result.values = filteredItems;
        } else {
            synchronized (this) {
                result.values = originalList;
                result.count = originalList.size();
            }
        }
        return result;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence constraint,
            FilterResults results) {

        countryList = (ArrayList<PartList_Holder>) results.values;
        notifyDataSetChanged();
        clear();
        for (int i = 0, l = countryList.size(); i < l; i++)
            add(countryList.get(i));
        notifyDataSetInvalidated();

    }
}
}

Before Search Look the first Row Values of Repair and Replace enter image description here

If I search for the First Value it shows successfully and i can check and unchecked radio buttons.

enter image description here

But i go back means clearing the characters from Edittext view it show previous values

enter image description here

Aucun commentaire:

Enregistrer un commentaire