0
我正在開發一個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方法
,而不是mEtReference把你EDITTEXT –
但問題是,應該有取消訂單的可能性,這就是爲什麼我想將數據保存在「訂單」按鈕單擊...有可能我可以將數據保存在其他數據結構中,然後將其傳遞給其他數據結構活動? – Janna
取消訂單意味着您要從編輯文本中刪除輸入的值,然後在afterTextChanged方法if(editable.length()> 0)int value = Integer.parseInt(editable +「」); //保存值 }其他{// //在此處刪除值 } –