1

Context: I have created a search function, it returns results in a listview, and when I click on one of the results I's like to have the app go to one of my other activities, a screen that is just an expandablelistview populated by a HashMap, scroll to the item that was clicked on in the search, and expand that group.

What I already know: I have the search function, it makes the list, and when you click on an item, it takes you to the correct activity listview. I looked at this question and know I can scroll to a specific item, and thanks to this question I know I can expand a specific item in the list. When I send the intent to start the activity, I add in the text that was clicked on as extra data, and then I retrieve the data in the new activity.

What I don't know: After I send the intent to start the new list view activity, I retrieve the extra data that I put in, which is a string that is the key of the item in the hashmap/the text in the parent view I want to expand. How do I get the position of the group I want to expand from the key of the group? (so that i can eventually call the scroll and expand methods on that object)

The method in my searchable activity that starts the new activity:

private void doSearch(Cursor query) {
        // get a Cursor, prepare the ListAdapter
        // and set it
        Cursor c = query;
        startManagingCursor(c);


        String[] from = new String[] {"QUANTITY", "_id"};
        int[] to = new int[] {android.R.id.text1};
        SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, c, from, to);
        mListView.setAdapter(cursorAdapter);
        Log.e("doSearch method:", "has been called");

        mListView.setOnItemClickListener(
                new AdapterView.OnItemClickListener() {
                    public void onItemClick(AdapterView<?> parent, View view,
                                            int position, long id) {
                        // When clicked, log with the TextView text

                        Log.e("doSearch method:", "Answer: " + ((TextView) view).getText());

                        if(cMap.containsKey(((TextView) view).getText())){
                            Intent i=new Intent(getApplicationContext(),CommonConstants.class);
                            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            i.putExtra("key", ((TextView) view).getText());
                            getApplicationContext().startActivity(i);

                        } else if (chMap.containsKey(((TextView) view).getText())){
                            Intent i=new Intent(getApplicationContext(),PhysicoChemicalConstants.class);
                            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            i.putExtra("key", ((TextView) view).getText());
                            getApplicationContext().startActivity(i);

                        } else if (aMap.containsKey(((TextView) view).getText())){
                            Intent i=new Intent(getApplicationContext(),AtomicNuclearConstants.class);
                            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            i.putExtra("key", ((TextView) view).getText());
                            getApplicationContext().startActivity(i);

                        }
                        else if (eMap.containsKey(((TextView) view).getText())){
                            Intent i=new Intent(getApplicationContext(),ElectromagneticConstants.class);
                            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            i.putExtra("key", ((TextView) view).getText());
                            getApplicationContext().startActivity(i);

                        }
                        else{
                            Log.e("doSearch method:", "not any map ");
                        }


                    }
                });
    }

The new listview activity that gets started by the previous method:

public class PhysicoChemicalConstants extends SearchViewActivity {

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

        MyDataProvider dp = new MyDataProvider(this);

        ExpandableListView view;
        view = (ExpandableListView) findViewById(R.id.expandableList);
        HashMap constantsHashMap;
        constantsHashMap = dp.getChemMap();
        ArrayList constantsHashMapKeys = new ArrayList<String>(constantsHashMap.keySet());

        MyCustomAdapter adapter = new MyCustomAdapter(this, constantsHashMap, constantsHashMapKeys);
        view.setAdapter(adapter);

        Intent intent = getIntent();
        String mKey = intent.getStringExtra("key");




//creates toolbar
        Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
        setSupportActionBar(myToolbar);
    }



}

Thanks so much for any and all help!

Community
  • 1
  • 1
grassss
  • 199
  • 1
  • 1
  • 15

1 Answers1

0

Ok, after a fair amount of thought I came up with a way to do this, although I'm not sure it is the most elegant.

I after I sent the intent to open the expandableListView (with the key of the item selected included in the extra data), I received the extra data, then made an arrayList of all of the keys of the HashMap used to populate the list. I searched the arrayList using a simple for loop, then returned the position when I found a match, letting me call the smoothScrollToPosition() and expandGroup() methods easily. My ExpandableListView class is below, I hope someone can benefit from this.

public class CommonConstants extends SearchViewActivity {
    View view;
    String searchedCon;
    MyDataProvider dp;
    HashMap constantsHashMap;
    ArrayList constantsHashMapKeys;


    //finds view and hashmap, passes to adapter to display
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_common_constants);

        dp = new MyDataProvider(this);
        ExpandableListView view;
        view = (ExpandableListView) findViewById(R.id.expandableList);

        constantsHashMap = dp.getCommonMap();
        constantsHashMapKeys = new ArrayList<String>(constantsHashMap.keySet());

        MyCustomAdapter adapter = new MyCustomAdapter(this, constantsHashMap, constantsHashMapKeys);
        view.setAdapter(adapter);

        int pos;
        Intent intent = getIntent();
        searchedCon = intent.getStringExtra("key");
        if(searchedCon != null){
            pos = handleIntent(intent);
            view.smoothScrollToPosition(pos);
            view.expandGroup(pos);

        }






//creates toolbar
        Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
        setSupportActionBar(myToolbar);

    }

    private int handleIntent(Intent intent) {

        searchedCon = intent.getStringExtra("key");
        boolean exist = false;
        int i;

        for( i  = 0; i<constantsHashMapKeys.size();i++){
            if (constantsHashMapKeys.get(i).equals(searchedCon)) {
                exist = true;
                break;
            }
        }
        if(exist){
            return i;
        }else{
            return 0;
        }
    }



}
grassss
  • 199
  • 1
  • 1
  • 15