我有一个名为balances的ArrayList,内部余额我有一个对象和另一个我需要显示的ArrayList,下一个图像会显示给你。
在图像中,为listView形成了事务,其中每个事务都有6个显示事务属性的文本视图。
private String userID ;
private String time ;
private String date ;
private String transactionStatusCode ;
private String referenceID ;
private String amountAfterTax ;
private String description;
我读到了有关ExpandableListView的内容,但所有示例都显示了String数组。正如您将在我的代码中看到的,我有一个需要显示的事务对象。
对象由以下JSON
组成 [{
"guest": {
"surname": "BENETOIDE A.1",
"guestID": "004"
},
"transactions": [{
"userID": "AO15",
"time": "0600",
"date": "20161216",
"transactionStatusCode": "Committed",
"referenceID": "83366",
"amountAfterTax": "300.00",
"description": "RESTAURANT"
}, {
"userID": "AO15",
"time": "0600",
"date": "20161216",
"transactionStatusCode": "Committed",
"referenceID": "83654",
"amountAfterTax": "100.62",
"description": "RESTAURANT"
}, {
"userID": "AO15",
"time": "05331",
"date": "20161217",
"transactionStatusCode": "Committed",
"referenceID": "8329",
"amountAfterTax": "100.62",
"description": "ROOM SERVICE"
}, {
"userID": "S1PR",
"time": "14554",
"date": "20170107",
"transactionStatusCode": "Committed",
"referenceID": "2524",
"amountAfterTax": "636.93",
"description": "ROOM SERVICE"
}]
}, {
"guest": {
"surname": "ALDITOY A.0",
"guestID": "005"
},
"transactions": [{
"userID": "AO15",
"time": "0510",
"date": "20161222",
"transactionStatusCode": "Committed",
"referenceID": "94257",
"amountAfterTax": "300.00",
"description": "ROOM SERVICE"
}, {
"userID": "AO15",
"time": "0510",
"date": "20161222",
"transactionStatusCode": "Committed",
"referenceID": "92355",
"amountAfterTax": "33.54",
"description": "ROOM SERVICE"
}, {
"userID": "GT10",
"time": "1405",
"date": "20170109",
"transactionStatusCode": "Committed",
"referenceID": "19260",
"amountAfterTax": "302.00",
"description": "RESTAURANT"
}]
}]
我创建了一个名为BalanceAdapter的类,它从BaseExpandableListAdapter扩展,我试图像往常一样从ArrayAdapter扩展时显示项目
package com.softwareengineerandroid.davidmata.coreclasses.Balance;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ListAdapter;
import android.widget.TextView;
import com.softwareengineerandroid.davidmata.armletpaymentpilot.R;
import com.softwareengineerandroid.davidmata.coreclasses.transaction.Transaction;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by davidmata on 18/03/2017.
*/
public class BalanceAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> expanableListTitle;
private HashMap<String, ArrayList<Transaction>> expandableListDatails;
private LayoutInflater inflater;
public BalanceAdapter(Context context, List<Balance> listBalance){
inflater= (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.context = context;
expanableListTitle = new ArrayList<>();
expandableListDatails = new HashMap<>();
Map<String, ArrayList<Transaction>> map = new HashMap<>();
int i=0;
for (Balance balance:listBalance) {
expanableListTitle.add(balance.getGuest().getSurname());
map.put(balance.getGuest().getSurname()+i,(ArrayList<Transaction>) balance.getTransactions());
i++;//evita ducplicacion de nombre de huespedes.
}
this.expandableListDatails = (HashMap)map;
}
@Override
public int getGroupCount() {
return 0;
}
@Override
public int getChildrenCount(int groupPosition) {
return 0;
}
@Override
public Object getGroup(int groupPosition) {
return null;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return this.expandableListDatails.get(this.expanableListTitle.get(groupPosition)).get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return 0;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
return null;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if(convertView==null){
convertView = inflater.inflate(R.layout.list_balance_item,null);
}
Transaction transaction =(Transaction) getChild(groupPosition,childPosition);
TextView tv_description = (TextView)convertView.findViewById(R.id.listBalance_description);
TextView tv_referenceID = (TextView)convertView.findViewById(R.id.listBalance_referenceID);
TextView tv_amount = (TextView)convertView.findViewById(R.id.listBalance_amount);
TextView tv_currencycode = (TextView)convertView.findViewById(R.id.listBalance_currencyCode);
TextView tv_date = (TextView)convertView.findViewById(R.id.listBalance_date);
TextView tv_time = (TextView)convertView.findViewById(R.id.listBalance_time);
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
This is my **list_balance_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:orientation="vertical"
android:layout_height="match_parent"
android:padding="16dp"
android:divider="@null"
android:dividerHeight="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="@+id/listBalance_description"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="SERVICE ROOM"
android:textSize="20dp"
android:textStyle="bold"
/>
<TextView
android:id="@+id/listBalance_amount"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="$850.63"
android:textSize="20dp"
android:textStyle="bold"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="8dp"
>
<TextView
android:id="@+id/listBalance_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="18/AGO/2017"/>
<TextView
android:id="@+id/listBalance_time"
android:paddingLeft="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="12:08"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="8dp"
>
<TextView
android:id="@+id/listBalance_currencyCode"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="CurrencyCode: MXN"/>
<TextView
android:id="@+id/listBalance_referenceID"
android:paddingLeft="16dp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Feference : 1671"/>
</LinearLayout>
</LinearLayout>
fragment_balance.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.softwareengineerandroid.davidmata.armletpaymentpilot.FragmentBalance">
<ExpandableListView
android:id="@+id/fragBalance_expListView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ExpandableListView>
</LinearLayout>
FragmentBalace.java,我应该在哪里设置适配器。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_balance, container, false);
ListView balanceList = (ListView)view.findViewById(R.id.fragBalance_listview);
ArrayList<Balance> arrayList = null;
try {
arrayList = (ArrayList<Balance>) new BalanceController(new BalanceFromApiJson()).getBalance(getContext(),"http://192.168.1.100:50591/api/Balance?reservationID=45454655465");
BalanceAdapter balanceAdapter = new BalanceAdapter(getContext(),arrayList);
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
// TransationAdapter balanceAdapter = new TransationAdapter(getActivity(), arrayList);
// balanceList.setAdapter(balanceAdapter);
return view;
}