如何从组键获取组在可扩展列表视图中的位置?
How to get position of group in expandablelistview from group key?
上下文:
我已经创建了一个搜索功能,它 returns 结果是一个列表视图,当我点击其中一个结果时,我想让应用程序转到我的其他活动之一,一个屏幕,它只是一个可扩展的列表视图,由一个 HashMap,滚动到在搜索中单击的项目,然后展开该组。
我已经知道的:
我有搜索功能,它生成列表,当您单击某个项目时,它会将您带到正确的 activity 列表视图。我看了 this question and know I can scroll to a specific item, and thanks to this question 我知道我可以展开列表中的特定项目。当我发送启动 activity 的意图时,我添加了作为额外数据单击的文本,然后我在新的 activity.
中检索数据
我不知道的:
在我发送启动新列表视图 activity 的意图后,我检索了我放入的额外数据,这是一个字符串,它是父视图中 hashmap/the 文本中项目的键我想扩大。如何从组的key中得到我要展开的组的位置? (这样我最终可以调用该对象的滚动和扩展方法)
我的可搜索 activity 中启动新 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 ");
}
}
});
}
新的listviewactivity通过之前的方法启动:
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);
}
}
非常感谢您的帮助!
好的,经过深思熟虑,我想到了一种方法,尽管我不确定这是最优雅的方法。
我在发送打开 expandableListView 的意图后(在额外数据中包含所选项目的键),我收到了额外数据,然后制作了一个包含 HashMap 的所有键的 arrayList 用于填充列表。我使用简单的 for 循环搜索 arrayList,然后在找到匹配项时返回位置,让我可以轻松调用 smoothScrollToPosition()
和 expandGroup()
方法。下面是我的 ExpandableListView class,希望有人能从中受益。
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;
}
}
}
上下文: 我已经创建了一个搜索功能,它 returns 结果是一个列表视图,当我点击其中一个结果时,我想让应用程序转到我的其他活动之一,一个屏幕,它只是一个可扩展的列表视图,由一个 HashMap,滚动到在搜索中单击的项目,然后展开该组。
我已经知道的: 我有搜索功能,它生成列表,当您单击某个项目时,它会将您带到正确的 activity 列表视图。我看了 this question and know I can scroll to a specific item, and thanks to this question 我知道我可以展开列表中的特定项目。当我发送启动 activity 的意图时,我添加了作为额外数据单击的文本,然后我在新的 activity.
中检索数据我不知道的: 在我发送启动新列表视图 activity 的意图后,我检索了我放入的额外数据,这是一个字符串,它是父视图中 hashmap/the 文本中项目的键我想扩大。如何从组的key中得到我要展开的组的位置? (这样我最终可以调用该对象的滚动和扩展方法)
我的可搜索 activity 中启动新 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 ");
}
}
});
}
新的listviewactivity通过之前的方法启动:
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);
}
}
非常感谢您的帮助!
好的,经过深思熟虑,我想到了一种方法,尽管我不确定这是最优雅的方法。
我在发送打开 expandableListView 的意图后(在额外数据中包含所选项目的键),我收到了额外数据,然后制作了一个包含 HashMap 的所有键的 arrayList 用于填充列表。我使用简单的 for 循环搜索 arrayList,然后在找到匹配项时返回位置,让我可以轻松调用 smoothScrollToPosition()
和 expandGroup()
方法。下面是我的 ExpandableListView class,希望有人能从中受益。
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;
}
}
}