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!