Android Volley Request无法使用可扩展列表视图和基本可扩展列表适配器

时间:2014-05-17 20:13:06

标签: android expandablelistview android-volley

不知道为什么我的截击请求没有显示任何东西。它不是给我失败的实例,但如果我使用带有BaseExpandableListAdapter的ExpandableListView,则成功列表器不会调用。

请参阅我的Fragment类代码:ShopCategoryFragment.java。

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;

import com.android.volley.Request.Method;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;
import com.examples.testone.domain.CategorySection;
import com.examples.testone.domain.ShopCategory;
import com.examples.testone.domain.ShopCategoryParent;
import com.examples.testone.domain.ShopCategorySection;
import com.examples.testone.domain.response.ShopCategorySectionsRestResponse;
import com.examples.testone.network.volley.GsonRequest;

public class ShopCategoryFragment extends Fragment {

// private ShopCategorySectionListAdapter shopCategorySectionListAdapter;
private RequestQueue requestQueue;
private boolean requestRunning = false;
private String volleyRequestMode = "in";
final String _appLogTag = " - ShopCategoryFragment - ";

private List<ShopCategorySectionVO> shopCategorySectionVOs;
Map<ShopCategorySectionVO, List<ShopCategory>> myMapCollection;

private ExpandableListView expListView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setRetainInstance(true);
    Log.d(_appLogTag, "Inside onCreate");
    //this.requestQueue = SmartShopVolleySettings.getRequestQueue();
    this.requestQueue = Volley.newRequestQueue(getActivity());
    startRequest();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    Log.d(_appLogTag, "Inside onCreateView");
    View viewHierarchy = 
inflater.inflate(R.layout.fragment_explist, container, false);
    // List View Settings
    expListView = 
(ExpandableListView)    viewHierarchy.findViewById(R.id.laptop_list);

    // getActivity(), this.shopCategorySectionVOs, this.collection );
    expListView.setAdapter
(new ShopCategoryExpandibleListAdapter(getActivity(), 
shopCategorySectionVOs,   myMapCollection));

    return viewHierarchy;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    Log.d(_appLogTag, "Inside onViewCreated");
}

@Override
public void onDestroy() {
    super.onResume();
    Log.d(_appLogTag, "Inside onDestroy");
    //this.requestQueue.cancelAll(this);
    //this.requestRunning = false;
    // cancelRequests();//Json shop lists
}

@Override
public void onPause() {
    super.onPause();
    Log.d(_appLogTag, "Inside onPause");
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // inflater.inflate(R.menu.simplemap_menu, menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    /*
     * switch (item.getItemId()) {
     * 
     * case R.id.simplemap_menu_info:
     * SimpleDialogFragment.createBuilder(this.getActivity(),
     * this.getActivity().getSupportFragmentManager())
     * .setMessage(Html.fromHtml
     * (getString(R.string.simplemap_info_details)))
     * .setTitle(R.string.simplemap_info_title) .show(); return true; }
     */
    return super.onOptionsItemSelected(item);
}

private void startRequest() {
Log.d(_appLogTag,"Inside on Start Request for getting the Shop Categories ");
    if ( !requestRunning ) {
        ShopCategoryFragment.this.requestRunning = true ;
String url = getString(R.string.shops_category_get_url, this.volleyRequestMode);
        Log.d(_appLogTag, "Url for json is:" + url);
GsonRequest<ShopCategorySectionsRestResponse> jsonObjectRequest = 
new GsonRequest<ShopCategorySectionsRestResponse>(Method.GET, 
url, ShopCategorySectionsRestResponse.class,
                null,
                getShopsCategoriesRequestSuccessListener(),
                getShopsCategoriesRequestErrorListener());
        jsonObjectRequest.setShouldCache(false);
//Log.i(_appLogTag, "  volley request Finished: "+
jsonObjectRequest.getBodyContentType() + " More: " + jsonObjectRequest.toString());
        this.requestQueue.add(jsonObjectRequest);
    } else if (BuildConfig.DEBUG) {
        Log.i(_appLogTag, "  volley request is already running");
    }
/*myMapCollection = new LinkedHashMap<ShopCategorySectionVO, 
List<ShopCategory>>(); // My Map
shopCategorySectionVOs = new ArrayList<ShopCategorySectionVO>(); // My Section Headers
    List<ShopCategory> shopCategories = new ArrayList<ShopCategory>();

    ShopCategorySectionVO shopCategorySectionVO1 = 
new ShopCategorySectionVO("Dog", "icon", null);
    shopCategorySectionVOs.add(shopCategorySectionVO1);

    ShopCategory sc1 = new ShopCategory();      
    Title sc1t1 = new Title();
    sc1t1.setEn("Tommy");
    sc1.setTitle(sc1t1);

    Description sc1d1 = new Description();
    sc1d1.setEn("My Kuta Tommy");       
    sc1.setDescr(sc1d1);        
    shopCategories.add(sc1);

    ShopCategory sc2 = new ShopCategory();
    Title sc1t2 = new Title();
    sc1t2.setEn("Boomer");
    sc2.setTitle(sc1t2);

    Description sc1d2 = new Description();
    sc1d2.setEn("My Kuta Boomer");

    sc2.setDescr(sc1d2);
    shopCategories.add(sc2);

    myMapCollection.put(shopCategorySectionVO1, shopCategories);*/
}

