我正在将数据(Json)从服务器解析到可扩展的 ListView,其结构如下:
{
"package":[
"title":"Selection 1",
"price": 200,
"content":[stuff inside the exp listview]
]
} ,
{
"discount":[
"title:"discount 1",
"amount": 100
]
}
结果应该是这样的: Screenshot
解析数据不是问题,我有两个问题:
折扣按钮应该具有按钮行为,但只能选择(可点击)一个折扣,onClick:折扣应用于上面的 ExpandableListView
我考虑了单个按钮的 CardView,然后将其与我的 json 中的折扣金额相乘(由于适配器):
<androidx.cardview.widget.CardView
android:layout_width="91dp"
android:layout_height="132dp"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="350dp" >
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="35dp"
android:text="TextView"
tools:text="Discount" />
</androidx.cardview.widget.CardView>
但我的主要问题是 Buttons/CardView 的 OnClickBehaviour 应该将折扣应用于“确定”按钮旁边的价格。客户做出选择后,“确定”按钮应访问我的 CardView 折扣的选择。
这是适配器的列表组:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/lblListHeaderLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/default_padding"
android:orientation="horizontal"
>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingStart="?android:attr/expandableListPreferredItemPaddingLeft"
android:paddingEnd="@dimen/feed_item_padding_left_right"
android:layout_weight="0.9"
>
<TextView
android:id="@+id/lblListHeader"
android:textSize="@dimen/text_title_list_header"
android:textColor="@color/Textwhite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/mont_bold"
/>
<TextView
android:id="@+id/lblListHeader_Price"
android:textSize="@dimen/text_title_list_header"
android:textColor="@color/Textwhite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:fontFamily="@font/mont_bold"
/>
</RelativeLayout>
<Button
android:id="@+id/lblListHeaderButton"
android:layout_width="60dp"
android:layout_height="30dp"
android:background="@drawable/bg_btn_ripple_dark"
android:text="@string/btn_ok"
android:textColor="@color/Textwhite"
android:fontFamily="@font/mont_bold"
android:focusable="false"
/>
</LinearLayout>
这是我的适配器类:
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> listDataHeader,listDataHeaderPrice;
private HashMap<String,List<String>> listHashMap;
private List<Package> packageList;
public ExpandableListAdapter(Context context, List<Package> packageList) {
this.context = context;
this.packageList= packageList;
}
@Override
public int getGroupCount() {
return packageList.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return packageList.get(groupPosition).getContent().size();
}
@Override
public Object getGroup(int groupPosition) {
return listDataHeader.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return packageList.get(groupPosition).getContent();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(final int groupPosition, boolean isExpanded, View view, ViewGroup parent) {
String headerTitle = (String) packageList.get(groupPosition).getTitle();
/** HEADER TEXT HERE */
float headerPrice = packageList.get(groupPosition).getPrice();
if(view==null) {
LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.vip_package_listgroup,null);
}
final LinearLayout bgcolor = view.findViewById(R.id.lblListHeaderLayout);
TextView lblListHeader = (TextView)view.findViewById(R.id.lblListHeader);
TextView lblListHeaderPrice = (TextView)view.findViewById(R.id.lblListHeader_Price);
Button lblListHeaderButton = view.findViewById(R.id.lblListHeaderButton);
lblListHeaderButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent (context, drinks_selection.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
});
lblListHeader.setText(headerTitle);
lblListHeaderPrice.setText(String.format("%.02f",headerPrice).replace(".",",") +"USD");
return view;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view, ViewGroup parent) {
final List<String> childTest = packageList.get(groupPosition).getContent();
final String childText = childTest.get(childPosition);
if(view == null) {
LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.vip_package_listitem,null);
}
TextView txtListChild = (TextView)view.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
return view;
}
最佳答案
尝试使用RadioGroup和RadioButtons来选择折扣并在适配器内添加公共(public)方法。我假设 RadioButton 上的文本是折扣金额,所以 单选按钮的代码:
CompoundButton.OnCheckedChangeListener onCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(b) {
mainAdapter.setDiscount(Float.valueOf(compoundButton.getText().toString()));
}
}
};
rb1.setOnCheckedChangeListener(onCheckedChangeListener);
rb2.setOnCheckedChangeListener(onCheckedChangeListener);
rb3.setOnCheckedChangeListener(onCheckedChangeListener);
适配器:
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
//private List<String> listDataHeader, listDataHeaderPrice;
//private HashMap<String, List<String>> listHashMap;
private List<Package> packageList;
private float discount = 0; //Added
public ExpandableListAdapter(Context context, List<Package> packageList) {
this.context = context;
this.packageList = packageList;
}
@Override
public int getGroupCount() {
return packageList.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return packageList.get(groupPosition).getContent().size();
}
@Override
public Object getGroup(int groupPosition) {
return packageList.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return packageList.get(groupPosition).getContent().get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(final int groupPosition, boolean isExpanded, View view, ViewGroup parent) {
String headerTitle = (String) packageList.get(groupPosition).getTitle();
/** HEADER TEXT HERE */
float headerPrice = packageList.get(groupPosition).getPrice();
if (view == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.vip_package_listgroup, null);
}
LinearLayout bgcolor = view.findViewById(R.id.lblListHeaderLayout);
TextView lblListHeader = (TextView) view.findViewById(R.id.lblListHeader);
TextView lblListHeaderPrice = (TextView) view.findViewById(R.id.lblListHeader_Price);
Button lblListHeaderButton = view.findViewById(R.id.lblListHeaderButton);
lblListHeaderButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, drinks_selection.class);
intent.putExtra("discount", discount); //Added
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
});
lblListHeader.setText(headerTitle);
float endPrice = headerPrice + discount; //Added
if(endPrice < 0) endPrice = 0; //Added
lblListHeaderPrice.setText(String.format("%.02f", endPrice).replace(".", ",") + "USD"); //Changed
return view;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view, ViewGroup parent) {
List<String> childTest = packageList.get(groupPosition).getContent();
String childText = childTest.get(childPosition);
if (view == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.vip_package_listitem, null);
}
TextView txtListChild = (TextView) view.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
return view;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return false;
}
//Added this public method
public void setDiscount(float discount){
this.discount = discount;
notifyDataSetChanged();
}
}
希望有帮助!
关于java - 单击 : update content of expandable ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57524601/