我需要一些帮助。我正在尝试实现可扩展列表视图。我的问题是,当有超过2个子项时,如果组布局上没有文本视图和按钮可见,则只有组必须展开。请仔细阅读我的代码和附件中的图片。当有两个以上的孩子时,文本视图和按钮是可见的。
Menu.java
public class SubMenu extends BaseActivity {
String item;
ExpandableListView sec;
Toolbar carttoolbar;
private int lastExpandedPosition = -1;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sub_menu);
carttoolbar = (Toolbar) findViewById(R.id.carttoolbar);
setSupportActionBar(carttoolbar);
assert getSupportActionBar() != null;
final Drawable upArrow = getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
upArrow.setColorFilter(getResources().getColor(R.color.white), PorterDuff.Mode.SRC_ATOP);
getSupportActionBar().setHomeAsUpIndicator(upArrow);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
String catname = getIntent().getStringExtra("CatName");
getSupportActionBar().setTitle(catname);
sec = (ExpandableListView) findViewById(R.id.seondlst);
sec.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
if (lastExpandedPosition != -1
&& groupPosition != lastExpandedPosition) {
sec.collapseGroup(lastExpandedPosition);
}
lastExpandedPosition = groupPosition;
}
});
Intent hello = getIntent();
item = hello.getStringExtra("Catergory");
loaditems();
}
private void loaditems() {
String url = Constant.commonurlyell + "data_standard_item_ind_new.php?rname=standardtakeaway&id=" + item;
Log.d("ITems", url);
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading...");
progressDialog.show();
JsonObjectRequest arrayRequest = new JsonObjectRequest(Request.Method.GET, url, (String) null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
ArrayList<Subitem_base> list = new ArrayList<Subitem_base>();
ArrayList<Subitem_base_child> ch_list = new ArrayList<Subitem_base_child>();
try {
JSONArray itemsarray = response.getJSONArray("items");
Map<Integer, ArrayList<Subitem_base_child>> ch_lists = new HashMap<Integer, ArrayList<Subitem_base_child>>();
for (int i = 0; i < itemsarray.length(); i++) {
ch_list = new ArrayList<Subitem_base_child>();
JSONObject itemsobj = itemsarray.getJSONObject(i);
Subitem_base subitem = new Subitem_base();
subitem.setGroupitemname(itemsobj.getString("BaseName"));
JSONArray subitemsarray=itemsobj.getJSONArray("subitems");
for (int j = 0; j <subitemsarray.length() ; j++) {
JSONObject subitemsobj=subitemsarray.getJSONObject(j);
Subitem_base_child subitembase=new Subitem_base_child();
subitembase.setChilditemname(subitemsobj.getString("SubItemdesc"));
String pricevalue = "£";
subitembase.setChilditemprice(pricevalue.concat(subitemsobj.getString("SubItemprice")));
ch_list.add(subitembase);
}
ch_lists.put(i, ch_list);
subitem.setItems(ch_lists.get(i));
list.add(subitem);
}
SubItem_ExpandAdapter subexpand = new SubItem_ExpandAdapter(getApplicationContext(), list,SubMenu.this);
int childCount=sec.getChildCount();
Log.d("Std", String.valueOf(childCount));
sec.setAdapter(subexpand);
subexpand.notifyDataSetChanged();
progressDialog.hide();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("SecondleveError", String.valueOf(error));
}
});
arrayRequest.setRetryPolicy(new DefaultRetryPolicy(50000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
AppController.getInstance().addToRequestQueue(arrayRequest);
}
}
MenuAdapter.java
public class SubItem_ExpandAdapter extends BaseExpandableListAdapter {
Context subcontext;
private ArrayList<Subitem_base> subitem_bases;
private SharedPreferences loginPreferences;
private SharedPreferences.Editor loginPrefsEditor;
String itembasename, itemde, subtmsub, subitempri;
private Activity parentActivity;
public SubItem_ExpandAdapter(Context subcontext, ArrayList<Subitem_base> subitem_bases, Activity parentactivity) {
this.subcontext = subcontext;
this.subitem_bases = subitem_bases;
this.parentActivity=parentactivity;
}
static class Groupname {
private TextView group_name;
private TextView groupitem_price;
private Button grouporder;
private ImageView droparrow;
}
static class Childnames {
private TextView item_name;
private TextView item_price;
private Button order;
}
@Override
public int getGroupCount() {
return subitem_bases.size();
}
@Override
public int getChildrenCount(int groupPosition) {
ArrayList<Subitem_base_child> subitem_base_children = subitem_bases.get(groupPosition).getItems();
return subitem_base_children.size();
}
@Override
public Object getGroup(int groupPosition) {
return subitem_bases.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
ArrayList<Subitem_base_child> subitem_base_children = subitem_bases.get(groupPosition).getItems();
return subitem_base_children.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 true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
Groupname groupname;
if (convertView == null) {
convertView = LayoutInflater.from(subcontext).inflate(R.layout.group_item, parent, false);
groupname = new Groupname();
groupname.group_name = (TextView) convertView.findViewById(R.id.group_name);
groupname.groupitem_price = (TextView) convertView.findViewById(R.id.groupitem_price);
groupname.grouporder = (Button) convertView.findViewById(R.id.grouporder);
groupname.droparrow = (ImageView) convertView.findViewById(R.id.droparrow);
convertView.setTag(groupname);
} else {
groupname = (Groupname) convertView.getTag();
}
Subitem_base groupitem = subitem_bases.get(groupPosition);
groupname.group_name.setText(groupitem.getGroupitemname());
if (isExpanded){
String[] elements = {groupitem.getGroupitemname() };
for (String s: elements) {
itembasename=s;
}
}
if (getChildrenCount(groupPosition)>=2) {
Log.d("Std", "called");
}else {
groupname.groupitem_price.setVisibility(View.VISIBLE);
groupname.droparrow.setVisibility(View.GONE);
groupname.grouporder.setVisibility(View.VISIBLE);
}
return convertView;
}
@Override
public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final Childnames childname;
if (convertView == null) {
convertView = LayoutInflater.from(subcontext).inflate(R.layout.child_item, parent, false);
childname = new Childnames();
childname.item_name = (TextView) convertView.findViewById(R.id.item_name);
childname.item_price = (TextView) convertView.findViewById(R.id.item_price);
childname.order = (Button) convertView.findViewById(R.id.order);
convertView.setTag(childname);
}else {
childname= (Childnames) convertView.getTag();
}
Subitem_base_child childitem = (Subitem_base_child) getChild(groupPosition, childPosition);
if (childitem.getChilditemname().isEmpty()){
childname.item_name.setVisibility(View.GONE);
}else {
childname.item_name.setText(childitem.getChilditemname());
}
childname.item_price.setText(childitem.getChilditemprice());
loginPreferences = subcontext.getSharedPreferences("loginPrefs", Context.MODE_PRIVATE);
final String status = loginPreferences.getString("Status", "");
if (status.equals("0")) {
childname.order.setBackgroundResource(R.drawable.cart_disable);
}else {
childname.order.setBackgroundResource(R.drawable.cart_button);
}
childname.order.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (status.equals("0")) {
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(parentActivity);
SharedPreferences customerid = subcontext.getSharedPreferences("loginPrefs", Context.MODE_PRIVATE);
String opentimes = customerid.getString("open", "");
alertDialogBuilder.setMessage(opentimes);
alertDialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
alertDialogBuilder.setCancelable(true);
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
} else {
String poundre = childname.item_price.getText().toString();
subtmsub=childname.item_name.getText().toString();
subitempri = poundre.replace("£", "");
addcart();
}
}
});
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
private void addcart(){
loginPreferences = subcontext.getSharedPreferences("loginPrefs", Context.MODE_PRIVATE);
loginPrefsEditor = loginPreferences.edit();
SharedPreferences customerid = subcontext.getSharedPreferences("loginPrefs", Context.MODE_PRIVATE);
loginPreferences.edit();
String customid = customerid.getString("customerid", "");
String cartdetails = itembasename + " " + subtmsub;
String cartspacong = cartdetails.replace(" ", "$");
cartspacong = cartspacong.replace("&", "and");
String addcarturl = Constant.commonurltake + "cart_process.php?userid=" + customid + "&Item=" + cartspacong + "&Itemcount=1&price=" + subitempri + "&page=items";
Log.d("Cart", addcarturl);
JsonArrayRequest cartreq = new JsonArrayRequest(Request.Method.POST, addcarturl, (String) null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
for (int i = 0; i < response.length(); i++) {
try {
JSONObject cartobj = response.getJSONObject(i);
String cartnumber = cartobj.getString("count");
loginPrefsEditor.putString("Count", cartnumber);
BaseActivity.cartnumber.setVisibility(View.VISIBLE);
BaseActivity.cartnumber.setText(cartnumber);
loginPrefsEditor.apply();
if (cartobj.has("message")) {
Toast carttost = Toast.makeText(subcontext, cartobj.getString("message"), Toast.LENGTH_SHORT);
carttost.setGravity(Gravity.CENTER, 0, 0);
carttost.show();
} else {
Toast carttost = Toast.makeText(subcontext, "Product Added To Cart", Toast.LENGTH_SHORT);
carttost.setGravity(Gravity.CENTER, 0, 0);
carttost.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(subcontext, "" + error, Toast.LENGTH_SHORT).show();
}
});
cartreq.setRetryPolicy(new DefaultRetryPolicy(10000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
AppController.getInstance().addToRequestQueue(cartreq);
}
}