如何从可扩展列表视图中获取某些孩子?

时间:2017-09-13 12:00:32

标签: java android listview expandablelistview

我正在开发一个用于下订单的Android应用程序,我有一个带有Expandable ListView的活动,我通过解析一个JSON文件来填充它。现在,在Expandable ListView的每一行中,我都有一个用于输入金额的EditText字段,并且在按钮单击时,我希望在该EditText字段中获取值为0的所有子项,并将数据放入数据库中(对于产品和已订购的金额)。

我需要帮助创建一个函数来获取这些元素,因为我最近一直在努力解决这个问题。 这是我的代码

可扩展列表适配器:

 public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private Context _context;
    private List<String> _listDataHeader; // header titles
    // child data in format of header title, child title
    private HashMap<String, List<Artikl>> _listDataChild;

    public ExpandableListAdapter(Context context, List<String> 
listDataHeader,
                                 HashMap<String, List<Artikl>> 
listChildData) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
    }

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return 
this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .get(childPosititon);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    public int getChildAmount(int groupPosition, int childPosition,View 
    convertView) {

        int kolicina;
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_item, null);
        }

        EditText amount = (EditText) 
convertView.findViewById(R.id.editText);
        kolicina=Integer.parseInt(amount.getText().toString());

        return kolicina;


    }

    @Override
    public View getChildView(int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, 
ViewGroup 
    parent) {

        final Artikl child = (Artikl) getChild(groupPosition, 
childPosition);
        final String childText = child.getNaziv();


        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_item, null);
        }

        TextView txtListChild = (TextView) convertView
                .findViewById(R.id.from_name);


        txtListChild.setText(childText);
        return convertView;
    }



    @Override
    public int getChildrenCount(int groupPosition) {
        return 
this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return this._listDataHeader.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return this._listDataHeader.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group, null);
        }

        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.lblListHeader);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

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

List_item.xml

<?xml version="1.0" encoding="utf-8"?>
     <LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
      android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingTop="2.5dp"
    android:paddingBottom="2.5dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#fff"
        android:orientation="horizontal"
        android:padding="10dp" >



        <TextView
            android:id="@+id/from_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/thumbnail"
            android:text="Pineapple"
            android:textColor="#040404"
            android:textSize="15dip"
            android:typeface="sans" />

        <LinearLayout
            android:id="@+id/cart_plus_minus_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/from_name"
            android:orientation="horizontal"
            android:weightSum="1">


            <TextView
                android:id="@+id/cena_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal|center_vertical"
                android:paddingRight="25dip"
                android:text="100 gm"
                android:textColor="#343434"
                android:textSize="12dip" />

            <TextView
                android:text="Amount"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/textView" />

            <EditText
        android:layout_width="48dp"
        android:layout_height="66dp"
        android:inputType="textPersonName"
                android:ems="10"
        android:layout_alignParentBottom="true"
        android:layout_toRightOf="@+id/from_name"
        android:layout_toEndOf="@+id/from_name"
        android:layout_marginLeft="21dp"
        android:layout_marginStart="21dp"
        android:id="@+id/editText"
        android:layout_weight="0.08"
                android:hint="0"
                android:text="0" />

        </LinearLayout>

       </RelativeLayout>

      </LinearLayout>

ListGroup.xml

  <?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="wrap_content"
    android:orientation="vertical"
    android:padding="8dp"
    android:background="#0a2859">


    <TextView
        android:id="@+id/lblListHeader"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="?
    android:attr/expandableListPreferredItemPaddingLeft"
        android:textSize="17dp"
        android:textColor="#ffffff" />

    </LinearLayout>

订单活动布局:

<?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"
    android:background="#f4f4f4" >

    <TextView android:layout_width="fill_parent"
        android:layout_height="70dp"
        android:id="@+id/market_dolg"
        android:background="#8090aa"
        android:textColor="@android:color/white"
        android:textSize="18dp"
        android:text="Hello!"
        android:gravity="center"/>

    <ExpandableListView
        android:id="@+id/lvExp"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_below="@id/market_dolg"
        android:layout_weight="1"/>


    <Button
    android:layout_width="wrap_content"
    android:layout_height="50dp"
    android:layout_below="@id/lvExp"
    android:id="@+id/otkazi"
    android:background="#9ea6a8"
    android:textColor="@android:color/white"
    android:textSize="18dp"
    android:text="Cancel Order"
    android:onClick="Otkazi_nar"
    android:gravity="center"
    android:layout_alignParentLeft="true"
    android:orientation="horizontal"

    />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_below="@id/lvExp"
        android:id="@+id/order"
        android:background="#3fa5ba"
        android:textColor="@android:color/white"
        android:textSize="18dp"
        android:text="Order"
        android:gravity="center"
        android:layout_alignParentRight="true"
        android:orientation="horizontal"

        />

    </RelativeLayout>

订单活动Java代码:

public class Order extends Activity {


private static final String TAG = Order.class.getSimpleName();
private static final String url = "artikli.json";
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
public List<Artikl> voda = new ArrayList<Artikl>();
public List<Artikl> sok = new ArrayList<Artikl>();

HashMap<String, List<Artikl>> listDataChild;

Button button;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_naracka);

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

            return false;
        }
    });


}

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

    // Adding child data
    listDataHeader.add("Вода");
    listDataHeader.add("Сок");


    // Adding child data

    JsonArrayRequest Req = new JsonArrayRequest(url,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d(TAG, response.toString());


                    // Parsing json
                    for (int i = 0; i < response.length(); i++) {
                        try {

                            JSONObject obj = response.getJSONObject(i);
                            String sifra = obj.getString("sifra");

                            if (sifra.equals("1")) {
                                Artikl artikl = new Artikl();
                                artikl.setNaziv(obj.getString("naziv"));
                                artikl.setCena(obj.getInt("cena"));

                                artikl.setSifra(sifra);
                                artikl.setEdm(obj.getString("edm"));

                                voda.add(artikl);
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }

                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(TAG, "Error: " + error.getMessage());


        }
    });

    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(Req);


    JsonArrayRequest Req1 = new JsonArrayRequest(url,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d(TAG, response.toString());


                    // Parsing json
                    for (int i = 0; i < response.length(); i++) {
                        try {

                            JSONObject obj = response.getJSONObject(i);
                            String sifra = obj.getString("sifra");
                            if (sifra.equals("2")) {
                                Artikl artikl1 = new Artikl();
                                artikl1.setNaziv(obj.getString("naziv"));
                                artikl1.setCena(obj.getInt("cena"));
                                artikl1.setSifra(obj.getString("sifra"));
                                artikl1.setEdm(obj.getString("edm"));

                                sok.add(artikl1);
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }

                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(TAG, "Error: " + error.getMessage());


        }
    });

    AppController.getInstance().addToRequestQueue(Req1);


    listDataChild.put(listDataHeader.get(0), voda);
    listDataChild.put(listDataHeader.get(1), sok);


}}

模型(Artikl.java)只有Get和Set方法

1 个答案:

答案 0 :(得分:0)

在适配器的getChildView方法中,将addTextChangedListener添加到edittext,并在afterTextChanged方法中保存在数据库中输入文本,如果只想获取数字,则限制键盘编号

  mEtReference.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            if (editable.length() > 0) {
               int value = Integer.parseInt(editable + "");
               // save value 
            }
        }
    });