我无法理解为什么我的ExpandableListView
没有扩展。我已经多次检查过我的代码但无法找到任何想法。我已将我的代码与其他教程进行了比较,但无法找到解决方案。任何人都提前帮助我。
这是我的代码
public class All_Offers extends Fragment {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
private ProgressDialog sweetProgressDialog;
public static final String URL = "https://some link";
ArrayList<Offer_Model> Offers;
ArrayList<Offer_Model> Name;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
sweetProgressDialog = new ProgressDialog(getContext(), R.style.MyAlertDialogStyle);
sweetProgressDialog.setMessage(String.format(getResources().getString(R.string.loading)));
sweetProgressDialog.setCancelable(false);
View v = inflater.inflate(R.layout.activity_all__offers, container, false);
Offers = new ArrayList<>();
Name = new ArrayList<>();
expListView = (ExpandableListView) v.findViewById(R.id.lvExp);
// Listview Group click listener
expListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
Log.d("onGroupClick:", "worked");
parent.expandGroup(groupPosition);
return false;
}
});
// Listview Group expanded listener
expListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
}
});
// Listview Group collasped listener
expListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
}
});
// Listview on child click listener
expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// child click
return false;
}
});
show_Offers();
return v;
}
private void show_Offers() {
RequestQueue queue = Volley.newRequestQueue(getContext());
StringRequest myReq = new StringRequest(Request.Method.POST,
URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if (sweetProgressDialog.isShowing()) {
sweetProgressDialog.dismiss();
}
if (response.isEmpty() || response == null || response.equals("0")) {
sweetProgressDialog.dismiss();
Toast.makeText(getActivity(), "Error Loading Restaurants, check your connection.", Toast.LENGTH_LONG).show();
return;
}
try {
//sweetProgressDialog.dismiss();
Log.e("tag", "response " + response);
JSONArray array = new JSONArray(response);
for (int i = 0; i < array.length(); i++)
{
JSONObject jObj = (JSONObject) array.get(i);
Offers.add(new Offer_Model(jObj.getString("percentOff"), jObj.getString("details"), jObj.getString("restaurantID"), jObj.getString("restaurantName")));
Log.e("jObj", String.valueOf(jObj));
Name.add(new Offer_Model(jObj.getString("name")));
Log.e("Offers value", String.valueOf(Offers));
Log.e("Name value", String.valueOf(Name));
}
listAdapter = new Offer_adaptor(getActivity(), Name,Offers);
// setting list adapter
expListView.setAdapter(listAdapter);
} catch (JSONException e) {
if (sweetProgressDialog.isShowing()) {
sweetProgressDialog.dismiss();
}
Log.i("myTag", e.toString());
Toast.makeText(getContext(), "Parsing error", Toast.LENGTH_SHORT).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("myTag", error.toString());
if (sweetProgressDialog.isShowing()) {
sweetProgressDialog.dismiss();
}
Toast.makeText(getContext(), "Server Error", Toast.LENGTH_SHORT).show();
}
}) {
myReq.setRetryPolicy(new DefaultRetryPolicy(
20000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT
));
myReq.setShouldCache(false);
queue.add(myReq);
sweetProgressDialog = new ProgressDialog(getContext());
sweetProgressDialog.setMessage("Loading Offers...");
sweetProgressDialog.show();
}
}
这是我的Adapter
课程,我正在设置小组和子级数据以及其他一些功能
public class Offer_adaptor extends BaseExpandableListAdapter {
private Context _context;
// header titles
private ArrayList<Offer_Model> _listDataHeader;
// child data in format of header title, child title
private ArrayList _listDataChild;
public Offer_adaptor(Context context, ArrayList<Offer_Model> listDataHeader, ArrayList<Offer_Model> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return (int) this._listDataChild.get(_listDataHeader.size());
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
Offer_Model childText = (Offer_Model) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.offer_child_list_item, null);
}
TextView percentOff = (TextView) convertView.findViewById(R.id.percentOff_et);
percentOff.setText(childText.getDetails());
TextView details = (TextView) convertView.findViewById(R.id.details_et);
details.setText(childText.getPercentOff());
TextView restaurantID = (TextView) convertView.findViewById(R.id.restaurantID_et);
restaurantID.setText(childText.getRestaurantID());
TextView restaurantName = (TextView) convertView.findViewById(R.id.restaurantName_et);
restaurantName.setText(childText.getRestaurantName());
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return (int) this._listDataChild.get(_listDataChild.size());
}
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
return _listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
Offer_Model headerTitle = (Offer_Model) getGroup(groupPosition);
RecyclerView.ViewHolder holder = null;
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.offer_cardview, null);
}
ImageView Tic_img = (ImageView) convertView.findViewById(R.id.tic_img);
ImageView Share_img = (ImageView) convertView.findViewById(R.id.share_img);
ImageView lblListHeader = (ImageView) convertView.findViewById(R.id.img_switcher);
TextView Offer_Detail_Tv = (TextView) convertView.findViewById(R.id.offer_detail_tv);
Offer_Detail_Tv.setText(headerTitle.getName());
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}