FATAL EXCEPTION: main
Process: com.sam.makeanoffer, PID: 13067
java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.ArrayList.get(ArrayList.java:411)
at com.sam.makeanoffer.adapters.PinnedListAdapter.getView(PinnedListAdapter.java:78)
at com.sam.makeanoffer.pinnedlistLib.AdapterWrapper.getView(AdapterWrapper.java:154)
at com.sam.makeanoffer.pinnedlistLib.AdapterWrapper.getView(AdapterWrapper.java:16)
at android.widget.AbsListView.obtainView(AbsListView.java:2367)
at android.widget.ListView.makeAndAddView(ListView.java:1972)
at android.widget.ListView.fillDown(ListView.java:704)
at android.widget.ListView.fillFromTop(ListView.java:765)
at android.widget.ListView.layoutChildren(ListView.java:1716)
at com.sam.makeanoffer.pinnedlistLib.WrapperViewList.layoutChildren(WrapperViewList.java:193)
at android.widget.AbsListView.onLayout(AbsListView.java:2161)
at android.view.View.layout(View.java:17535)
at android.view.ViewGroup.layout(ViewGroup.java:5616)
at com.sam.makeanoffer.pinnedlistLib.StickyListHeadersListView.onLayout(StickyListHeadersListView.java:265)
at android.view.View.layout(View.java:17535)
at android.view.ViewGroup.layout(ViewGroup.java:5616)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
at android.view.View.layout(View.java:17535)
at android.view.ViewGroup.layout(ViewGroup.java:5616)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
at android.view.View.layout(View.java:17535)
at android.view.ViewGroup.layout(ViewGroup.java:5616)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17535)
at android.view.ViewGroup.layout(ViewGroup.java:5616)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
at android.view.View.layout(View.java:17535)
at android.view.ViewGroup.layout(ViewGroup.java:5616)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17535)
at android.view.ViewGroup.layout(ViewGroup.java:5616)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
at android.view.View.layout(View.java:17535)
at android.view.ViewGroup.layout(ViewGroup.java:5616)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:724)
at android.view.View.layout(View.java:17535)
at android.view.ViewGroup.layout(ViewGroup.java:5616)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2354)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2081)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1258)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6348)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
at android.view.Choreographer.doCallbacks(Choreographer.java:683)
at android.view.Choreographer.doFrame(Choreographer.java:619)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)

