Android searchView打开Activity

时间:2015-09-15 12:45:14

标签: android search searchview onitemclick

我的应用程序正在搜索字符串,如果单击打开活动,

但我的位置问题,如果我写第二个并点击它后显示我的第一个活动。我想打开第二个活动

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

我想要字符串搜索和开放活动。

1 个答案:

答案 0 :(得分:0)

多数民众赞成因为你的条件是:if(position == 0) ,,,所以当你过滤列表时,第二个活动项目将变为0 ,,,

改为使你的条件:

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

同样适用于第二个条件