1

i have json as bellow :

 {  
"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
            }        ]
    }
}

as you see, few json "investasi" values are same with "Excellink Dynamic Fund" and others with "Excellink Aggressive Fund" . i want to collect it base one json json "investasi"with same values, but my problems are i don't know how to collect it in one view and others in others view. for detail i enclosure two image :

json "investasi" values are same with "Excellink Dynamic Fund"

enter image description here

under this view is json "investasi" values are same with "Excellink Aggressive Fund"

enter image description here

so far i have make two layout, first layout is containing 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>

second layout for value 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>

this is how i parse my 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);

so how to collect json with same value in one view and others in another view and make a dynamic view in one layout? i hope somebody can help me to solve my problem

4

2 回答 2

1

试试这个:

//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 个列表视图,一个用于 EAF,一个用于 EDF。剩下的就是将数据插入到 listView 中。

祝你好运 :)

编辑

用于Set<String>获取 的值Investasi,读取 的所有条目UnitLinkVariabel,并将所有字符串Investasi放入Set.

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

这是一些片段:

//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

于 2013-05-29T16:14:01.723 回答
0

为了处理它,我做了一些逻辑如下:

  1. 添加两个新的数组列表,它们是:

私有 ArrayList StringUnit []; // 对于我已经拆分的处理数组列表私有 ArrayList kelompok; // 用于句柄数组列表

this is how i parse my 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"),
                                            unit.optString("jumlah_unit"),
                                            unit.optString("harga_unit"));
                                    ListUnitLink.add(VarUnit);

                                    // this is how i divide my arraylist and collect them 

                                    String[] mStringArray = new String[ListUnitLink.size()];
                                    mStringArray = ListUnitLink.toArray(mStringArray);

                                    for(int e= 0; e < mStringArray.length ; e++){
                                        kelompok.add(mStringArray[e]);
                                        if (mStringArray.optString("investasi") == mStringArray[e-1].optString("investasi"))
                                        {
                                            StringUnit.add(kelompok);
                                        }

好吧,这还没有完成,因为我在这里有堆栈。我不知道我的逻辑是错误的还是错误的。

于 2013-05-30T03:37:22.700 回答