简体   繁体   中英

Android searchView with open Activity

My application is searching string and if I click open activity,

But my problem with position, if I write Second and after click its show me first activity .I wanna open just second activty

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editsearch = (EditText)findViewById(R.id.editText1);
        listView = (ListView)findViewById(R.id.listView1);

        mItems = new ArrayList<String>();
        mItems.add(new String(getResources().getString(R.string.First)));
        mItems.add(new String(getResources().getString(R.string.Second)));
        mItems.add(new String(getResources().getString(R.string.Third)));
        mItems.add(new String(getResources().getString(R.string.D)));
        mItems.add(new String(getResources().getString(R.string.E)));


        listView.setAdapter(new CustomeArrayAdapter(MainActivity.this, mItems));
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener()  {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                    if(position == 0)
                    {
                        Intent myIntent = new Intent(MainActivity.this, Test.class);
                        MainActivity.this.startActivity(myIntent);  

                    }

                    if(position == 1)
                    {
                        Intent myIntent = new Intent(MainActivity.this, Test2.class);
                        MainActivity.this.startActivity(myIntent);  

                    }       
        }
        });
    editsearch.addTextChangedListener(new TextWatcher() {
        //Event when changed word on EditTex
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
                ArrayList<String> temp = new ArrayList<String>();
                int textlength = editsearch.getText().length();
                temp.clear();
                for (int i = 0; i < mItems.size(); i++)
                {
                    if (textlength <= mItems.get(i).length())
                    {
                        if(editsearch.getText().toString().equalsIgnoreCase(
                                (String)
                                mItems.get(i).subSequence(0,
                                        textlength)))
                        {
                            temp.add(mItems.get(i));
                        }
                    }
                }
                  listView.setAdapter(new CustomeArrayAdapter(MainActivity.this, temp));
            }
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

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

I want just string search and open activity.

thats because your condition is : if(position == 0) ,,, so when you filter the list, the second activity item will become in position 0,,,

instead make your condition :

if((String) getListAdapter().getItem(position).equals(new String(getResources().getString(R.string.First)))

do the same for the second condition as well

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