Skip to content Skip to sidebar Skip to footer

Recyclerview Addinarray Error When Adding

When I add in List any item in loadOrdersData(), it throws this: D/ViewGroup: addInArray been called, this = android.support.v7.widget.RecyclerView {41cab8f0 VFED

Solution 1:

Use

adapter = new RecycleAdapter(listItems, CourierActivity.this);

instead of

adapter = new RecycleAdapter(listItems, getApplicationContext());

Also add orientation in your RecyclerView

android:orientation="vertical"

Update this:

String rsp = newString(response.getBytes(), "UTF-8");

Solution 2:

Try this way

CourierActivity.java

publicclassCourierActivityextendsAppCompatActivity {
    ...
   //Don't forget to intialize your ListprivateList<OrderListItem> listItems = newArrayList<OrderListItem>();
    ...

    privatevoidloadOrdersData() {    
        RequestQueue queue = Volley.newRequestQueue(this);
        StringRequest sr = newStringRequest(Request.Method.GET, URL, newResponse.Listener<String>() {
            @OverridepublicvoidonResponse(String response) {
                listItems = newArrayList<>();

                try {
                    String rsp = newString(response.getBytes("ISO-8859-1"), "UTF-8");
                    JSONArray orders = newJSONArray(rsp);

                    for (int i = 0; i < orders.length(); i++) {
                        OrderListItem listItem = newOrderListItem(
                                // .. 
                        );

                        listItems.add(listItem);
                    }
                    //set list to adapter this way
                    adapter.setList(listItems);
                } catch (JSONException | UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
        }, newResponse.ErrorListener() {
            @OverridepublicvoidonErrorResponse(VolleyError error) {
                Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT);
            }
        }) {

            @OverridepublicMap<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> params = newHashMap<String, String>();
                return params;
            }
        };

        queue.add(sr);
    }
    }

RecycleAdapter.java

publicclassRecycleAdapterextendsRecyclerView.Adapter<RecycleAdapter.ViewHolder> {

    private List<OrderListItem> listItems;
    private Context context;

    publicRecycleAdapter(List<OrderListItem> listItems, Context context) {
        this.listItems = listItems;
        this.context = context;
    }

    publicsetList(List<OrderListItem> listItems){
        //add list to parent list and notify your adapterthis.listItems.addAll(listItems);
        notifyDataSetChanged();
    }
    ...
}

Edit 1 :

main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"xmlns:app="http://schemas.android.com/apk/res-auto"tools:context="com.example.seether.myapplication.CourierActivity"android:background="#fff"android:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/drawerLayout"><android.support.v7.widget.RecyclerViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/rv"/
</LinearLayout>

Solution 3:

I have made couple of changes and written in comment. Try that out,

publicclassCourierActivityextendsAppCompatActivity {
    privateTableLayout orderTable;
    privateRecyclerView recyclerView;
    privateRecycleAdapter adapter;

    //********* Initialize over here **********//privateList<OrderListItem> listItems = newArrayList<>();
    privateContext mContext;

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.courier_main);

        //********** Initialize *******//
        mContext = CourierActivity.this;

        recyclerView = (RecyclerView) findViewById(R.id.rv);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(newLinearLayoutManager(CourierActivity.this));

        //********* added class context *********//
        adapter = newRecycleAdapter(listItems, mContext);
        recyclerView.setAdapter(adapter);
        loadOrdersData();
    }

    privatevoidloadOrdersData() {
        RequestQueue queue = Volley.newRequestQueue(this);
        StringRequest sr = newStringRequest(Request.Method.GET, URL, newResponse.Listener<String>() {
            @OverridepublicvoidonResponse(String response) {
                //******** Remove this line *************//
                listItems = newArrayList<>();

                //******** Add this line ************//
                listItems.clear();

                try {
                    String rsp = newString(response.getBytes("ISO-8859-1"), "UTF-8");
                    JSONArray orders = newJSONArray(rsp);

                    for (int i = 0; i < orders.length(); i++) {
                        OrderListItem listItem = newOrderListItem(
                                // ..
                        );

                        listItems.add(listItem);
                    }



                    //*********Remove these both lines **********//
                    adapter = newRecycleAdapter(listItems, getApplicationContext());
                    recyclerView.setAdapter(adapter);

                    //************ Add this line **********//
                    adapter.notifyDataSetChanged();

                } catch (JSONException | UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
        }, newResponse.ErrorListener() {
            @OverridepublicvoidonErrorResponse(VolleyError error) {
                Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT);
            }
        }) {

            @OverridepublicMap<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> params = newHashMap<String, String>();
                return params;
            }
        };

        queue.add(sr);
    }
}

Solution 4:

Volley seems to get the data asynchronously, so the most reasonable way to manage the RecyclerView is setting the adapter at first with an empty (but not null) List and the Activity context (e.g. Mainactivity.this).

When Volley has finally got all the elements, you add all of them (instead of for(int i = 0; i < orders.length(); i++) you could use for(OrderListItem listItem : orders);), you can call adapter.notifyDataSetChanged()

Solution 5:

Instead of create new ArrayList and RecyclerAdapter instance in onResponse(), I prefer,

  1. Clear listItems.
  2. Add all OrderListItem instance to listItems.
  3. Call notifyDataSetChanged() of adapter.

For example:

@OverridepublicvoidonResponse(String response) {
     listItems.clear();

     try {
         //loop and add all items to listItems here
         adapter.notifyDataSetChanged();
     } catch (JSONException | UnsupportedEncodingException e) {
         e.printStackTrace();
     }
}

and make sure that you initialize RecyclerAdapter properly because I do not see any code that create listItems in onCreate() method of CourierActivity.

listItems = new ArrayList<OrderListItem>();adapter = new RecycleAdapter(listItems, getApplicationContext());        

Post a Comment for "Recyclerview Addinarray Error When Adding"