简体   繁体   中英

When i search items on my listview. it always open the 1st activity

When i search in my listview it always open the Articfox even i search the bear When i search in my listview it always open the Articfox even i search the bear When i search in my listview it always open the Articfox even i search the bear strong text

private ListView mainlistView ;  
    private ArrayAdapter<String> listAdapter ; 
    EditText inputSearch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_animals);


         mainlistView = (ListView) findViewById( R.id.AnimalsView );
         inputSearch = (EditText) findViewById(R.id.inputSearch);

         final String[] Animals = new String[] { "Articfox", "Bear", "Cat", "Dog",  
                 "Elephant", "Flamingo", "Giraffe", "Huming Bird"};    
         ArrayList<String> AnimalsList = new ArrayList<String>();  
         AnimalsList.addAll( Arrays.asList(Animals) );  

         listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, AnimalsList);  
         mainlistView.setAdapter(listAdapter);
         inputSearch.addTextChangedListener(new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                    // When user changed the Text
                    Animals.this.listAdapter.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                          
                }


         });


         ListView mainlistView=(ListView)findViewById(R.id.AnimalsView);

       this.setListAdapter(new ArrayAdapter<String>(this,
                  android.R.layout.simple_list_item_1,Animals));

        mainlistView.setOnItemClickListener(new onItemClickListener(){
            @Override
       public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
            super.onItemClick(arg0, view, position, arg3);

            if (position == 0) {
                Intent int0 = new Intent(getApplicationContext(),Articfox.class);
                startActivity(int0);
            }

            else if (position == 1) {
                Intent int1 = new Intent(getApplicationContext(), ArticfoxInfo.class);
                startActivity(int1);
            }
            else if (position == 2) {
                Intent int2 = new Intent(getApplicationContext(), Articfox.class);
                startActivity(int2);
            }

            else if (position == 3) {
                Intent int3 = new Intent(getApplicationContext(),
                        Articfox.class);
                startActivity(int3);
            }

        }
 });


         listAdapter.add( "Iguana" );  
            listAdapter.add( "Jaguar" );  
            listAdapter.add( "Kangaroo" );  
            listAdapter.add( "Lizard" );  
            listAdapter.add( "Monkey" );  




    }

It seems that you are calling the same activity, in this case Articfox.class on every item click:

if (position == 0) {
        Intent int0 = new Intent(getApplicationContext(),Articfox.class);
        startActivity(int0);
    }

    else if (position == 1) {
        Intent int1 = new Intent(getApplicationContext(), ArticfoxInfo.class);
        startActivity(int1);
    }
    else if (position == 2) {
        Intent int2 = new Intent(getApplicationContext(), Articfox.class);
        startActivity(int2);
    }

    else if (position == 3) {
        Intent int3 = new Intent(getApplicationContext(),
                Articfox.class);
        startActivity(int3);
    }

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