【问题标题】:Dynamically load items to ExpandableListView using ExpandableListAdapter使用 ExpandableListAdapter 将项目动态加载到 ExpandableListView
【发布时间】:2015-05-09 01:01:23
【问题描述】:

这是我用来在可扩展列表视图中显示我的项目的活动。目前我已经硬编码了应该出现在可扩展列表视图中的值。

String subTotal = getIntent().getStringExtra("subTotal"); // I have these 2 values
String price = getIntent().getStringExtra("price");

这是我想要动态加载的 2 个值。

在下面我有prepareListData(),我有硬编码值到我的 ExpandableListView。

listDataHeader.add("Top 250"); // subTotal
List<String> top250 = new ArrayList<String>();
top250.add("The Shawshank"); //price

我想知道如何用我的动态值替换这些硬编码值。任何帮助将不胜感激。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cart_activity);

    String subTotal = getIntent().getStringExtra("subTotal"); // i ahve these 2 values
    String price = getIntent().getStringExtra("price");//

    ActionBar actionBar = getActionBar();
    getActionBar().setIcon(
            new ColorDrawable(getResources().getColor(android.R.color.transparent)));
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    // Enabling Back navigation on Action Bar icon
    actionBar.setDisplayHomeAsUpEnabled(true);

    // get the listview
            expListView = (ExpandableListView) findViewById(R.id.lvExp);

            // preparing list data
            prepareListData();

            listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);

            // setting list adapter
            expListView.setAdapter(listAdapter);

            // Listview Group click listener
            expListView.setOnGroupClickListener(new OnGroupClickListener() {

                @Override
                public boolean onGroupClick(ExpandableListView parent, View v,
                        int groupPosition, long id) {
                     Toast.makeText(getApplicationContext(),
                     "Group Clicked " + listDataHeader.get(groupPosition),
                     Toast.LENGTH_SHORT).show();
                    return false;
                }
            });

            // Listview Group expanded listener
            expListView.setOnGroupExpandListener(new OnGroupExpandListener() {

                @Override
                public void onGroupExpand(int groupPosition) {
                    Toast.makeText(getApplicationContext(),
                            listDataHeader.get(groupPosition) + " Expanded",
                            Toast.LENGTH_SHORT).show();
                }
            });

            // Listview Group collasped listener
            expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {

                @Override
                public void onGroupCollapse(int groupPosition) {
                    Toast.makeText(getApplicationContext(),
                            listDataHeader.get(groupPosition) + " Collapsed",
                            Toast.LENGTH_SHORT).show();

                }
            });

            // Listview on child click listener
            expListView.setOnChildClickListener(new OnChildClickListener() {

                @Override
                public boolean onChildClick(ExpandableListView parent, View v,
                        int groupPosition, int childPosition, long id) {
                    // TODO Auto-generated method stub
                    Toast.makeText(
                            getApplicationContext(),
                            listDataHeader.get(groupPosition)
                                    + " : "
                                    + listDataChild.get(
                                            listDataHeader.get(groupPosition)).get(
                                            childPosition), Toast.LENGTH_SHORT)
                            .show();
                    return false;
                }
            });
        }

        /*
         * Preparing the list data
         */
        private void prepareListData() {
            listDataHeader = new ArrayList<String>();
            listDataChild = new HashMap<String, List<String>>();

            // Adding child data
            listDataHeader.add("Top 250"); // subTotal

            // Adding child data
            List<String> top250 = new ArrayList<String>();
            top250.add("The Shawshank"); //price

            listDataChild.put(listDataHeader.get(0), top250); // Header, Child data

            DisplayMetrics metrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(metrics);
            int width = metrics.widthPixels; 

         mExpandableList = (ExpandableListView)findViewById(R.id.lvExp);
         mExpandableList.setIndicatorBounds(width - GetPixelFromDips(50), width - GetPixelFromDips(10));  }

           public int GetPixelFromDips(float pixels) {
            // Get the screen's density scale 
            final float scale = getResources().getDisplayMetrics().density;
            // Convert the dps to pixels, based on density scale
            return (int) (pixels * scale + 0.5f);

        }

【问题讨论】:

  • 你的问题很不清楚。看起来您只想将top250.add("The Shawshank"); 替换为top250.add(subTotal);
  • listDataHeader.add("Top 250");这会在可扩展列表视图中加载标题。我想用 subTotal 替换它。所以我的主标题带有 subTotal。当列表视图展开时,我有子标题,此时它加载 top250.add("The Shawshank");而不是我想加载价格变量。
  • top250.add(price); ?
  • 今天有很多类似代码和屏幕截图的问题:带有小计的列表和“顶级”项目之类的东西......你们有很多人在同一个工作吗?比如家庭作业之类的?
  • yh 我们正在做最后一年的项目

标签: android expandablelistadapter


【解决方案1】:

我能够通过这样做来克服这个问题。

String price;
String subTotal ;
    subTotal = getIntent().getStringExtra("subTotal");
    price = getIntent().getStringExtra("price");

    listDataHeadersubTotal= new ArrayList<String>();
    listDataHeaderPrice= new ArrayList<String>();

    listDataHeadersubTotal.add(subTotal); 
    listDataHeaderPrice.add(price); 

    List<String> price= new ArrayList<String>();
    price.add("The Shawshank"); 
    List<String> subTotal= new ArrayList<String>();
    subTotal.add("The Shawshank");

    listDataChild.put(listDataHeadersubTotal.get(0), subTotal);
    listDataChild.put(listDataHeaderPrice.get(0), price);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-08
    • 2011-09-30
    • 2018-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-22
    • 1970-01-01
    相关资源
    最近更新 更多