简体   繁体   中英

Searchview in Android working but displaying incorrect result when clicked on.

So i have a custom adapter with a searchview. I got my searchview to display the correct results whenever I search for a particular person. But when I click on the item in the searchview it returns a result from the original list.

So for example, I have items ab,cd,ef and i search for ef. The searchview returns the 1 item ef. But if I click on it, I will get the data for ab.

How do I fix this? Or do i need to override a method for a onclick for the searchview?

Thank you. (I will post my code (I will take out the nonrelated code), my custom adapter has the searchview code in it. The other is my fragment that contains my searchview and listview list).

My Custom Adapater. 



public class oncallAdapter extends ArrayAdapter<OnCallContact> implements SectionIndexer
{
    private ArrayList<OnCallContact> contactList;
    private Context context;

    //For our searchview
    private MyFilter filter;
    private ArrayList<OnCallContact> mcontactListFilter;

    //OUr adapter constructor
    //WE have an arraylist which represents the bulletin objects
    public oncallAdapter(ArrayList<OnCallContact> ONCALLLIST, Context ctx)
    {
        super(ctx, R.layout.list_item_oncall, ONCALLLIST);
        this.contactList = ONCALLLIST;
        this.context= ctx;
        this.mcontactListFilter = ONCALLLIST;
    }

    //This is what sets up the textviews or one item in the listview.
    //We are overiding the orignial method
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        //if null then inflate the view which is the row for the bulletins
        if(convertView == null)
        {
            LayoutInflater inflator = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflator.inflate(R.layout.list_item_oncall,parent,false);
        }



        TextView mgroup = (TextView) convertView.findViewById(R.id.oncall_group);
        TextView mname = (TextView) convertView.findViewById(R.id.oncall_name);
        TextView mCircleText = (TextView) convertView.findViewById(R.id.circleText);

        OnCallContact b = contactList.get(position);


        //Setting the textviews.
        mgroup.setText(b.getMgroup());
        mname.setText(b.getMname());
        mCircleText.setText(b.getMgroup().substring(0,1).toUpperCase());

        return convertView;
    }

    @Override
    public android.widget.Filter getFilter()
    {
        if(filter == null)
        {
            filter = new MyFilter();
        }

        return filter;
    }
 private class MyFilter extends android.widget.Filter
 {
    @Override
    protected android.widget.Filter.FilterResults performFiltering(CharSequence constraint)
    {
      FilterResults results = new FilterResults();

        if(constraint!= null && constraint.length()>0)
        {
            //Arraylist where we will add the items (oncallConcacts) that have the letter
            ArrayList<OnCallContact> filterList = new ArrayList<>();

            for(int i=0; i < mcontactListFilter.size();i++)
            {
                //CHecking if the letter is in the name
                if(mcontactListFilter.get(i).getMname().toUpperCase().contains(constraint.toString().toUpperCase()))
                {
                    OnCallContact contact = new OnCallContact();

                    contact.setMname(mcontactListFilter.get(i).getMname());
                    contact.setMgroup(mcontactListFilter.get(i).getMgroup());
                    contact.setMtitle(mcontactListFilter.get(i).getMtitle());
                    contact.setMbusinessPhone(mcontactListFilter.get(i).getMbusinessPhone());
                    contact.setMemail(mcontactListFilter.get(i).getMemail());
                    contact.setMpager(mcontactListFilter.get(i).getMpager());
                    contact.setmManagerName(mcontactListFilter.get(i).getmManagerName());
                    contact.setmManagerBusinessPhone(mcontactListFilter.get(i).getmManagerBusinessPhone());
                    contact.setmManagerPager(mcontactListFilter.get(i).getmManagerPager());
                    contact.setmManagerEmail(mcontactListFilter.get(i).getmManagerEmail());

                    filterList.add(contact);
                }
            }

            results.count = filterList.size();
            results.values = filterList;

            Log.v("HEre is the filtersize " , "" +filterList.size());
        }

        else
        {
            results.count = mcontactListFilter.size();
            results.values = mcontactListFilter;
        }

        return results;
    }

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

         contactList = (ArrayList<OnCallContact>) results.values;
         notifyDataSetChanged();

     }
}

}

My fragment class

  public class OnCallFragment extends android.support.v4.app.Fragment implements SearchView.OnQueryTextListener{

oncallAdapter adapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_on_call, container, false);

    //Parsing the xml file and getting the data we need which is an arraylist of oncallContacts
    OnCallXMLParser b = new OnCallXMLParser();
    final ArrayList<OnCallContact> list = b.parse(getContext());

    //HEre the custom adapter is making the listviews
    adapter = new oncallAdapter(list, getContext());
    final ListView listView = (ListView) rootView.findViewById(R.id.onCallListView);

    SearchView search = (SearchView)rootView.findViewById(R.id.onCall_searchView);
    search.setOnQueryTextListener(this);

    //displaying the listview by setting the adapter.
    listView.setAdapter(adapter);
    listView.setFastScrollEnabled(true);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //The contact we are passing through. (Item that was clicked on)
            OnCallContact temp = list.get(position);
            // Intent i = new Intent(getActivity(), DetailActivity.class).putExtra("extra", ForecastAdapter.getItem(position));
            Intent i = new Intent(getActivity(), OnCallDetail.class).putExtra("ContactInformation", temp);

            startActivity(i);
        }
    });

    // Inflate the layout for this fragment
    return rootView;
}

@Override
public boolean onQueryTextChange(String newText) {
    adapter.getFilter().filter(newText);
    return false;
}

@Override
public boolean onQueryTextSubmit(String query) {
    return false;
}

}

Thank You!!

It may most likely because the position of the result ef within all filtered results, and the position of the result ef within all available items are different. You need to use another variable like a self-defined identifier instead of using position. Refer to this for more information: Wrong item from the listview is selected and ListView custom filter gives wrong item selected when filtered

For anyone that runs into this issue, make sure you override your getcount, getitem. GetItemId should stay as return 0; The other 2 methods you must override them. Hope that helps anyone that may run into this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM