How to make an object in the ListView have an absolute position?

3

I have a listView with several items, I also have a searchView to make a filter on these items, being "clickable", starting another Activity.

The problem is that the numbering of lisView items changes according to what I search in searchView. That is, I can not set an Activity for each listView item so that Array items have an absolute position. Any light?

Here's the code in question.

ListView lv;
    SearchView sv;
    String[] teams={"Absolutismo",
            "Agentes de Relevo",
            "Alelobiose",
            "Alta Idade Média",
            "América",


    };
    ArrayAdapter<String> adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tela_pesquisa);
        lv=(ListView) findViewById(R.id.listView);
        sv=(SearchView) findViewById(R.id.searchView);
        adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,teams);
        lv.setAdapter(adapter);


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

                int itemPosition = position;

                // ListView Clicked item value
                String  itemValue = (String) lv.getItemAtPosition(position);

                // Show Alert
                Toast.makeText(getApplicationContext(), "Position :"+itemPosition+"  ListItem : " +itemValue , Toast.LENGTH_LONG).show();

                switch( position )
                {
                    case 0:  Intent newActivity = new Intent(telaPesquisa.this, historiaAbsolutismo.class);
                        startActivity(newActivity);
                        break;
                    case 1:  Intent newActivityy = new Intent(telaPesquisa.this, geografiaAgentesFormadoresDeRelevo.class);
                        startActivity(newActivityy);
                        break;
                    case 2:  Intent newActivity2 = new Intent(telaPesquisa.this, biologiaAlelobioses.class);
                        startActivity(newActivity2);
                        break;
                    case 3:  Intent newActivity3 = new Intent(telaPesquisa.this, historiaAltaIdadeMedia.class);
                        startActivity(newActivity3);
                        break;
                    case 4:  Intent newActivity4 = new Intent(telaPesquisa.this, geografiaAmerica.class);
                        startActivity(newActivity4);
                        break;
                }

            }
        });




             sv.setOnQueryTextListener(new OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String text) {
                // TODO Auto-generated method stub
                return false;
            }
            @Override
            public boolean onQueryTextChange(String text) {
                adapter.getFilter().filter(text);
                return false;
            }
        });





    }

}

Herethe"position" is at 0 for the item Absolutism Here the' position 'is at 0 for the item Absolutism

Herethe"position" is at the same 0 for another item Here the' position 'is at the same 0 for another item

List in question

    
asked by anonymous 30.12.2016 / 05:01

2 answers

2

You can use the indexOf() method. Then it would look like this:

int posicaoAbsoluta = teams.indexOf(itemValue);
    
30.12.2016 / 12:37
1

I discovered the answer, it was quite basic. instead of position or itemPosition no Switch you must set itemValue and you must also change the value of case to String you want it to be:

switch( itemValue )
                {
                    case "Absolutismo":  Intent newActivity = new Intent(telaPesquisa.this, historiaAbsolutismo.class);
                        startActivity(newActivity);
                        break;
                    case "Agentes Formadores de Relevo":  Intent newActivityy = new Intent(telaPesquisa.this, geografiaAgentesFormadoresDeRelevo.class);
                        startActivity(newActivityy);
                        break;
                    case "Alelobiose":  Intent newActivity2 = new Intent(telaPesquisa.this, biologiaAlelobioses.class);
                        startActivity(newActivity2);
                        break;
                    case "Alta Idade Média":  Intent newActivity3 = new Intent(telaPesquisa.this, historiaAltaIdadeMedia.class);
                        startActivity(newActivity3);
                        break;
                    case "América":  Intent newActivity4 = new Intent(telaPesquisa.this, geografiaAmerica.class);
                        startActivity(newActivity4);
                        break;




                }

            }
        });

Thanks to all for the help, this community has helped me a lot.

    
30.12.2016 / 16:08