如何使用过滤后的列表视图?

时间:2019-06-12 03:54:58

标签: android

我使用了打算用于其他活动的过滤器和OnItemClickListner创建了一个自定义列表视图, 但是当我单击已过滤列表视图中的项目时,原始列表视图(未过滤列表视图)中的一个项目带来了。

我认为原因是我使用“位置”进行一项活动,但我不知道要取得想要的结果。

public class Restaurant_Main_Search extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("Search");
        setContentView(R.layout.activity_reataurant__main__search);

        final ListView listView_Restaurant_Main_Search;
        Restaurant_Listview_Adapter_Search adapter;

        adapter = new Restaurant_Listview_Adapter_Search() ;

        listView_Restaurant_Main_Search = (ListView) findViewById(R.id.listview_Restaurant_Main_Search);
        listView_Restaurant_Main_Search.setAdapter(adapter);

        EditText editTextFilter = (EditText)findViewById(R.id.editTextFilter_Restaurant_Main_Search) ;
        editTextFilter.addTextChangedListener(new TextWatcher() {
            @Override
            public void afterTextChanged(Editable edit) {
                String filterText = edit.toString() ;
                if (filterText.length() > 0) {
                    listView_Restaurant_Main_Search.setFilterText(filterText) ;
                } else {
                    listView_Restaurant_Main_Search.clearTextFilter() ;
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }
        }) ;


        adapter.addItem(ContextCompat.getDrawable(this, R.drawable.icon_coffee),
                "Coffee_1", "031-204-1006") ;
        adapter.addItem(ContextCompat.getDrawable(this, R.drawable.icon_coffee),
                "Coffee_2", "031-202-7477") ;
        adapter.addItem(ContextCompat.getDrawable(this, R.drawable.icon_coffee),
                "Coffee_3", "031-203-4355") ;
        adapter.addItem(ContextCompat.getDrawable(this, R.drawable.icon_coffee),
                "Coffee_4", "031-273-3677") ;
        adapter.addItem(ContextCompat.getDrawable(this, R.drawable.icon_coffee),
                "Coffee_5", "031-203-0700") ;
        adapter.addItem(ContextCompat.getDrawable(this, R.drawable.icon_pizza),
                "Pizza_1", "031-204-6112") ;
        adapter.addItem(ContextCompat.getDrawable(this, R.drawable.icon_pizza),
                "Pizza_2", "031-215-8789") ;
        adapter.addItem(ContextCompat.getDrawable(this, R.drawable.icon_pizza),
                "Pizza_3", "031-222-5840") ;
        adapter.addItem(ContextCompat.getDrawable(this, R.drawable.icon_pizza),
                "Pizza_4", "031-221-8890") ;
        adapter.addItem(ContextCompat.getDrawable(this, R.drawable.icon_pizza),
                "Pizza_5", "031-204-2666") ;
        adapter.addItem(ContextCompat.getDrawable(this, R.drawable.icon_chicken),
                "Chicken_1", "031-217-2008") ;
        adapter.addItem(ContextCompat.getDrawable(this, R.drawable.icon_chicken),
                "Chicken_2", "031-707-3316") ;
        adapter.addItem(ContextCompat.getDrawable(this, R.drawable.icon_chicken),
                "Chicken_3", "031-707-5655") ;
        adapter.addItem(ContextCompat.getDrawable(this, R.drawable.icon_chicken),
                "Chicken_4", "031-715-0349") ;
        adapter.addItem(ContextCompat.getDrawable(this, R.drawable.icon_chicken),
                "Chicken_5", "031-704-4122") ;





        listView_Restaurant_Main_Search.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View v, int position, long id) {
                Restaurant_Listview_Item_Search item = (Restaurant_Listview_Item_Search) parent.getItemAtPosition(position) ;

                String titleStr = item.getTitle() ;
                String descStr = item.getDesc() ;
                }
        }) ;

        listView_Restaurant_Main_Search.setDividerHeight(4);
        listView_Restaurant_Main_Search.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> adapterView, View clickedView, int p, long id) {
                if (p == 0){
                    Intent intent = new Intent(Restaurant_Main_Search.this, Restaurant_1.class);
                    startActivity(intent);}
                else  if (p == 1){
                    Intent intent = new Intent(Restaurant_Main_Search.this, Restaurant_2.class);
                    startActivity(intent);
                }
                else  if (p == 2){
                    Intent intent = new Intent(Restaurant_Main_Search.this, Restaurant_3.class);
                    startActivity(intent);
                }
                else if(p == 3){
                    Intent intent = new Intent(Restaurant_Main_Search.this, Restaurant_4.class);
                    startActivity(intent);
                }
                else if(p == 4){
                    Intent intent = new Intent(Restaurant_Main_Search.this, Restaurant_5.class);
                    startActivity(intent);
                }
                else if(p == 5){
                    Intent intent = new Intent(Restaurant_Main_Search.this, Restaurant_6.class);
                    startActivity(intent);
                }
                else  if (p == 6){
                    Intent intent = new Intent(Restaurant_Main_Search.this, Restaurant_7.class);
                    startActivity(intent);
                }
                else  if (p == 7){
                    Intent intent = new Intent(Restaurant_Main_Search.this, Restaurant_8.class);
                    startActivity(intent);
                }
                else  if (p == 8){
                    Intent intent = new Intent(Restaurant_Main_Search.this, Restaurant_9.class);
                    startActivity(intent);
                }
                else  if (p == 9){
                    Intent intent = new Intent(Restaurant_Main_Search.this, Restaurant_10.class);
                    startActivity(intent);
                }
                else  if (p == 10){
                    Intent intent = new Intent(Restaurant_Main_Search.this, Restaurant_11.class);
                    startActivity(intent);
                }
                else  if (p == 11){
                    Intent intent = new Intent(Restaurant_Main_Search.this, Restaurant_12.class);
                    startActivity(intent);
                }
                else  if (p == 12){
                    Intent intent = new Intent(Restaurant_Main_Search.this, Restaurant_13.class);
                    startActivity(intent);
                }
                else  if (p == 13){
                    Intent intent = new Intent(Restaurant_Main_Search.this, Restaurant_14.class);
                    startActivity(intent);
                }
                else  if (p == 14){
                    Intent intent = new Intent(Restaurant_Main_Search.this, Restaurant_15.class);
                    startActivity(intent);
                }
                            }

        });
    }
}

0 个答案:

没有答案