android


IndexOutOfBoundsException when trying to populate data in recyclerview from two json arrays in a one json object


I am getting a Json Object from the server and that Json object contained two Json arrays. I know how to put data in to the recyclerview in a json array. But i don't know how to put two json array data to the recyclerview. One Json array contained rep, name, name1. Here name means name1's id(codeID). Next array contained customer(codeID) and coll (name and customer are same). I am generating the json using two different tables. I am populating the name1 in the recyclerview now successfully.
What I want is to get the name1 and its corresponding coll value. For example each row need to represent name1 and name1's coll value. Some name1 has no coll values(null). In that case coll textview need to be empty. Sometimes two arrays sizes(length) are not equal.
This is my Json.
{
"feed": [
{
"rep": "503",
"name": "30000216",
"name1": "GERLINE TYRE HOUSE (BRANCH)"
},
{
"rep": "503",
"name": "30000217",
"name1": "*G.L.P.K.Liyanage(ISURU ENTERP"
},
{
"rep": "503",
"name": "30000218",
"name1": "**Kamal Motor Stores"
},
{
"rep": "503",
"name": "30000223",
"name1": "T.Thamotherempillai & Sons"
},
{
"rep": "503",
"name": "30001037",
"name1": "Iynkarn Motors(Distributor)"
},
{
"rep": "503",
"name": "30001074",
"name1": "Northern Province MPCS FED"
},
{
"rep": "503",
"name": "30001131",
"name1": "Kishor Tyre House"
},
{
"rep": "503",
"name": "45000012",
"name1": "R.G.M.Transporters"
}
],
"feeddd": [
{
"customer": "30000220",
"coll": "92820.00"
},
{
"customer": "30000222",
"coll": "1784348.61"
},
{
"customer": "30000224",
"coll": "2661442.57"
},
{
"customer": "30000225",
"coll": "15580475.00"
},
{
"customer": "30001302",
"coll": "698968.93"
}
]
}
YourDealerListFragment class
private void getJsonRequest() {
progressView.start();
final SQLiteHandler sqLiteHandler = new SQLiteHandler(getActivity().getApplicationContext());
Cursor cr = sqLiteHandler.getData(sqLiteHandler);
cr.moveToFirst();
do {
repNo = cr.getString(0);
} while (cr.moveToNext());
cr.close();
CustomJsonObjectRequest request = new CustomJsonObjectRequest(Request.Method.POST, AppConfig.URL_JSON_DEALER_LIST, hashMap, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
progressView.stop();
try {
JSONObject jsonObject = new JSONObject(String.valueOf(response));
if (jsonObject.names().get(0).equals("feed")) {
dealerList = parseJsonResponse(response);
dlAdapter.setDealertList(dealerList);
JSONArray arrayDealerColl = response.getJSONArray("feeddd");
ArrayList<DealerCollection> dealerColList = new ArrayList<>();
for (int i = 0; i < arrayDealerColl.length(); i++) {
JSONObject obj3 = arrayDealerColl.getJSONObject(i);
String dealerName = obj3.getString("customer");
String dealerCollection = obj3.getString("coll");
DealerCollection dealerColl = new DealerCollection();
dealerColl.setDealerName(dealerName);
dealerColl.setDealerCollection(dealerCollection);
dealerColList.add(dealerColl);
dlAdapter.setDealerCollectionList(dealerColList);
}
} else {
Toast.makeText(getActivity(), "No Dealers Available", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
}) {
protected Map<String, String> getParams() throws AuthFailureError {
hashMap = new HashMap<String, String>();
hashMap.put("repNo", repNo);
return hashMap;
}
};
requestQueue.add(request);
request.setRetryPolicy(new DefaultRetryPolicy(15 * 1000, 0,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
}
private ArrayList<SuggestGetSet> parseJsonResponse(JSONObject response) {
ArrayList<SuggestGetSet> groupList = new ArrayList<>();
if (response != null || response.length() > 0) {
try {
JSONArray arrayDelaers = response.getJSONArray(KEY_FEED_NAME);
for (int i = 0; i < arrayDelaers.length(); i++) {
JSONObject currentObject = arrayDelaers.getJSONObject(i);
String rep = currentObject.getString(KEY_REP_ID);
String name = currentObject.getString(KEY_REP_NAME);
String dealerId = currentObject.getString(KEY_DEALER_ID);
SuggestGetSet delaers = new SuggestGetSet();
delaers.setId(rep);
delaers.setName(name);
delaers.setDealerId(dealerId);
groupList.add(delaers);
}
//Toast.makeText(getApplicationContext(), productList.toString(), Toast.LENGTH_LONG).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
return groupList;
}
My Adapter class
public class DealerListAdapter extends RecyclerView.Adapter<DealerListAdapter.ViewHolderDealerList> {
private LayoutInflater layoutInflater;
public Context mcontext;
private List<SuggestGetSet> dealerArrayList;
private List<SuggestGetSet> originalDealerArrayList;
private List<DealerCollection> dealerCollectionArrayList;
Typeface type;
private static String selectedRepId, selectedDealerId, selectedDealerName;
public DealerListAdapter() {
}
public DealerListAdapter(Context context) {
layoutInflater = LayoutInflater.from(context);
type = Typeface.createFromAsset(context.getAssets(), "helvr.ttf");
}
public static String getDealerName() {
return selectedDealerName;
}
public static String getDealerID() {
return selectedDealerId;
}
public static String getRepID() {
return selectedRepId;
}
public DealerListAdapter(ArrayList<SuggestGetSet> dList, Context context) {
this.mcontext = context;
layoutInflater = LayoutInflater.from(context);
this.dealerArrayList = new ArrayList<>(dList);
this.originalDealerArrayList = new ArrayList<>(dList);
type = Typeface.createFromAsset(context.getAssets(), "helvr.ttf");
}
public void setDealertList(ArrayList<SuggestGetSet> dealerAList) {
this.dealerArrayList = dealerAList;
originalDealerArrayList = new ArrayList<>(dealerAList);
notifyItemRangeChanged(0, dealerArrayList.size());
}
public void setDealerCollectionList(ArrayList<DealerCollection> dealerColList) {
this.dealerCollectionArrayList = dealerColList;
notifyItemRangeChanged(0, dealerCollectionArrayList.size());
}
#Override
public ViewHolderDealerList onCreateViewHolder(ViewGroup parent, int viewType) {
View view = layoutInflater.inflate(R.layout.custom_dealer_list_layout, parent, false);
ViewHolderDealerList viewHolderDealerList = new ViewHolderDealerList(view);
return viewHolderDealerList;
}
#Override
public void onBindViewHolder(ViewHolderDealerList holder, int position) {
final SuggestGetSet model = dealerArrayList.get(position);
holder.bind(model);
final int pos = position;
final DealerCollection model_two = dealerCollectionArrayList.get(position);
holder.bind_two(model_two);
holder.dealerName.setText(dealerArrayList.get(position).getName());
holder.dealerName.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
selectedDealerId = dealerArrayList.get(pos).getDealerId();
selectedRepId = dealerArrayList.get(pos).getId();
selectedDealerName = dealerArrayList.get(pos).getName();
}
});
if (dealerArrayList.get(position).getDealerId() == dealerCollectionArrayList.get(position).getDealerName()) {
holder.dealerCol.setText(dealerCollectionArrayList.get(position).getDealerCollection());
}
}
#Override
public int getItemCount() {
return dealerArrayList.size();
}
static class ViewHolderDealerList extends RecyclerView.ViewHolder {
private TextView dealerName, dealerCol;
public ViewHolderDealerList(View itemView) {
super(itemView);
dealerName = (TextView) itemView.findViewById(R.id.yourDelaerName);
dealerCol = (TextView) itemView.findViewById(R.id.txtDelaerCollection);
}
public void bind(SuggestGetSet model) {
dealerName.setText(model.getName());
}
public void bind_two(DealerCollection model) {
dealerCol.setText(model.getDealerCollection());
}
}
public void setFilter(List<SuggestGetSet> models) {
dealerArrayList = new ArrayList<>();
dealerArrayList.addAll(models);
notifyDataSetChanged();
}
}
My LogCat
04-29 14:06:53.529 11709-11709/com.ceatkelanisrilanka.dushanmadushanka.ceat E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.IndexOutOfBoundsException: Invalid index 10, size is 10
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
at java.util.ArrayList.get(ArrayList.java:304)
at com.ceatkelanisrilanka.dushanmadushanka.ceat.adapters.DealerListAdapter.onBindViewHolder(DealerListAdapter.java:83)
at com.ceatkelanisrilanka.dushanmadushanka.ceat.adapters.DealerListAdapter.onBindViewHolder(DealerListAdapter.java:21)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5471)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5504)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4741)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4617)
at android.support.v7.widget.LayoutState.next(LayoutState.java:100)
at android.support.v7.widget.StaggeredGridLayoutManager.fill(StaggeredGridLayoutManager.java:1538)
at android.support.v7.widget.StaggeredGridLayoutManager.onLayoutChildren(StaggeredGridLayoutManager.java:657)
at android.support.v7.widget.StaggeredGridLayoutManager.onLayoutChildren(StaggeredGridLayoutManager.java:592)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3028)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2906)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3283)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1695)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1866)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1687)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
at android.view.Choreographer.doCallbacks(Choreographer.java:555)
at android.view.Choreographer.doFrame(Choreographer.java:525)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.intern
Please Write this condition instead of fetching Object from index position:
if (dealerCollectionArrayList.size < position) {
final DealerCollection model_two = dealerCollectionArrayList.get(position);
holder.bind_two(model_two);
if (dealerArrayList.get(position).getDealerId() == dealerCollectionArrayList.get(position).getDealerName()) {
holder.dealerCol.setText(dealerCollectionArrayList.get(position).getDealerCollection());
}
}else {
//Set not available or something
}

