How to make an object in Listview have an absolute position?

Asked

Viewed 162 times

3

I have a listview with several items, I also have right up a searchView to perform a filter on these items, being they "clickable", starting another Activity.

The problem is that the numbering of lisView items changes according to what I search on searchView. That is, I can’t set an Activity for each listview item so that the Array items have an absolute position.Some 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;
            }
        });





    }

}

Aqui a "position" está em 0 para o item AbsolutismoHere the "position" is 0 for the item Absolutism

Aqui a "position" está no mesmo 0 para outro itemHere the "position" is in the same 0 for another item

Lista em questãoList in question

  • I couldn’t get over the code formatting on the forum, sorry.

  • The absolute position in relation to all items gives list?

  • This, for example, the first item in the array list will always be "0" is so on, so that it does not change position in Adapter. Since I use this number as a reference to open certain activities

2 answers

2

You can use the method indexOf(). Then I’d stay that way:

int posicaoAbsoluta = teams.indexOf(itemValue);
  • This line of code would go where? on Lv.setOnItemClickListener? and I would have to create a variable for each item? for example: int placeAbsoluta0 = Teams.indexof(0); int placeAbsoluta1 = Teams.indexof(1); .... I know this must be simple, but I’m a beginner and I don’t know some things right.

  • @juniorklawa you will do inside your own Adapter. Instead of putting index(0) you will put the name gives string. Ex: index("America").

1

I figured out the answer, it was pretty basic. instead of position or itemPosition in the Switch should be placed itemValue and the value of case to the String desired to stay this way:

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;




                }

            }
        });

Thank you all for your help, this community has helped me a lot.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.