【问题标题】:How to get position of group in expandablelistview from group key?如何从组键获取组在可扩展列表视图中的位置?
【发布时间】:2016-11-16 14:21:39
【问题描述】:

背景: 我创建了一个搜索功能,它在列表视图中返回结果,当我单击其中一个结果时,我想让应用程序转到我的其他活动之一,一个屏幕只是一个由 HashMap 填充的可扩展列表视图,滚动到在搜索中单击的项目,然后展开该组。

我已经知道的: 我有搜索功能,它制作列表,当您单击一个项目时,它会将您带到正确的活动列表视图。我查看了this question 并知道我可以滚动到特定项目,感谢this question 我知道我可以展开列表中的特定项目。当我发送启动活动的意图时,我添加了作为额外数据单击的文本,然后在新活动中检索数据。

我不知道的: 在我发送启动新列表视图活动的意图后,我检索了我放入的额外数据,这是一个字符串,它是哈希图中项目的键/我想要展开的父视图中的文本。我如何从组的键中获得我想要扩展的组的位置? (这样我最终可以在该对象上调用滚动和展开方法)

可搜索活动中启动新活动的方法:

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 ");
                        }


                    }
                });
    }

上一个方法启动的新listview活动:

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);
    }



}

非常感谢您的任何帮助!

【问题讨论】:

    标签: java android listview expandablelistview


    【解决方案1】:

    好的,经过深思熟虑,我想出了一个方法来做到这一点,虽然我不确定它是最优雅的。

    我在发送打开expandableListView的意图后(选择的项目的键包含在额外数据中),我收到了额外数据,然后制作了一个用于填充HashMap的所有键的arrayList列表。我使用简单的 for 循环搜索了 arrayList,然后在找到匹配项时返回位置,让我轻松调用 smoothScrollToPosition()expandGroup() 方法。我的 ExpandableListView 类如下,希望有人能从中受益。

    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;
            }
        }
    
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-12
      • 1970-01-01
      • 1970-01-01
      • 2015-10-29
      • 1970-01-01
      相关资源
      最近更新 更多