选择组

时间:2016-01-06 12:15:44

标签: java android sqlite expandablelistadapter

嗨,我创建了一个应用程序,将我的销售发票保存在sqlite中,再次显示你可以看到图片中的示例数据。

enter image description here

enter image description here

现在我想将数据添加到扩展列表适配器 HFO中的每一行都是列表视图中的一个组。 并且Hid = id的每个DFO都是HFO的子项。 我添加组数据,但当我想打开一个组我的应用程序崩溃

请帮助我的坦克。

dfo_listitem.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="6dip" >

    <CheckBox
        android:id="@+id/checkBoxchilditem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/tvchilditem"
        android:focusable="false"
        android:focusableInTouchMode="false" />

    <TextView
        android:id="@+id/tvDfo_Rid_child"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginRight="14dp"
        android:layout_toLeftOf="@+id/checkBoxchilditem"
        android:text="TextView" />

    <TextView
        android:id="@+id/tvDfo_Qty_child"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         android:layout_centerVertical="true"
        android:layout_alignBaseline="@+id/tvDfo_Rid_child"
        android:layout_alignBottom="@+id/tvDfo_Rid_child"
        android:layout_toLeftOf="@+id/tvDfo_Rid_child"
        android:layout_marginRight="5dp"
        android:text="TextView" />

    <TextView
        android:id="@+id/tvDfo_Kprice_child"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
      android:layout_centerVertical="true"
        android:layout_marginRight="14dp"
        android:layout_toLeftOf="@+id/tvDfo_Qty_child"
        android:text="TextView" />

    <TextView
        android:id="@+id/tvDfo_Sumprice_child"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
      android:layout_centerVertical="true"
        android:layout_marginRight="14dp"
        android:text="TextView" />

</RelativeLayout>

hfo_listiem.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="#cb9216" >

    <ImageView
        android:id="@+id/ivgroup"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:src="@drawable/abc_btn_check_to_on_mtrl_000" />

    <TextView
        android:id="@+id/tvHfo_Gcode_group"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_margin="5dp"
        android:layout_toLeftOf="@+id/ivgroup"
        android:text="TextView"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/tvHfo_Ttem_group"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_margin="5dp"
        android:layout_toLeftOf="@+id/tvHfo_Gcode_group"
        android:text="TextView"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/tvHfo_SumKprice_group"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_margin="5dp"
        android:layout_toLeftOf="@+id/tvHfo_Ttem_group"
        android:text="TextView"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/tvHfo_tv4_group"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_margin="5dp"
        android:layout_toLeftOf="@+id/tvHfo_SumKprice_group"
        android:text=""
        android:textColor="#000000" />

</RelativeLayout>

fact.xml

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ExpandableListView
        android:id="@+id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="36dp"
        android:layout_above="@+id/button1"
        android:layout_alignParentTop="true" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:text="show data" />

    <TextView
        android:id="@+id/android:empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_above="@+id/button1"
        android:text="@string/app_name" />

</RelativeLayout>

DFoStruct.java

      int DFo_HID;  
      int Dfo_RID;  
      String   Dfo_KCode;
      Double Dfo_KQty1;
      Double  Dfo_KQty2; 
      Double Dfo_RUnit1_2;
      String   Dfo_KPrice;
      String Dfo_KTakh;
      String Dfo_KTax;
      String Dfo_SumPrice;
      String Dfo_KDesc;
      Boolean State;

HFoStruct.java

int HFoID;
int HFoNu;
String FoKind;
String HFo_GCode;
int HFoItem;
String HFoDesc;
String HFoTarikh;
String HFoUn_Takh;
String HFoK_Takh;
String HFoTot_Takh;
String HFoTot_Tax;
String HFoSum_KPrice;
String HFoTot_Price;
String IsSend;
String SendDate;
Boolean state;