Related Links

WeChat Integration to my android application for login
Alternative for AppCompat setSupportProgressBarIndeterminateVisibility for AppCompat v23
Adding android support library v4 r13 to my android project
Proguard is not obfuscating AAR classes
:app:compileDebugJavaWithJavac error when running react-native run-android
How to update a view in particular position in recycler view?
Application on screen active time/Get max Priority
Android: Error receiving id onItemLongClickListener in ExpandableListView
Android Monitor Graph logs
“Nephew” views block pinch gesture
AsyncTask in custom EditText
Specify text color for an included layout android
Visual Studio Emulator for Android and NAT
For Android ListView, Is it possible to set a divider and create a padding for each listitem?
Border Outside of Layout
AOSP repo manifest branch name confusing. go for rev## or mr1?

Categories

HOME
turing-machines
case
oracle-apex-5
sublimetext
grafana
xbee
switch-statement
permissions
cq5
big-o
bookmarks
watson-conversation
android-securityexception
siddhi
cmdb
rtf
plink
screen-readers
google-container-registry
apache2.4
sar
simpleitk
strongname
android-things
bringtofront
record
fstream
card.io
html-encode
reverse
parse-tree
xcode7.3
cd
loading
metawidget
signals-slots
mcustomscrollbar
resolution
portforwarding
double-click
cnc
qtranslate
static-cast
oracleclient
android-testing
type-theory
trigger.io
shinobi
structuremap4
custom-lists
myspace
signalr.client
observablecollection
jtwig
nullable
invoke-sqlcmd
user-management
asyncdisplaykit
character-replacement
roadkill-wiki
extjs-grid
doctype
slash
lasso
xmltype
mmwormhole
gamekit
ejb-2.x
android-vibration
sonarqube-5.0
genymotion-call
grails-domain-class
malformedurlexception
synapse
gtm-oauth2
windows-phone-8-sdk
neos-server
workitem
merb
poker
bjyauthorize
resource-management
idn
qtconcurrent
correlated-subquery
jdb
expression-blend-4
user-tracking
jquery-tools
codebase
appledoc
timertask
uideviceorientation
non-clustered-index
leader

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App