我使用的是PinnedSectionListView,它具有标题和子标题的自定义布局。我在这里遇到的问题是所有子布局都附加到第一个标题,而第二个标题没有显示。在我的jsonarray我有2个标题,每个都有单个孩子。我试图调试代码,发现' j' for循环工作两次而没有进入' i' for循环。这就是为什么两个子布局都附加到第一个标题的原因。
public class PinnedListAdapter extends BaseAdapter implements StickyListHeadersAdapter, SectionIndexer {
private final Context mContext;
private LayoutInflater mInflater;
private ArrayList<HeaderModel> arrayList = new ArrayList<>();
private ArrayList<ChildModel> childList = new ArrayList<>();
public static int prepos=-1;
private SharedPref pref;
public PinnedListAdapter(Context context, ArrayList<HeaderModel> arrayList) {
this.mContext = context;
this.arrayList = arrayList;
this.childList = childList;
mInflater = LayoutInflater.from(context);
pref = new SharedPref(mContext);
}
@Override
public int getCount() {
return arrayList.size();
}
@Override
public Object getItem(int position) {
return arrayList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.multicartchildrow, parent, false);
holder.myimage = (ImageView) convertView.findViewById(R.id.myimage);
holder.titletxt = (TextView) convertView.findViewById(R.id.titletxt);
holder.qtytxt = (TextView) convertView.findViewById(R.id.qtytxt);
holder.pricetxt = (TextView) convertView.findViewById(R.id.pricetxt);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ChildModel model = arrayList.get(position).getChildList().get(position);
//ChildModel model = childList.get(position);
holder.titletxt.setText(model.getChildtitle());
holder.qtytxt.setText(model.getChildqty());
holder.pricetxt.setText(pref.getString(Utility.CURRENCY)+" "+model.getChildprice());
Glide.with(mContext).load(model.getChildimage()).dontAnimate().into(holder.myimage);
return convertView;
}
@Override
public View getHeaderView(final int position, View convertView, ViewGroup parent) {
final HeaderViewHolder holder;
if (convertView == null) {
holder = new HeaderViewHolder();
convertView = mInflater.inflate(R.layout.multicartheaderrow, parent, false);
holder.RL_root = (RelativeLayout) convertView.findViewById(R.id.RL_root);
holder.image = (CircleImageView) convertView.findViewById(R.id.image);
holder.nametxt = (TextView) convertView.findViewById(R.id.nametxt);
holder.select = (CheckBox) convertView.findViewById(R.id.select);
convertView.setTag(holder);
} else {
holder = (HeaderViewHolder) convertView.getTag();
}
final HeaderModel model = arrayList.get(position);
holder.nametxt.setText(model.getHeadertext());
Glide.with(mContext).load(model.getHeaderimage()).dontAnimate().into(holder.image);
holder.select.setTag(position);
/*holder.select.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) {
//holder.select.setChecked(true);
model.setChecked(true);
notifyDataSetChanged();
}else{
//holder.select.setChecked(false);
model.setChecked(false);
notifyDataSetChanged();
}
}
});*/
System.out.println("sammy_position "+position);
System.out.println("sammy_prepos "+prepos);
if(position == prepos){
holder.select.setChecked(true);
pref.setString(Utility.MULTI_SELLLER_ID, model.getHeaderid());
}else{
holder.select.setChecked(false);
}
holder.select.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.select.setChecked(true);
if(holder.select.isChecked()){
prepos = position;
}else{
prepos = -1;
}
notifyDataSetChanged();
}
});
return convertView;
}
/**
* Remember that these have to be static, postion=1 should always return
* the same Id that is.
*/
@Override
public long getHeaderId(int position) {
// return the first character of the country as ID because this is what
// headers are based upon
return arrayList.get(position).getHeadertext().subSequence(0, 1).charAt(0);
}
@Override
public int getPositionForSection(int section) {
return 0;
}
@Override
public int getSectionForPosition(int position) {
return 0;
}
@Override
public Object[] getSections() {
return null;
}
/*public void clear() {
mCountries = new String[0];
mSectionIndices = new int[0];
mSectionLetters = new Character[0];
notifyDataSetChanged();
}
public void restore() {
mCountries = mContext.getResources().getStringArray(R.array.countries);
mSectionIndices = getSectionIndices();
mSectionLetters = getSectionLetters();
notifyDataSetChanged();
}*/
class HeaderViewHolder {
RelativeLayout RL_root;
CircleImageView image;
TextView nametxt;
CheckBox select;
}
class ViewHolder {
ImageView myimage;
TextView titletxt, qtytxt, pricetxt;
}
}
&#13;
{
"all_cart_products": [
{
"seller_id": "1",
"seller_name": "Supriya ghosh",
"email": "abc@gmail.com",
"seller_image": "http:\/\/www.example.com\/upload\/userimage\/1493023436_ajeet_1000016806.jpg",
"products": [
{
"id": "9",
"product_user_id": "1",
"name": "antique5",
"desc": "Scads dsvdjkshvdsv dsvjhdsvdsv dsjkvhdjksvds vdskhjvndsvd svdjshnvjkdsv dsvkdjsh",
"quantity": "1",
"unit_price": "3200.00",
"total_price1": 3200,
"total_price": "3200.00",
"itemImage": "http:\/\/www.example.com\/upload\/product\/1492757990myprod.jpg",
"totalquantity": "99"
}
]
},
{
"seller_id": "3",
"seller_name": "Somnath Pal",
"email": "xyz@gmail.com",
"seller_image": "http:\/\/www.example.com\/upload\/userimage\/1492766252IMG_20150325_174836~3.jpg",
"products": [
{
"id": "44",
"product_user_id": "3",
"name": "Roses",
"desc": "fresh flowers",
"quantity": "0",
"unit_price": "26.00",
"total_price1": 0,
"total_price": "0.00",
"itemImage": "http:\/\/www.example.com\/upload\/product\/1493017537myprod.jpg",
"totalquantity": "100"
}
]
}
],
"total_qty": 0,
"Ack": 1
}
&#13;
StringRequest stringRequest = new StringRequest(Request.Method.POST, Utility.GETFULLCART,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
pd.dismiss();
System.out.println("sammy_MultiCartActivity_getAllCartProducts_response "+response);
try {
JSONObject jObj = new JSONObject(response);
if(jObj.getInt("Ack")==1){
JSONArray jsonArray = jObj.getJSONArray("all_cart_products");
System.out.println("sammy_headerArray "+jsonArray.length());
for(int i=0;i<jsonArray.length();i++){
JSONObject main = jsonArray.getJSONObject(i);
JSONArray jarr = main.getJSONArray("products");
System.out.println("sammy_childArray "+jarr.length()); for(int j=0; j<jarr.length(); j++){
JSONObject obj = jarr.getJSONObject(j);
ChildModel childModel = new ChildModel();
childModel.setChildimage(obj.getString("itemImage"));
childModel.setChildtitle(obj.getString("name"));
childModel.setChildqty(obj.getString("quantity"));
childModel.setChildprice(obj.getString("total_price"));
childList.add(childModel);
}
HeaderModel model = new HeaderModel();
model.setHeaderimage(main.getString("seller_image"));
model.setHeadertext(main.getString("seller_name"));
model.setHeaderid(main.getString("seller_id"));
model.setChildList(childList);
arrayList.add(model);
}
}
mAdapter.notifyDataSetChanged();
} catch (JSONException e) {
pd.dismiss();
// JSON error
System.out.println("sammy_JSONError "+e);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
pd.dismiss();
if (error instanceof TimeoutError) {
Toast.makeText(MultiCartActivity.this,getString(R.string.tooslow),Toast.LENGTH_LONG).show();
}else if (error instanceof NoConnectionError){
Toast.makeText(MultiCartActivity.this,getString(R.string.nointernet),Toast.LENGTH_LONG).show();
}else if (error instanceof AuthFailureError) {
System.out.println("sammy_AuthFailureError "+error);
} else if (error instanceof ServerError) {
System.out.println("sammy_ServerError "+error);
} else if (error instanceof NetworkError) {
System.out.println("sammy_NetworkError "+error);
} else if (error instanceof ParseError) {
System.out.println("sammy_ParseError "+error);
}
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("user_id", pref.getString(Utility.USERID));
System.out.println("sammy_MultiCartActivity_getAllCartProducts_params "+params);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(MultiCartActivity.this);
requestQueue.add(stringRequest);
stringRequest.setRetryPolicy(new DefaultRetryPolicy(
10000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
}
&#13;
答案 0 :(得分:0)
您的代码似乎没问题。我亲自尝试过它的工作原理。请参阅下面的output
。
清洁并再次运行 project
。
以下是工作代码:
public void parseJson() {
// Your JSON
String response = "{\n" +
" \"all_cart_products\": [\n" +
" {\n" +
" \"seller_id\": \"1\",\n" +
" \"seller_name\": \"Supriya ghosh\",\n" +
" \"email\": \"abc@gmail.com\",\n" +
" \"seller_image\": \"http:\\/\\/www.example.com\\/upload\\/userimage\\/1493023436_ajeet_1000016806.jpg\",\n" +
" \"products\": [\n" +
" {\n" +
" \"id\": \"9\",\n" +
" \"product_user_id\": \"1\",\n" +
" \"name\": \"antique5\",\n" +
" \"desc\": \"Scads dsvdjkshvdsv dsvjhdsvdsv dsjkvhdjksvds vdskhjvndsvd svdjshnvjkdsv dsvkdjsh\",\n" +
" \"quantity\": \"1\",\n" +
" \"unit_price\": \"3200.00\",\n" +
" \"total_price1\": 3200,\n" +
" \"total_price\": \"3200.00\",\n" +
" \"itemImage\": \"http:\\/\\/www.example.com\\/upload\\/product\\/1492757990myprod.jpg\",\n" +
" \"totalquantity\": \"99\"\n" +
" }\n" +
" ]\n" +
" },\n" +
" {\n" +
" \"seller_id\": \"3\",\n" +
" \"seller_name\": \"Somnath Pal\",\n" +
" \"email\": \"xyz@gmail.com\",\n" +
" \"seller_image\": \"http:\\/\\/www.example.com\\/upload\\/userimage\\/1492766252IMG_20150325_174836~3.jpg\",\n" +
" \"products\": [\n" +
" {\n" +
" \"id\": \"44\",\n" +
" \"product_user_id\": \"3\",\n" +
" \"name\": \"Roses\",\n" +
" \"desc\": \"fresh flowers\",\n" +
" \"quantity\": \"0\",\n" +
" \"unit_price\": \"26.00\",\n" +
" \"total_price1\": 0,\n" +
" \"total_price\": \"0.00\",\n" +
" \"itemImage\": \"http:\\/\\/www.example.com\\/upload\\/product\\/1493017537myprod.jpg\",\n" +
" \"totalquantity\": \"100\"\n" +
" }\n" +
" ]\n" +
" }\n" +
" ],\n" +
" \"total_qty\": 0,\n" +
" \"Ack\": 1\n" +
"}";
try {
JSONObject jObj = new JSONObject(response);
if(jObj.getInt("Ack") == 1) {
JSONArray jsonArray = jObj.getJSONArray("all_cart_products");
System.out.println("sammy_headerArray " + jsonArray.length());
for(int i = 0; i < jsonArray.length(); i++) {
JSONObject main = jsonArray.getJSONObject(i);
JSONArray jarr = main.getJSONArray("products");
System.out.println("sammy_childArray " + jarr.length());
for(int j = 0; j < jarr.length(); j++){
JSONObject obj = jarr.getJSONObject(j);
String itemImage = obj.getString("itemImage");
String ItemName = obj.getString("name");
String quantity = obj.getString("quantity");
String totalPrice = obj.getString("total_price");
Log.d("SUCCESS", "PRODUCT: " + "\nImage: " + itemImage + "\nName: " + ItemName +
"\nQuantity: " + quantity + "\nTotal Price: " + totalPrice);
/*ChildModel childModel = new ChildModel();
childModel.setChildimage(obj.getString("itemImage"));
childModel.setChildtitle(obj.getString("name"));
childModel.setChildqty(obj.getString("quantity"));
childModel.setChildprice(obj.getString("total_price"));
childList.add(childModel);*/
}
String sellerImage = main.getString("seller_image");
String sellerName = main.getString("seller_name");
String sellerId = main.getString("seller_id");
Log.d("SUCCESS", "SELLER: " + "\nSeller Image: " + sellerImage + "\nSeller Name: " + sellerName +
"\nSeller Id: " + sellerId);
/*HeaderModel model = new HeaderModel();
model.setHeaderimage(main.getString("seller_image"));
model.setHeadertext(main.getString("seller_name"));
model.setHeaderid(main.getString("seller_id"));
model.setChildList(childList);
arrayList.add(model);*/
}
}
} catch (JSONException e) {
// JSON error
System.out.println("sammy_JSONError " + e);
}
}
<强>更新强>
您尚未在childList
新卖家inserting
之前清除childs
。
试试这个:
................
...........................
childList.clear()
for(int j = 0; j < jarr.length(); j++){
}
.............
......................
<强>输出:强>
I/System.out: sammy_headerArray 2
I/System.out: sammy_childArray 1
D/SUCCESS: PRODUCT:
Image: http://www.example.com/upload/product/1492757990myprod.jpg
Name: antique5
Quantity: 1
Total Price: 3200.00
D/SUCCESS: SELLER:
Seller Image: http://www.example.com/upload/userimage/1493023436_ajeet_1000016806.jpg
Seller Name: Supriya ghosh
Seller Id: 1
I/System.out: sammy_childArray 1
D/SUCCESS: PRODUCT:
Image: http://www.example.com/upload/product/1493017537myprod.jpg
Name: Roses
Quantity: 0
Total Price: 0.00
D/SUCCESS: SELLER:
Seller Image: http://www.example.com/upload/userimage/1492766252IMG_20150325_174836~3.jpg
Seller Name: Somnath Pal
Seller Id: 3