【问题标题】:Generate dynamic array list and fill data in listview android生成动态数组列表并在listview android中填充数据
【发布时间】:2016-03-28 04:48:23
【问题描述】:

我有以下 JSON 格式

{
"listing": {

    "attribute": [{
        "name": "brand",
        "data": [{
            "value": "brand with subtitle"
        }, {
            "value": "Brand2"
        }, {
            "value": "Samsung"
        }]
    }, {
        "name": "price",
        "data": [{
            "value": 12
        }, {
            "value": 1499
        }]
    }, {
        "name": "color",
        "data": [{
            "value": "Red"
        }, {
            "value": "Green"
        }]
    }, {
        "name": "size",
        "data": [{
            "value": "XXXL"
        }, {
            "value": "L"
        }]
    }]
  } 
}

在上述格式中有两个数组属性和数据。所以我的属性数组包含多个我想在列表视图中显示的名称,所以我成功完成了这个任务,见下图

但是里面有特定名称的数据数组列表,并且我想在右侧列表视图中显示该arraylist值意味着如果我当时点击品牌,品牌的价值将显示在右侧列表视图中强>。我为左侧列表项完成了以下代码

if (mGetProductListing.getListing().getAttribute().size() > 0) {
                    mainMenuDataArrayList = getData();
                    if (mainMenuDataArrayList.size() > 0) {
                        mFilterCateAdapter = new FilterCateAdapter(ActivityFilter.this, mainMenuDataArrayList);
                        mListViewLeft.setAdapter(mFilterCateAdapter);
                        mFilterCateAdapter.setSelectedIndex(0);


                    }
                }

但是如何根据选择类别动态列出右侧列表项?你的所有建议都很有价值

【问题讨论】:

  • stackoverflow.com/questions/2250770/… 的可能重复项。此外,您可以编写一个自定义 ListView 适配器,然后简单地将相应左侧列表视图选择的 JSONArray 对象传递给适配器中的更新函数,您可以在其中更新内容并按照上述答案中的步骤操作。
  • Obscure Geek 我听不懂你说的,我有动态的主类别和它的子类别,所以我该如何处理它的位置呢?
  • 您想要自定义适配器还是 ArrayAdapter?您需要一个 XML 文件来为适配器中的每个项目列表扩展视图。确保添加ids,以便您可以引用它来分配值。所以先提供这个。 1. XML 用于项目列表的膨胀。 2.自定义适配器或ArrayAdapter。
  • Aizen:我用 CustomAdapter 实现了它
  • 您是在询问更改列表数据并调用 notifyDataSetChanged() 吗?

标签: android arrays listview arraylist


【解决方案1】:
    private List<Attribute> attributeList;
    private List<AttributeData> dataList;

    initialise both lists in constructor of adapter.

    This list contains your json response under tag "attribute" that has "name" and "data"

    In your custom list adapter under getView() method, 
    convertView.setOnClickListener(new View.OnClickListener() {

    @Override
                public void onClick(View v) {
                   if(attributeList.get(position) != null && !attributeList.get(position).getDataList().isEmpty) {
                      dataList.addAll(attributeList.get(position).getDataList);
//Now pass dataList list to the fragment on the right.

                   }

     }
            });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-09
    • 1970-01-01
    • 2017-11-09
    • 2016-06-08
    相关资源
    最近更新 更多