Search code examples
javaandroidlistviewarraylistsearchview

ListView indexing and SearchView


I'm building a very basic app: I have a list, once you click on an item of that list you get an alert dialog with more information of the clicked item.

I implemented a filter search function and it's working quite well but there's a big problem: let's say you text on the search bar "1" and you get on screen all the items starting with 1, so:1, 11, 12, 13, 14, 15 etc.

Once you click on the second item of the new list you won't get R.layout.11 but you get R.layout.2 basically because the code will recognize 11 as the second item of the list with id == 1 and not the 11th item with id == 12.

Obviously it's not about the second item only but all the items of the filtered list. Can someone help me with this problem? Do you have any idea about how to solve it? I'm very new to Android app developing, I know little about Java, I'm just learning, thank you for your patience.

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

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_max);

    final String[] nameproducts = new String[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};

    // ArrayList
    final ArrayList<String> list = new ArrayList<String>();
    for (int i = 0; i < nameproducts.length; ++i) {
        list.add(nameproducts[i]);

    }

    final ListView mylist = (ListView) findViewById(R.id.listView1);

    final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);

    sv=(SearchView) findViewById(R.id.searchView1);
    sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String text) {
            return false;

        }

        @Override
        public boolean onQueryTextChange(String text) {
            adapter.getFilter().filter(text);
            return false;

        }

    });

    mylist.setAdapter(adapter);

    mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> testo, View v, int pos, long id) {

            if (id == 0) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MaxActivity.this);
                builder.setCancelable(true);
                LayoutInflater inflater = getLayoutInflater();
                View view = inflater.inflate(R.layout.1, null);
                builder.setView(view);
                builder.show();
            }

            if (id == 1) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MaxActivity.this);
                builder.setCancelable(true);
                LayoutInflater inflater = getLayoutInflater();
                View view = inflater.inflate(R.layout.2, null);
                builder.setView(view);
                builder.show();
            }

            if (id == 2) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MaxActivity.this);
                builder.setCancelable(true);
                LayoutInflater inflater = getLayoutInflater();
                View view = inflater.inflate(R.layout.3, null);
                builder.setView(view);
                builder.show();
            }

        }

    });

}

Solution

  • How about checking the value of the item clicked rather than the id. This works:

    @Override
    public void onItemClick(AdapterView<?> test, View v, int pos, long id) {
        String num = test.getItemAtPosition((int)id).toString();
        if (num.equalsIgnoreCase("11")) {
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setCancelable(true);
            LayoutInflater inflater = getLayoutInflater();
            View view = inflater.inflate(R.layout.eleven, null);
            builder.setView(view);
            builder.show();
        }
        else if (num.equalsIgnoreCase("12")) {
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setCancelable(true);
            LayoutInflater inflater = getLayoutInflater();
            View view = inflater.inflate(R.layout.twelve, null);
            builder.setView(view);
            builder.show();
        }