我的列表活动:

    public class Listsefaresh_Activity extends ExpandableListActivity {
        private static final String LOG_TAG = "ElistCBox2";
        private ExpandableListAdapter expListAdapter;

        Button b;
        ArrayList<String> groupNames;
        ArrayList<ArrayList<Color>> colors;
        ArrayList<Color> color;

        ArrayList<ArrayList<HFoStruct>> hFoStructs;
        ArrayList<HFoStruct> hFoStruct;
    //  List<GuyStruct> hfolist;
        List<HFoStruct> hfolist;

        ArrayList<ArrayList<DFoStruct>> dFoStructs;
        ArrayList<DFoStruct> dFoStruct;
        List<DFoStruct> dfolist;

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            setContentView(R.layout.factor_list);

            try {
                hFoStructs = new ArrayList<ArrayList<HFoStruct>>();
                hFoStruct = new ArrayList<HFoStruct>(); 
                dFoStructs = new ArrayList<ArrayList<DFoStruct>>();
                dFoStruct = new ArrayList<DFoStruct>();

                G.HFoList.addAll(G.dbHelper.getAllHfo());
                hfolist = G.HFoList;

                G.DFoList.addAll(G.dbHelper.getAllDfo());
                dfolist = G.DFoList;

                for (int i = 0; i < hfolist.size(); i++) {
                    hFoStruct.add(hfolist.get(i));
                    Toast.makeText(Listsefaresh_Activity.this, String.valueOf(hFoStruct.get(i).getHFo_GCode()), Toast.LENGTH_LONG).show();
                    for (int j = 0; j < dfolist.size(); j++) {
                        if (dfolist.get(j).getDFo_HID() == hfolist.get(i).getHFoID()) {
                            dFoStruct.add(dfolist.get(j));
                            Toast.makeText(Listsefaresh_Activity.this, String.valueOf(dFoStruct.get(j).getDfo_KPrice()), Toast.LENGTH_LONG).show();
                        }
                    }
                }
                hFoStructs.add(hFoStruct);
                dFoStructs.add(dFoStruct);
    //      Toast.makeText(Listsefaresh_Activity.this, String.valueOf(dfolist.size()), Toast.LENGTH_LONG).show();

                b = (Button) findViewById(R.id.button1);

                b.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        for (int i = 0; i < groupNames.size(); i++) {
                            for (int j = 0; j < colors.get(i).size(); j++) {
                                if (colors.get(i).get(j).state == true) {
                                    Toast.makeText(
                                            Listsefaresh_Activity.this,
                                            String.valueOf(groupNames.get(i)) + " "
                                                    + String.valueOf(j + 1),
                                            Toast.LENGTH_LONG).show();
                                }
                            }
                        }
                    }
                });

    //  setdata();


                expListAdapter = new Hfo_Dfo_Adabter(this, hfolist, dfolist, hFoStructs, dFoStructs);
                setListAdapter(expListAdapter);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        public void onContentChanged() {
            super.onContentChanged();
            Log.d(LOG_TAG, "onContentChanged");
        }

        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {

    }
}

Hfo_Dfo_Adabter.java

package my_adabters;

import ir.nanohesab.R;

import java.util.ArrayList;
import java.util.List;

import Struct.DFoStruct;
import Struct.HFoStruct;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

@SuppressWarnings("unused")
public class Hfo_Dfo_Adabter extends BaseExpandableListAdapter {

    private Context context;
    private List<HFoStruct> hfolist = null;
    private List<DFoStruct> dfolist = null;
    private ArrayList<ArrayList<HFoStruct>> hFoStructs = null;
    private ArrayList<ArrayList<DFoStruct>> dFoStructs = null;

    /*
     * private List<HFoStruct> HFolist = null; private List<DFoStruct> DFolist =
     * null;
     */
    private LayoutInflater inflater;

