2016-02-19 48 views
0

所以我有一个带有searchview的自定义适配器。每当我搜索某个特定的人时,我都会使用我的搜索查看来显示正确的结果。但是当我点击searchview中的项目时,它会返回原始列表中的结果。Android中的Searchview工作,但点击时显示不正确的结果。

因此,例如,我有项目ab,cd,ef和我搜索ef。 searchview返回1个项目ef。但是如果我点击它,我会得到ab的数据。

我该如何解决这个问题?或者,我是否需要覆盖searchview的onclick方法?

谢谢。 (我会发布我的代码(我将取出不相关的代码),我的自定义适配器中有searchview代码。另一个是我的片段,它包含我的searchview和listview列表)。

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(); 

    } 
} 

} 

我的片段类

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; 
} 

}

谢谢!!

+0

尝试在您的onQueryTextChanged方法中的adapter.getFilter()。filter(newText)之后添加adapter.notifyDataSetChanged() – Arshad

+0

感谢您的建议,但没有:/。任何其他建议或想法? @ arshadkazmi42 – huey77

回答

0

对于遇到此问题的任何人,请确保您覆盖您的getcount,getitem。 GetItemId应保持为返回0;其他两种方法你必须重写它们。希望能帮助任何可能遇到此问题的人。