android - 如何在一个 View 中收集具有相同值的json,在另一个 View 中收集其他值并制作动态 View

标签 android json listview view

我有下面的 json :

 {  
"transaksi": {
        "jenis_link": "unit_link",
        "rks_ulink": [
            {
                "lji_invest": "Excellink Dynamic Fund",
                "tgl": "22/05/2013",
                "harga_unit": "3,581.38",
                "nilai": "0.000",
                "total_unit": "0.0000"
            },
            {
                "lji_invest": "Excellink Aggressive Fund",
                "tgl": "22/05/2013",
                "harga_unit": "3,758.73",
                "nilai": "35,737,078.757",
                "total_unit": "9,507.7435"
            }
        ],
"ulink": [
            {
                "jumlah_unit": 46.9545,
                "investasi": "Excellink Aggressive Fund",
                "harga_unit": 2041.807,
                "tgl_nab": "25/07/2007",
                "transaksi": "Alokasi Investasi",
                "jumlah": 95872
            },
            {
                "jumlah_unit": 54.722,
                "investasi": "Excellink Aggressive Fund",
                "harga_unit": 1827.417,
                "tgl_nab": "27/08/2007",
                "transaksi": "Alokasi Investasi",
                "jumlah": 100000
            },
            {
                "jumlah_unit": -2.2826,
                "investasi": "Excellink Aggressive Fund",
                "harga_unit": 1857.119,
                "tgl_nab": "17/09/2007",
                "transaksi": "Penarikan-Potongan Aug 07",
                "jumlah": 4239
            },

            {
                "jumlah_unit": 186.3333,
                "investasi": "Excellink Dynamic Fund",
                "harga_unit": 2146.691,
                "tgl_nab": "25/10/2007",
                "transaksi": "Alokasi Investasi",
                "jumlah": 400000
            },
            {
                "jumlah_unit": -7.5143,
                "investasi": "Excellink Dynamic Fund",
                "harga_unit": 2185.977,
                "tgl_nab": "01/11/2007",
                "transaksi": "Penarikan-Potongan Oct 07",
                "jumlah": 16426
            },
            {
                "jumlah_unit": 186.2001,
                "investasi": "Excellink Dynamic Fund",
                "harga_unit": 2148.226,
                "tgl_nab": "26/11/2007",
                "transaksi": "Alokasi Investasi",
                "jumlah": 400000
            },
            {
                "jumlah_unit": -7.5314,
                "investasi": "Excellink Dynamic Fund",
                "harga_unit": 2183.127,
                "tgl_nab": "03/12/2007",
                "transaksi": "Penarikan-Potongan Nov 07",
                "jumlah": 16442
            },
            {
                "jumlah_unit": 183.0457,
                "investasi": "Excellink Dynamic Fund",
                "harga_unit": 2185.247,
                "tgl_nab": "26/12/2007",
                "transaksi": "Alokasi Investasi",
                "jumlah": 400000
            }        ]
    }
}

如您所见,很少有 json "investasi" 值与 "Excellink Dynamic Fund" 相同,而其他值与 "Excellink Aggressive Fund" 相同.我想收集它基于一个 json json "investasi" 具有相同的值,但我的问题是我不知道如何在一个 View 中收集它而在其他 View 中收集其他 View 。有关详细信息,我附上两张图片:

json "investasi" 值与 "Excellink Dynamic Fund" 相同

enter image description here

在这个 View 下是 json "investasi" 值与 "Excellink Aggressive Fund" 相同

enter image description here

到目前为止我做了两个布局,第一个布局包含 ListView :

   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@color/medium_gray">
     <TextView 
                android:id="@+id/title"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textSize="20sp"
                android:textColor="#FF0000"
                android:text="TRANSAKSI"
                android:textStyle="bold"
                android:divider="#000000"
                 android:dividerHeight="1dp"
                android:layout_marginTop="8dp"/>

   <TableRow
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">

       <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            style="@style/size10ptWhite3dpBold"
            android:layout_width="0dip"
            android:layout_weight=".35"
            android:gravity="left"
            android:orientation="vertical" 
            android:paddingTop="5dip"
            android:paddingBottom="10dip">

        <!--Kolom 1-->
        <TextView
            android:text="Transaksi"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#FFFFFF"
            android:textSize="17sp"
            android:paddingTop="10dip"
            android:paddingBottom="10dip" />

    </LinearLayout>

        <!-- Kolom 2 -->
        <LinearLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                style="@style/size10ptWhite3dpBold"
                android:layout_width="0dip"
                android:layout_weight=".35"
                android:orientation="vertical"
                android:paddingTop="5dip"
                android:paddingBottom="10dip" >

        <TextView
            android:text="Jumlah"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#FFFFFF"
            android:textSize="14sp"
            android:paddingTop="10dip"
            android:paddingBottom="10dip"
            android:gravity="center" />

        <TextView
            android:text="Tanggal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#b5b5b5"
            android:textSize="14sp"
            android:gravity="center"
            android:paddingBottom="10dip"/>
    </LinearLayout> 

        <!-- Kolom 3 -->
    <LinearLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                style="@style/size10ptWhite3dpBold"              
                android:layout_width="0dip"
                android:layout_weight=".30"
                android:orientation="vertical"
                android:paddingTop="5dip"
                android:paddingBottom="10dip">

        <TextView
            android:id="@+id/jumlah_unit"
            android:text="Jumlah Unit"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#FFFFFF"
            android:textSize="14sp"
            android:paddingTop="10dip"
            android:paddingBottom="10dip" 
            android:gravity="right"/>

        <TextView
            android:id="@+id/harga_unit"
            android:text="Harga Unit"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="14sp"
            android:textColor="#b5b5b5"
            android:gravity="right"
            android:paddingBottom="10dip"/>

    </LinearLayout>

        </TableRow>
        <LinearLayout 
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">
                <include layout="@layout/line"/>
            </LinearLayout>

       <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="#000000"
        android:dividerHeight="1dp"
        android:focusable="false"
        android:clickable="false"/>

</LinearLayout>

值 json 的第二个布局:

   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:background="@color/medium_gray">

    <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            style="@style/size10ptWhite3dpBold"
            android:layout_width="0dip"
            android:layout_weight=".33"
            android:gravity="left"
            android:orientation="vertical" 
            android:paddingTop="5dip"
            android:paddingBottom="10dip">

        <!--Kolom 1-->
        <TextView
            android:id="@+id/transaksi"
            android:text="1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#FFFFFF"
            android:textSize="17sp"
            android:paddingTop="10dip"
            android:paddingBottom="10dip" />

    </LinearLayout>

        <!-- Kolom 2 -->
        <LinearLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                style="@style/size10ptWhite3dpBold"
                android:layout_width="0dip"
                android:layout_weight=".33"
                android:orientation="vertical"
                android:paddingTop="5dip"
                android:paddingBottom="10dip" >

        <TextView
            android:id="@+id/jumlah"
            android:text="Jumlah"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#FFFFFF"
            android:textSize="14sp"
            android:paddingTop="10dip"
            android:paddingBottom="10dip"
            android:gravity="center" />

        <TextView
            android:id="@+id/tanggal"
            android:text="Tanggal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#b5b5b5"
            android:textSize="14sp"
            android:gravity="center"
            android:paddingBottom="10dip"/>
    </LinearLayout> 

        <!-- Kolom 3 -->
    <LinearLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                style="@style/size10ptWhite3dpBold"              
                android:layout_width="0dip"
                android:layout_weight=".33"
                android:orientation="vertical"
                android:paddingTop="5dip"
                android:paddingBottom="10dip">

        <TextView
            android:id="@+id/jumlah_unit"
            android:text="Jumlah Unit"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#FFFFFF"
            android:textSize="14sp"
            android:paddingTop="10dip"
            android:paddingBottom="10dip" 
            android:gravity="right"/>

        <TextView
            android:id="@+id/harga_unit"
            android:text="Harga_Unit"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="14sp"
            android:textColor="#b5b5b5"
            android:gravity="right"
            android:paddingBottom="10dip"/>

    </LinearLayout>

</LinearLayout>

