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

Button android while pressed
Why was this change made to SocketInputBuffer for Ice Cream Sandwich?
Comprehensive list of Splash Screen and Icons for Phonegap
Image classification with opencv
How to get libgdx view from andengine project
Horizontal scrolling between activities in Android
Read QR Code without using other app
Gps,GeoCoding causes app hang in android
Android App Closes on creation?
GridLayout spitting out “inconsistent constraint” debug-level logs
Android DB: Merging Two Databases Using SQL Dumps
Questio about AdMob ad url to application
trouble importing android-remote-stacktrace
ADT rev20 - apps don't install on any AVD
View will get twice at onItemClick
downloading xml from ftp in android

Categories

HOME
algorithm
microsoftgraph
oracle12c
pyqt
jmx
toolbox
interface
google-apps
dojo
pearson-correlation
pkcs#11
extjs4.2
augeas
rtf
symbol
scsi
google-picker
foreign-keys
delphi-xe5
zxing
roundup
xcode-ui-testing
itunes
angularjs-components
wcag
twiml
getpixel
installscript
html-encode
processwire
docfx
playframework-2.1
bc
recurrent-neural-network
subclipse
facebook-social-plugins
serde
fclose
flyout
cpu-cache
baidu
javafx-css
uicolor
scaffolding
cifilter
nservicebus5
project-organization
jparsec
qbxml
jaunt-api
highland.js
include-guards
iced-coffeescript
vacuum
telepat
snackbar
infosphere-spl
jbake
primitive-types
rmysql
ora-00904
android-framework
http-patch
c++builder-xe5
scringo
cmmi
workitem
flex++
css-reset
cardreader
bjyauthorize
device-width
nokia-s40
subgraph
ilasm
jquery-address
screen-size
xcelsius
cdonts
cac
sifr
qtabbar
skype4java
web-application-project
tui
google-wave
software-lifecycle

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