I have a listview and an editText to filter data. It filters data correctly but e.g, if I filter list and open filtered item it shows me the data of item which was at index 0 of original list not that of filtered item.
My Adapter class:
public class Patient_Adapter extends BaseAdapter implements Filterable{
Activity context;
String[] name, age, number, dip_rate;
//for filtering use arraylists
LayoutInflater inflater;
private ValueFilter valueFilter;
ArrayList<ArrayList_get_set> arrayList;
private ArrayList<ArrayList_get_set> StringFilterList;
public Patient_Adapter(Activity applicationContext, ArrayList<ArrayList_get_set> arrayList) {
this.context = applicationContext;
/////
this.arrayList = arrayList;
StringFilterList = arrayList;
this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
getFilter();
}
@Override
public int getCount() {
return arrayList.size();
}
@Override
public Object getItem(int i) {
return arrayList.get(i);
}
@Override
public long getItemId(int i) {
return 0;
}
private class Viewholder{
TextView name_
,date_
,number_
,dip_rate_
,tv_request_number;
Button button;
}
@Override
public View getView(final int position, View view, final ViewGroup viewGroup) {
Viewholder viewholder;
LayoutInflater inflater = context.getLayoutInflater();
if (view == null){
viewholder = new Viewholder();
view = inflater.inflate(R.layout.patient_item_list,null);
viewholder.tv_request_number = (TextView) view.findViewById(R.id.s_req_num);
viewholder.name_ = (TextView) view.findViewById(R.id.name);
viewholder.button = (Button) view.findViewById(R.id.btn_re);
viewholder.date_ = (TextView) view.findViewById(R.id.date);
view.setTag(viewholder);
}
else {
viewholder = (Viewholder) view.getTag();
}
viewholder.tv_request_number.setText((arrayList.size()-position)+".");
viewholder.name_.setText(arrayList.get(position).getName());
viewholder.date_.setText(getDate(Long.parseLong(arrayList.get(position).getDate())));
viewholder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((ListView)viewGroup).performItemClick(v,position,1);
}
});
return view;
}
//Filter Results
@Override
public Filter getFilter() {
if(valueFilter==null) {
valueFilter=new ValueFilter();
}
return valueFilter;
}
private class ValueFilter extends Filter {
//Invoked in a worker thread to filter the data according to the constraint.
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if(constraint != null && constraint.length()>0){
ArrayList<ArrayList_get_set> filterList = new ArrayList<>();
for(int i=0;i<StringFilterList.size();i++){
if((StringFilterList.get(i).getName().toUpperCase())
.contains(constraint.toString().toUpperCase())) {
ArrayList_get_set contacts = new ArrayList_get_set();
contacts.setName(StringFilterList.get(i).getName());
contacts.setDate(StringFilterList.get(i).getDate());
filterList.add(contacts);
}
}
results.count=filterList.size();
results.values=filterList;
}else{
results.count=StringFilterList.size();
results.values=StringFilterList;
}
return results;
}
//Invoked in the UI thread to publish the filtering results in the user interface.
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
arrayList = (ArrayList<ArrayList_get_set>) results.values;
notifyDataSetChanged();
}
}
This my main activity code
simListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(id == 1){
//CUSTOM DIALOG///////////////////////////////
final Dialog dialog = new Dialog(getActivity());
dialog.setCanceledOnTouchOutside(false);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_re_evaluate);
name_ = (TextView) dialog.findViewById(R.id.name);
f_name_ = (TextView) dialog.findViewById(R.id.f_name);
age_ = (TextView) dialog.findViewById(R.id.age);
number_ = (TextView) dialog.findViewById(R.id.number);
gender_ = (TextView) dialog.findViewById(R.id.gender);
dip_rate_ = (TextView) dialog.findViewById(R.id.dip_rate);
date_ = (TextView) dialog.findViewById(R.id.date);
name_.setText(name[position]);
f_name_.setText(f_name[position]);
age_.setText(age[position]);
number_.setText(number[position]);
dip_rate_.setText(dip_rate[position]);
gender_.setText(gender[position]);
date_.setText(getDate(Long.parseLong(date[position])));
Button re_eval = (Button) dialog.findViewById(R.id.btn);
Button btn_cross = (Button) dialog.findViewById(R.id.cross);
btn_cross.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
re_eval.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
p_name = name_.getText().toString();
p_f_name = f_name_.getText().toString();
p_age = age_.getText().toString();
p_number = number_.getText().toString();
p_dip_rate = dip_rate_.getText().toString();
p_gender = gender_.getText().toString();
p_date = date_.getText().toString();
//go from one fragment to another
Fragment fragment = null;
//send data to next fragment
Bundle bundle = new Bundle();
bundle.putString("name", p_name);
bundle.putString("f_name", p_f_name);
bundle.putString("age", p_age);
bundle.putString("number", p_number);
bundle.putString("dip_rate", p_dip_rate);
bundle.putString("gender", p_gender);
bundle.putString("date", p_date);
fragment = new Questions();
if (fragment != null) {
FragmentTransaction ft = getFragmentManager().beginTransaction();
fragment.setArguments(bundle);
ft.replace(R.id.content_frame, fragment);
ft.commit();
}
dialog.dismiss();
}
});
dialog.show();
}
}
});
performing filtering here
protected void onPostExecute(String s) {
progressDialog.dismiss();
if (server_check) {
if (server_response.equals("1")) {
if (name.length > 0) {
patient_adapter = new Patient_Adapter(getActivity(),
arrayList);
simListView.setAdapter(patient_adapter);
//filtering data
search.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
patient_adapter.getFilter().filter(charSequence);
}
@Override
public void afterTextChanged(Editable editable) {
}
});