- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在将数据(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/
什么是 SASS 方法要求干燥这样的东西: .content p, .content ul, .content li, .content a 最佳答案 这 4 个元素都有共同的样式吗? .conten
我正在评估 Contentful.com 作为 Angular SPA 的内容引擎。 我面临的问题是按内容类型检索条目(例如,获取“博客”类型的所有条目)。如 documentation exampl
在我编辑的主 wiki 上有一个名为 Item: 的自定义命名空间,提示是该命名空间内的每个页面都显示为 Item:This_item - - Item:That_item -- Item:Foo_i
我正在尝试编写一个Python脚本,可以将图片和pdf上传到WordPress。我希望图像上传到文件夹‘/wp-Content/Uploads/’,将pdf文件上传到文件夹‘/wp-Content/U
是否可以监控进行了多少次 Contentful API 调用,并理想地在即将超过配额时收到通知? 谢谢 最佳答案 当然,您可以在右侧用户配置文件的下拉菜单 > Organizations and Bi
我在尝试在 IE8 中下载带有分号的文件名时遇到问题。 Response.AddHeader("Content-Disposition", "attachment; filename=\"" + at
我在 Contentful Delivery API 中运行查询以返回基于它的 slug 的特定页面项目。这个查询还设置了语言环境,以便它只返回我需要呈现的语言的数据。 但是,我还需要设置页面的 hr
我有兴趣使用 Gatsby建一个Netlify使用来自 Contentful 的内容的静态网站 Netlify 有这个不错的 Gatsby 入门指南: https://www.netlify.com/
目标是提交一个 git 分支。分支的“git status”的输出是: On branch zeromq_new Your branch is up to date with 'origin/zero
我目前正在学习在 ASP.NET 3.5 和 C# 中使用 MasterPages 和 ContentPlaceHolders - 现在,我正在拼命尝试通过我的编程代码编辑 asp:Content-C
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我使用 bootstrap 3 作为我的网格框架和 CSS 来创建一个具有一个倾斜/倾斜边缘的半透明区域,但由于分层不透明度,我的元素遇到了问题。 期望是中心是倾斜的,但右侧仍然是正方形。 有没有更好
IllegalArgumentException: 未知 URL 内容:// ^ 对上述内容做了噩梦。我检查了我的变量和路径,但看不出问题是什么?非常感谢任何指点! 这是我的痕迹。 java.lan
我有两个元素:一个是 元素,另一个是 元素。 populated-drop-down extends drop-down ,但是,正如您可能已经猜到的那样,它会尝试使用一些选项预先填充它。假设我可以简
我想我也有同样的问题。 Using multiple yields to insert content 我尝试了这个解决方案。我试过 在我的 application.html.erb 中有 conte
此链接 ( https://css-tricks.com/snippets/css/a-guide-to-flexbox/ ) 表示 justify-content 和 align-content 的
我现在正在探索绑定(bind),并且有一个 NSPopUpButton - 它为我提供了一些值选择下的绑定(bind)选项 - Content , Content Objects , Content
正在尝试在内容页面中加载内容 View 。当我运行代码时,它不会出现在我的内容 View 中。我正在从我的内容页面分配两个可绑定(bind)参数。 内容页面: 内容 View :
我想从我的 :before 标签中获取 content。我知道有些人会说它不是真正的(伪)元素,但在 JS 中有一种方法,但有人可以帮助我在 JQ 中做到这一点,因为我有多个标签并且我想用 $.eac
我创建了一个.sh脚本,并将结果记录在一个文件中,执行后我会尝试将文件内容作为mail正文发送。 这是我运行的命令: sh update.sh >> update.$(date +"%Y-%m-%d:
我是一名优秀的程序员,十分优秀!