private Response.Listener<ShopCategorySectionsRestResponse> 
getShopsCategoriesRequestSuccessListener() {
    return new Response.Listener<ShopCategorySectionsRestResponse>() {
        @Override
        public void onResponse(ShopCategorySectionsRestResponse response) {

myMapCollection = new LinkedHashMap<ShopCategorySectionVO, List<ShopCategory>>(); 
    shopCategorySectionVOs = new ArrayList<ShopCategorySectionVO>();
    List<ShopCategory> shopCategories = new ArrayList<ShopCategory>();
            Log.d(_appLogTag,"Ready for going inside");
for (CategorySection categorySection :response.getShop_category_sections())   {
                Log.d(_appLogTag,"Inside Outer For");
ShopCategorySection catSection = categorySection.getShopCategorySection();
                ShopCategorySectionVO shopCategorySectionVO =
new ShopCategorySectionVO
 (catSection.getTitle(),catSection.getIcon(), 
 catSection.getBackcolor());
 shopCategorySectionVOs.add(shopCategorySectionVO);

 if (catSection.getShop_categories() != null && 
 catSection.getShop_categories().size() > 0) {
 Log.d(_appLogTag,"Inside Inner If");
for (ShopCategoryParent shopCategoryParent : catSection.getShop_categories()) {
        Log.d(_appLogTag,"Inside Inner For");
ShopCategory shopCategory = shopCategoryParent.getShopCategory();
                        shopCategories.add(shopCategory);
                    }// End of child For
}// End of IF
myMapCollection.put(shopCategorySectionVO, shopCategories); // Filling of Map
            }
            // End of Outer For
            requestRunning = false;
        }
    };
}

private Response.ErrorListener getShopsCategoriesRequestErrorListener() {
    return new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
Log.d(_appLogTag,"Inside on create Shops Request Error Listener ");
            requestRunning = false;
        }
    };
}
}

和我的BaseExpandableListAdapter。

 public class ShopCategoryExpandibleListAdapter extends BaseExpandableListAdapter {


final String _appLogTag = " - ShopCategoryExpandibleListAdapter - ";

// Define activity context
private Context mContext;

/*
 * Here we have a Hashmap containing a String key 
 * (can be Integer or other type but I was testing 
 * with contacts so I used contact name as the key)
*/ 
private Map<ShopCategorySectionVO, List<ShopCategory>> mListDataChild;

// ArrayList that is what each key in the above 
// hashmap points to
private List<ShopCategorySectionVO> mCategorySectionVOs;

// Hashmap for keeping track of our checkbox check states
private Map<Integer, boolean[]> mChildCheckStates;

// Our getChildView & getGroupView use the viewholder patter
// Here are the viewholders defined, the inner classes are
// at the bottom
private ChildViewHolder childViewHolder;
private GroupViewHolder groupViewHolder;

/*  
 *  For the purpose of this document, I'm only using a single
 *  textview in the group (parent) and child, but you're limited only
 *  by your XML view for each group item :)
*/ 
private String groupText;
private String childText;
//private String childText2;

/*  Here's the constructor we'll use to pass in our calling
 *  activity's context, group items, and child items
*/ 
public ShopCategoryExpandibleListAdapter(Context context,
List<ShopCategorySectionVO> listDataGroup, 
Map<ShopCategorySectionVO, List<ShopCategory>> listDataChild) {

    Log.d(_appLogTag, "Inside Constructor");

    mContext = context;
    mCategorySectionVOs = listDataGroup;
    mListDataChild = listDataChild;

    // Initialize our hashmap containing our check states here
    mChildCheckStates = new HashMap<Integer, boolean[]>();
}

@Override
public ShopCategory getChild(int groupPosition, int childPosition) {
Log.d(_appLogTag, "Inside getChild");
List<ShopCategory> 
 listOfChilds = mListDataChild.get(mCategorySectionVOs.get(groupPosition));
 Log.d(_appLogTag, "So total number of childs in Group number "
 +groupPosition+" are "+listOfChilds.size());
//return mListDataChild.get(mCategorySectionVOs.get(groupPosition)).get(childPosition);
    return listOfChilds.get(childPosition);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    Log.d(_appLogTag, "Inside getChild ID");
     return childPosition;
}

@Override
public View getChildView(int groupPosition, int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {
    Log.d(_appLogTag, "Inside getChild View");
    final int mGroupPosition = groupPosition;
    final int mChildPosition = childPosition;

    //  I passed a text string into an activity holding a getter/setter
    //  which I passed in through "ExpListChildItems".
    //  Here is where I call the getter to get that text
    //childText = getChild(mGroupPosition, mChildPosition).getChildText();
    ShopCategory shopCategory = getChild(mGroupPosition, mChildPosition);

    childText = shopCategory.getTitle().getEn();

    if (convertView == null) {

        LayoutInflater inflater = (LayoutInflater) this.mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.child_item, null);

        childViewHolder = new ChildViewHolder();

   childViewHolder.mChildText = (TextView) convertView.findViewById(R.id.laptop_child);


        childViewHolder.mCheckBox = (CheckBox) convertView
                .findViewById(R.id.checkBox);

        convertView.setTag(R.layout.child_item, childViewHolder);

    } else {

        childViewHolder = (ChildViewHolder) convertView
                .getTag(R.layout.child_item);
    }

    childViewHolder.mChildText.setText(childText);
    //childViewHolder.mChildText2.setText(childText2);
        /* 
     * You have to set the onCheckChangedListener to null
     * before restoring check states because each call to 
     * "setChecked" is accompanied by a call to the 
     * onCheckChangedListener
    */ 
    childViewHolder.mCheckBox.setOnCheckedChangeListener(null);

    if (mChildCheckStates.containsKey(mGroupPosition)) {
        /*
         * if the hashmap mChildCheckStates<Integer, Boolean[]> contains
         * the value of the parent view (group) of this child (aka, the key),
         * then retrive the boolean array getChecked[]
        */
        boolean getChecked[] = mChildCheckStates.get(mGroupPosition);

        // set the check state of this position's checkbox based on the 
        // boolean value of getChecked[position]
        childViewHolder.mCheckBox.setChecked(getChecked[mChildPosition]);

    } else {

        /*
        * if the hashmap mChildCheckStates<Integer, Boolean[]> does not
        * contain the value of the parent view (group) of this child (aka, the key),
        * (aka, the key), then initialize getChecked[] as a new boolean array
        *  and set it's size to the total number of children associated with 
        *  the parent group
        */
        boolean getChecked[] = new boolean[getChildrenCount(mGroupPosition)];

 // add getChecked[] to the mChildCheckStates hashmap using mGroupPosition as the key
        mChildCheckStates.put(mGroupPosition, getChecked);

        // set the check state of this position's checkbox based on the 
        // boolean value of getChecked[position]
        childViewHolder.mCheckBox.setChecked(false);
    }

 childViewHolder.mCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {

                    if (isChecked) {

                        boolean getChecked[] = mChildCheckStates.get(mGroupPosition);
                            getChecked[mChildPosition] = isChecked;
                            mChildCheckStates.put(mGroupPosition, getChecked);

                    } else {
                     boolean getChecked[] = mChildCheckStates.get(mGroupPosition);
                            getChecked[mChildPosition] = isChecked;
                            mChildCheckStates.put(mGroupPosition, getChecked);
                    }
                }
            });

    return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
