Ich arbeite an einer Android-Anwendung für Bestellungen, und ich habe eine Aktivität mit einer erweiterbaren ListView, die ich durch Parsen einer JSON-Datei bevölkern. Jetzt habe ich in jeder Zeile der Expandable ListView ein EditText Feld zur Eingabe des Betrags, und auf einen Button klicken möchte ich alle Kinder, die einen Wert ungleich 0 haben in diesem EditText Feld, und die Daten in eine Datenbank legen (für die Produkte und die Menge, die bestellt wurde).Wie bekomme ich bestimmte Kinder aus einem Expandable Listview?
Ich würde eine Hilfe brauchen, um eine Funktion zu erstellen, um diese Elemente zu bekommen, seit ich damit kürzlich gekämpft habe. Hier ist mein Code
erweiterbare Liste Adapter:
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>
Bestellen Aktivität Layout:
<?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>
Sortieren Aktivität Java-Code:
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);
}}
und das Modell (Artikl.java) nur Get und Methoden Set
statt mEtReference setzen Sie EditText –
Aber die Sache ist zu bekommen, dass es sollte eine Möglichkeit geben, die Bestellung zu stornieren, und deshalb möchte ich die Daten auf der "Bestellen" -Schaltfläche speichern. Gibt es eine Möglichkeit, dass ich diese Daten in einer anderen Datenstruktur speichern und dann an die andere weitergeben kann Aktivität? – Janna
Abbrechen der Reihenfolge bedeutet, dass Sie den eingegebenen Wert aus dem Text bearbeiten rechts entfernen, dann in afterTextChanged-Methode if (editable.length()> 0) { int Wert = Integer.parseInt (bearbeitbar + ""); // Wert speichern } else {// Wert hier entfernen } –