简体   繁体   English

在片段中使用ListView进行搜索

[英]Search using ListView in a fragment

I need help. 我需要帮助。

I'm having trouble about the function of my search bar. 我在搜索栏功能方面遇到麻烦。 Before putting the search code. 在放置搜索代码之前。 My ListandSearch.java is working well. 我的ListandSearch.java运行良好。 It work's the way what I wanted to do. 这就是我想要做的方式。

ListandSearch.java: ListandSearch.java:

public class ListandSearch extends Fragment{


    // Search EditText
    EditText inputSearch;
    ArrayAdapter<String> listViewAdapter;
    ListView listview;


    String[] esttype = {
            "Art Gallery", //1
            "ATM", //2
            "Bakery", //3
            "Bank", //4
            "Bar", //5


};


    public ListandSearch(){

    }


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

        listview = (ListView) view.findViewById(R.id.thislist);
        inputSearch = (EditText) view.findViewById(R.id.inputSearch);

        listViewAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1 , esttype);
        listview.setAdapter(listViewAdapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                android.app.FragmentManager fragmentManager = getFragmentManager();
                switch (position) {
                    case 0:
                        fragmentManager.beginTransaction().replace(R.id.content_frame, new ArtGalleryFragment()).commit();
                        break;
                    case 1:
                        fragmentManager.beginTransaction().replace(R.id.content_frame, new AtmFragment()).commit();
                        break;
                    case 2:
                        fragmentManager.beginTransaction().replace(R.id.content_frame, new BakeryFragment()).commit();
                        break;
                    case 3:
                        fragmentManager.beginTransaction().replace(R.id.content_frame, new BankFragment()).commit();
                        break;
                    case 4:
                        fragmentManager.beginTransaction().replace(R.id.content_frame, new BarFragment()).commit();
                        break;

                        default:
                        break;

                }
            }});
        return view;
    }
}

Then i insert this code: 然后我插入此代码:

/************************************************************************************/
        inputSearch.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                // When user changed the Text
               // getActivity().
                        listViewAdapter.getFilter().filter(cs);


            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                          int arg3) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub

            }
        });
        /*****************************************************************/

So far the code is working. 到目前为止,代码正在运行。 It can search. 它可以搜索。

The problem: (Ex. #1 I search the "complete" word Bar and select it instead it will go to the BarFragment it goes to the ArtGalleryFragment) 问题:(例如#1,我搜索“完整”单词Bar并选择它,取而代之的是将其转到BarFragment,然后将其转到ArtGalleryFragment)

(Ex. #2 When I search " ba " it display's the list bakery, bank, bar. When I select Bank instead when it goes to the BankFragment it goes to the AtmFragment. (例如#2。当我搜索“ ba ”时,它会显示面包店,银行,酒吧的列表。当我选择“银行”时,当它转到BankFragment时,它将转到AtmFragment。

Can you help me? 你能帮助我吗?

You can't use int position , as it is the position in your search result list, not the complete list. 您不能使用int position ,因为它是搜索结果列表中的位置,而不是完整列表中的位置。

Ex 1: There's only one element in your search result, so it has index 0. Selecting it causes case 0 to run, which is ArtGalleryFragment . 例1:搜索结果中只有一个元素,因此它的索引为0。选择它会导致case 0运行,即ArtGalleryFragment

Ex 2: "Bank" is the second element in the search result list, and AtmFragment is the second case (index 1) in your switch . 示例2:“银行”是搜索结果列表中的第二个元素,而AtmFragment是您的switch的第二种情况(索引1)。

我认为您需要实现Filterable接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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