Log.d(_appLogTag, "Inside getChildrenCount");
//ShopCategory childList = mListDataChild.get(mCategorySectionVOs.get(groupPosition));
    return mListDataChild.get(mCategorySectionVOs.get(groupPosition)).size();
}

@Override
public Object getGroup(int groupPosition) {
    Log.d(_appLogTag, "Inside getGroup");
    return mCategorySectionVOs.get(groupPosition);
}

@Override
public int getGroupCount() {
    Log.d(_appLogTag, "Inside getGroupCount");
    return mCategorySectionVOs.size();
}

@Override
public long getGroupId(int groupPosition) {
    Log.d(_appLogTag, "Inside getGroupId");
    return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
    //  I passed a text string into an activity holding a getter/setter
    //  which I passed in through "ExpListGroupItems".
    //  Here is where I call the getter to get that text
    //groupText = getGroup(groupPosition).getGroupText();
    Log.d(_appLogTag, "Inside getGroupView");
    groupText = ((ShopCategorySectionVO)getGroup(groupPosition)).getTitle();

    if (convertView == null) {

        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.group_item, null);

        // Initialize the GroupViewHolder defined at the bottom of this document
groupViewHolder = new GroupViewHolder();

//groupViewHolder.mGroupText = (TextView) convertView.findViewById(R.id.groupTextView);
groupViewHolder.mGroupText = (TextView) convertView.findViewById(R.id.laptop_head);

        convertView.setTag(groupViewHolder);
    } else {

        groupViewHolder = (GroupViewHolder) convertView.getTag();
    }

    groupViewHolder.mGroupText.setText(groupText);

    return convertView;
}

@Override
public boolean hasStableIds() {
    Log.d(_appLogTag, "Inside hasStableIds");
    return false;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    Log.d(_appLogTag, "Inside isChildSelectable");
    return false;
}


 public final class GroupViewHolder {

    TextView mGroupText;
 }

 public final class ChildViewHolder {

    TextView mChildText;
    TextView mChildText2;
    CheckBox mCheckBox;
 }
 }

没有任何意义如果我只是禁用适配器,则会在我的域类中获取成功并加载数据。

1 个答案:

答案 0 :(得分:0)

我认为错误在于您希望使用尚未加载的数据填充ExpandableListView。在你的onCreateView中,myMapCollection仍然是空的。它会在凌空结束asyncTask之后加载一段时间。当你成功完成加载数据的响应时,你需要刷新你的用户界面。