    public Hfo_Dfo_Adabter(Context context, List<HFoStruct> hfolist,
            List<DFoStruct> dfolist,
            ArrayList<ArrayList<HFoStruct>> hFoStructs,
            ArrayList<ArrayList<DFoStruct>> dFoStructs) {
        this.context = context;
        this.hfolist = hfolist;
        this.dfolist = dfolist;
        this.hFoStructs = hFoStructs;
        this.dFoStructs = dFoStructs;
        inflater = LayoutInflater.from(context);
    }

    public Object getChild(int groupPosition, int childPosition) {
        try {
            return dFoStructs.get(groupPosition).get(childPosition);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    public long getChildId(int groupPosition, int childPosition) {
        try {
            return (long) (groupPosition * 1024 + childPosition);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return (Long) null; 
    }

    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
            View v = null;  

            try {

            if (convertView != null)
                v = convertView;
            else
                v = inflater.inflate(R.layout.dfo_listitem, parent, false);
            DFoStruct d = (DFoStruct) getChild(groupPosition, childPosition);

            TextView tvDfo_Rid_child = (TextView) v
                    .findViewById(R.id.tvDfo_Rid_child);
            TextView tvDfo_Qty_child = (TextView) v
                    .findViewById(R.id.tvDfo_Qty_child);
            TextView tvDfo_Kprice_child = (TextView) v
                    .findViewById(R.id.tvDfo_Kprice_child);
            TextView tvDfo_Sumprice_child = (TextView) v
                    .findViewById(R.id.tvDfo_Sumprice_child);

            if (tvDfo_Rid_child != null)
                tvDfo_Rid_child.setText(String.valueOf(d.getDfo_RID()));
            if (tvDfo_Qty_child != null)
                tvDfo_Qty_child.setText(String.valueOf(d.getDfo_KQty1()));

            if (tvDfo_Kprice_child != null)
                tvDfo_Kprice_child.setText(String.valueOf(d.getDfo_KPrice()));
            if (tvDfo_Sumprice_child != null)
                tvDfo_Sumprice_child.setText(String.valueOf(d.getDfo_SumPrice()));

            CheckBox cb = (CheckBox) v.findViewById(R.id.checkBoxchilditem);

            cb.setChecked(d.getState());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }           
            return v;

    }

    public int getChildrenCount(int groupPosition) {
        return dFoStructs.get(groupPosition).size();
    }

    public Object getGroup(int groupPosition) {
        return hfolist.get(groupPosition);
    }

    public int getGroupCount() {
        return hfolist.size();
    }

    public long getGroupId(int groupPosition) {
        return (long) (groupPosition * 1024); // To be consistent with
                                                // getChildId
    }

    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        View v = null;
        try {

            if (convertView != null)
                v = convertView;
            else
                v = inflater.inflate(R.layout.hfo_listitem, parent, false);

            HFoStruct h = hfolist.get(groupPosition);

            TextView tvHfo_Gcode_group = (TextView) v
                    .findViewById(R.id.tvHfo_Gcode_group);
            TextView tvHfo_Ttem_group = (TextView) v
                    .findViewById(R.id.tvHfo_Ttem_group);
            TextView tvHfo_SumKprice_group = (TextView) v
                    .findViewById(R.id.tvHfo_SumKprice_group);
            TextView tvHfo_tv4_group = (TextView) v
                    .findViewById(R.id.tvHfo_tv4_group);

            if (tvHfo_Gcode_group != null)
                tvHfo_Gcode_group.setText(String.valueOf(h.getHFo_GCode()));

            if (tvHfo_Ttem_group != null)
                tvHfo_Ttem_group.setText(String.valueOf(h.getHFoItem()));

            if (tvHfo_SumKprice_group != null)
                tvHfo_SumKprice_group.setText(String.valueOf(h.getHFoSum_KPrice()));

            if (tvHfo_tv4_group != null)
                tvHfo_tv4_group.setText("");

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return v;

    }

    public boolean hasStableIds() {
        return true;
    }

    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    public void onGroupCollapsed(int groupPosition) {
    }

    public void onGroupExpanded(int groupPosition) {
    }

}


01-06 20:50:03.776: E/AndroidRuntime(1337): FATAL EXCEPTION: main
    01-06 20:50:03.776: E/AndroidRuntime(1337): Process: ir.nanohesab, PID: 1337
    01-06 20:50:03.776: E/AndroidRuntime(1337): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
    01-06 20:50:03.776: E/AndroidRuntime(1337):     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
    01-06 20:50:03.776: E/AndroidRuntime(1337):     at java.util.ArrayList.get(ArrayList.java:308)
    01-06 20:50:03.776: E/AndroidRuntime(1337):     at my_adabters.Hfo_Dfo_Adabter.getChildrenCount(Hfo_Dfo_Adabter.java:102)
    01-06 20:50:03.776: E/AndroidRuntime(1337):     at android.widget.ExpandableListConnector.refreshExpGroupMetadataList(ExpandableListConnector.java:563)
    01-06 20:50:03.776: E/AndroidRuntime(1337):     at android.widget.ExpandableListConnector.expandGroup(ExpandableListConnector.java:688)
    01-06 20:50:03.776: E/AndroidRuntime(1337):     at android.widget.ExpandableListView.handleItemClick(ExpandableListView.java:691)
    01-06 20:50:03.776: E/AndroidRuntime(1337):     at android.widget.ExpandableListView.performItemClick(ExpandableListView.java:651)
    01-06 20:50:03.776: E/AndroidRuntime(1337):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2904)
    01-06 20:50:03.776: E/AndroidRuntime(1337):     at android.widget.AbsListView$3.run(AbsListView.java:3638)
    01-06 20:50:03.776: E/AndroidRuntime(1337):     at android.os.Handler.handleCallback(Handler.java:733)
    01-06 20:50:03.776: E/AndroidRuntime(1337):     at android.os.Handler.dispatchMessage(Handler.java:95)
    01-06 20:50:03.776: E/AndroidRuntime(1337):     at android.os.Looper.loop(Looper.java:136)
    01-06 20:50:03.776: E/AndroidRuntime(1337):     at android.app.ActivityThread.main(ActivityThread.java:5045)
    01-06 20:50:03.776: E/AndroidRuntime(1337):     at java.lang.reflect.Method.invokeNative(Native Method)
    01-06 20:50:03.776: E/AndroidRuntime(1337):     at java.lang.reflect.Method.invoke(Method.java:515)
    01-06 20:50:03.776: E/AndroidRuntime(1337):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    01-06 20:50:03.776: E/AndroidRuntime(1337):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    01-06 20:50:03.776: E/AndroidRuntime(1337):     at dalvik.system.NativeStart.main(Native Method)
    01-06 20:50:05.324: E/InputDispatcher(466): channel '4aa23a4c ir.nanohesab/ir.nanohesab.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
    01-06 20:50:05.324: E/InputDispatcher(466): channel '4aa38234 ir.nanohesab/new_job.Listsefaresh_Activity (server)' ~ Channel is unrecoverably broken and will be disposed!
    01-06 20:50:05.324: E/InputDispatcher(466): channel '4aa38dec Toast (server)' ~ Channel is unrecoverably broken and will be disposed!

我想要显示数据到消费列表,当我点击按钮我得到所有检查项目。

1 个答案:

答案 0 :(得分:0)

问题在于这个方法:

public int getChildrenCount(int groupPosition) {
        return dFoStructs.get(groupPosition).size();
}

dFoStructs只有一个元素。 如果您尝试使用索引1获取值,则必须指向ArrayIndexOutOfBoundsException,因为Array从位置0开始。

如果groupPosition的计数从位置1开始,则必须注意减少索引,如下所示:

public int getChildrenCount(int groupPosition) {
            return dFoStructs.get(groupPosition-1).size();
}