这就是我解析 json 的方式:

      JSONObject jsonObject = new JSONObject(result);

                            JSONObject transaksi = jsonObject.getJSONObject("transaksi");
                            for (int i=0; i<transaksi.length();i++){
                            String jenis_link = transaksi.getString("jenis_link");

RingkasanUlinkVariabel VarRKS = null;
                            JSONArray rks_ulink = transaksi.getJSONArray("rks_ulink");
                            for (int j=0; j<rks_ulink.length();j++){
                                JSONObject rks = rks_ulink.getJSONObject(j);
                                VarRKS= new RingkasanUlinkVariabel(rks.optString("lji_invest"),
                                        rks.optString("total_unit"),
                                        rks.optString("harga_unit"),
                                        rks.optString("nilai"),
                                        rks.optString("tgl"));
                                ListRingkasan.add(VarRKS);
                                System.out.println("Adam levine");

                            }
                                UnitLinkVariabel VarUnit = null;
                            JSONArray ulink = transaksi.getJSONArray("ulink");
                            for (int k=0; k<ulink.length();k++){
                            JSONObject unit = ulink.getJSONObject(k);
                            VarUnit= new UnitLinkVariabel(unit.optString("investasi"),
                                    unit.optString("transaksi"),
                                    unit.optString("jumlah"),
                                    unit.optString("tgl_nab"),
                                    unit.optString("jumlah_unit"),
                                    unit.optString("harga_unit"));
                            ListUnitLink.add(VarUnit);

那么如何在一个 View 中收集具有相同值的 json 而在另一个 View 中收集其他值并在一个布局中制作动态 View ?我希望有人能帮我解决我的问题

最佳答案

试试这个:

//Declare a different list for containing the UnitLinkVariabel
//one for the "Aggressive" and one for the "Dynamic"
List ListUnitLinkEAF = new ArrayList<UnitLinkVariabel>();
List ListUnitLinkEDF = new ArrayList<UnitLinkVariabel>();

//Declare the handler for each UnitLinkVariabel, 
//again, one for Aggressive, one for Dynamic
UnitLinkVariabel VarUnitEAF = null;
UnitLinkVariabel VarUnitEDF = null;

在:

   for (int k=0; k<ulink.length();k++){
         JSONObject unit = ulink.getJSONObject(k);

添加这样的支票:

         if (unit.optString("investasi").equals("Excellink Aggressive Fund") {
             VarUnitEAF = new UnitLinkVariabel(unit.optString("transaksi"),
                          unit.optString("jumlah"),
                          unit.optString("tgl"),
                          unit.optString("jumlah_unit"),
                          unit.optString("harga_unit"));
             ListUnitLinkEAF.add(VarUnitEAF);
         } else if (unit.optString("investasi").equals("Excellink Dynamic Fund") {
             VarUnitEDF = new UnitLinkVariabel(unit.optString("transaksi"),
                          unit.optString("jumlah"),
                          unit.optString("tgl"),
                          unit.optString("jumlah_unit"),
                          unit.optString("harga_unit"));
             ListUnitLinkEDF.add(VarUnitEDF);
         }

现在你有 2 个列表,一个带有 Excellink Aggressive Fund or (EAF) , 和一个 Excellink Dynamic Fund or (EDF)

你现在需要做的是将每一个列表放到ListView中,如果你想让它们都显示,准备2个listviews,一个用于EAF,一个用于EDF。我将留给您将数据插入 listView。

祝你好运:)

编辑

使用Set<String>获取 Investasi 的值, 阅读 UnitLinkVariabel 的所有条目, 并将所有字符串放在 Investasi 中在Set里面.

如您所知,Set不能有重复项,所以这就是您要查找的内容。

这是一些 fragment :

//Declare the set
Set<String> investasi = new LinkedHashSet<String>();

现在,阅读所有 Investasi并将其添加到 investasi

   for (int k=0; k<ulink.length();k++){
         JSONObject unit = ulink.getJSONObject(k);
         investasi.add(unit.optString("investasi"));
    }

然后,对于每个 investasi ,做点什么:

for (String jenisInvestasi : investasi) {
     for (int k=0; k<ulink.length();k++){
         JSONObject unit = ulink.getJSONObject(k);
         if (unit.optString("investasi").equals(jenisInvestasi)) {
            //insert into list
         }
}

这就是逻辑,你应该可以自己找到其余的代码,祝你好运:D

关于android - 如何在一个 View 中收集具有相同值的json,在另一个 View 中收集其他值并制作动态 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16726795/

相关文章:

android - 触摸不可见 View 后面的按钮

json - PostgreSQL - 在一列中返回 jsonb 键

json - Swift 中的远程 json 解析

java - ListView 中的 getItemIdAtPosition() 未显示

android - Firebase 无限滚动 ListView 在滚动时加载 10 个项目

java - removeAll() 没有删除对象

android - 使用 Twitter4J 库请求 OAuthRequest token 时出现 "SSL is required"异常

带叠加层的 Android 应用介绍

c# - 如何正确获取Json值?

android - ListView 显示叠加项的 ID,而不是标题