0

我正在制作可扩展的 RecyclerView。问题是我在 ArrayList 中有数据,但未设置适配器。

我尝试在arrayListNotiTypes.add(new NotiTypes("About SMS", addNotiToParticularNotiType(jaSms, "")));之后设置适配器。这一行,但发生同样的错误。我为不同的模块制作了这种类型的可扩展 RecyclerView。它在那里工作正常。

以下是我尝试过的..

Notification_Activity.java

    public class Notification_Activity extends AppCompatActivity {

    // Widgets
    private ProgressDialog progressDialog;
    private RecyclerView recyclerView_Noti;

    // Variables
    private String url = "notification.php";
    private ArrayList<NotiTypes> arrayListNotiTypes;
    private ArrayList<ActualNotis> arrayListActualNotis;

    // Others
    private AdapterNotification adapterNoti;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification);

        find_view_by_id();
        init();

        if (Commom_Methods.isNetworkAvailable(this)) {
            fetchData();
        } else {
            Toast.makeText(this, "Please, Coonect to internet!!", Toast.LENGTH_SHORT).show();
        }

        // Setting Adapter for Notification
        adapterNoti = new AdapterNotification(Notification_Activity.this, arrayListNotiTypes);
        recyclerView_Noti.setAdapter(adapterNoti);
        recyclerView_Noti.setLayoutManager(new LinearLayoutManager(Notification_Activity.this));
    }

    private void find_view_by_id() {
        recyclerView_Noti = (RecyclerView) findViewById(R.id.recycle_noti);
    }

    private void init() {
        arrayListNotiTypes = new ArrayList<>();
    }

    private void fetchData() {
        StringRequest stringReq = new StringRequest(Request.Method.POST, Constants.base_url + url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.e("onResponse: ", response);
                progressDialog.dismiss();
                if (response != null) {
                    try {
                        JSONObject jo = new JSONObject(response);
                        if (jo.has("success")) {

                            JSONObject joNoti = jo.getJSONObject("notification");

                            /*JSONArray jaStu = joNoti.getJSONArray("noti_student");
                            arrayListNotiTypes.add(new NotiTypes("About Student", addNotiToParticularNotiType(jaStu, "")));*/

                            JSONArray jaBatch = joNoti.getJSONArray("noti_batch");
                            arrayListNotiTypes.add(new NotiTypes("About Batch", addNotiToParticularNotiType(jaBatch, "")));

                            JSONArray jaInst = joNoti.getJSONArray("noti_institute");
                            arrayListNotiTypes.add(new NotiTypes("About Institute", addNotiToParticularNotiType(jaInst, "attach")));

                            JSONArray jaFee = joNoti.getJSONArray("noti_fee");
                            arrayListNotiTypes.add(new NotiTypes("About Fees", addNotiToParticularNotiType(jaFee, "attach")));

                            JSONArray jaSms = joNoti.getJSONArray("noti_sms");
                            arrayListNotiTypes.add(new NotiTypes("About SMS", addNotiToParticularNotiType(jaSms, "")));

                        } else {
                            Toast.makeText(getApplicationContext(), jo.getString("error_msg"), Toast.LENGTH_LONG).show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    Toast.makeText(getApplicationContext(), "Server error! Don't get Data from server. Try again later.", Toast.LENGTH_LONG).show();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                progressDialog.dismiss();
                Toast.makeText(getApplicationContext(), "Error:" + error.getMessage(), Toast.LENGTH_LONG).show();
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("id", Preferences.readString(getApplicationContext(), Preferences.KEY_SL_ID, ""));
                params.put("tution_center_sl", Preferences.readString(getApplicationContext(), Preferences.KEY_TUTION_CENTER_SL, ""));
                params.put("batch_sl", Preferences.readString(getApplicationContext(), Preferences.KEY_BATCH_SL, ""));
                params.put("batch_grup_sl", Preferences.readString(getApplicationContext(), Preferences.KEY_BATCH_GRUP_SL, ""));
                params.put("co_sl", Preferences.readString(getApplicationContext(), Preferences.KEY_CO_SL, ""));
                params.put("drange_sl", Preferences.readString(getApplicationContext(), Preferences.KEY_DRANGE_SL, ""));
                return params;
            }

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<String, String>();
                headers.put("X-Apikey", Preferences.readString(getApplicationContext(), Preferences.KEY_USER_XAPIKEY, ""));
                return headers;
            }
        };
        RequestQueue queue = Volley.newRequestQueue(this);
        queue.add(stringReq);

        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Loading...");
        progressDialog.show();
    }

    private ArrayList<ActualNotis> addNotiToParticularNotiType(JSONArray jsonArray, String attachments) throws JSONException {
        arrayListActualNotis = new ArrayList<>();
        if (jsonArray.length() > 0) {
            for (int j = 0; j < jsonArray.length(); j++) {
                JSONObject joInner = jsonArray.getJSONObject(j);
                String notiTitle = joInner.getString("title");
                String notiDesc = joInner.getString("decription");
                String attach = "";
                if (!attachments.equals(""))
                    attach = joInner.getString("attach");
                arrayListActualNotis.add(new ActualNotis(notiTitle, notiDesc, attach));
            }
        } else {
            arrayListActualNotis.add(new ActualNotis("No Notifications!!", "", ""));
        }
        return arrayListActualNotis;
    }
}

AdapterNotification.java

    public class AdapterNotification extends ExpandableRecyclerViewAdapter<AdapterNotification.NotiTypesViewHolder, AdapterNotification.ActualNotisViewHolder> {

    private Activity mActivity;

    public AdapterNotification(Activity activity, List<? extends ExpandableGroup> groups) {
        super(groups);
        this.mActivity = activity;
    }

    @Override
    public NotiTypesViewHolder onCreateGroupViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = (LayoutInflater) mActivity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.noti_type_group_view_holder, parent, false);
        return new NotiTypesViewHolder(view);
    }

    @Override
    public ActualNotisViewHolder onCreateChildViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = (LayoutInflater) mActivity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.actual_notis_child_view_holder, parent, false);
        return new ActualNotisViewHolder(view);
    }

    @Override
    public void onBindGroupViewHolder(NotiTypesViewHolder holder, int flatPosition, ExpandableGroup group) {
        holder.setGroupName(group);
    }

    @Override
    public void onBindChildViewHolder(ActualNotisViewHolder holder, int flatPosition, ExpandableGroup group, int childIndex) {
        final ActualNotis notis = ((NotiTypes) group).getItems().get(childIndex);
        holder.onBind(notis, group);
    }

    public class NotiTypesViewHolder extends GroupViewHolder {

        private TextView txt_noti_type;

        public NotiTypesViewHolder(View itemView) {
            super(itemView);
            txt_noti_type = (TextView) itemView.findViewById(R.id.txt_noti_type);
        }

        @Override
        public void expand() {
            txt_noti_type.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.up_arrow, 0);
            Log.e("Adapter", "Expand");
        }

        @Override
        public void collapse() {
            txt_noti_type.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.down_arrow, 0);
            Log.e("Adapter", "collapse");
        }

        public void setGroupName(ExpandableGroup group) {
            txt_noti_type.setText(group.getTitle());
        }
    }

    public class ActualNotisViewHolder extends ChildViewHolder {

        private TextView txt_noti, txt_noti_desc;

        public ActualNotisViewHolder(View itemView) {
            super(itemView);
            txt_noti = (TextView) itemView.findViewById(R.id.txt_noti);
            txt_noti_desc = (TextView) itemView.findViewById(R.id.txt_noti_desc);
        }

        public void onBind(ActualNotis actualNotis, ExpandableGroup group) {
            switch (actualNotis.getmNotiTitle()) {
                case "noti_student":
                    txt_noti.setText(actualNotis.getmNotiTitle());
                    txt_noti_desc.setText(actualNotis.getmNotiDesc());
                    break;
                case "noti_batch":
                    txt_noti.setText(actualNotis.getmNotiTitle());
                    txt_noti_desc.setText(actualNotis.getmNotiDesc());
                    break;
                case "noti_institute":
                    txt_noti.setText(actualNotis.getmNotiTitle());
                    txt_noti_desc.setText(actualNotis.getmNotiDesc());
                    break;
                case "noti_fee":
                    txt_noti.setText(actualNotis.getmNotiTitle());
                    txt_noti_desc.setText(actualNotis.getmNotiDesc());
                    break;
                case "noti_sms":
                    txt_noti.setText(actualNotis.getmNotiTitle());
                    txt_noti_desc.setText(actualNotis.getmNotiDesc());
                    break;
                default:
                    break;
            }
        }
    }
}

来自服务器的 JSON 响应

    {
  "notification": {
    "noti_batch": [
      {
        "title": "testtest",
        "decription": "testtest"
      }
    ],
    "noti_institute": [
      {
        "title": "test",
        "decription": "testtest",
        "attach": ""
      }
    ],
    "noti_fee": [
      {
        "title": "test",
        "decription": "test",
        "attach": ""
      }
    ],
    "noti_sms": [
      {
        "title": "2016-11-03 00:00:00",
        "decription": ""
      }
    ]
  },
  "success": true
}

提前致谢!

4

1 回答 1

2

setAdapter 从服务器获取数据后,即在 onResponse() 内,或者您必须在更改列表中的数据后通知适配器

于 2016-11-05T12:37:53.430 回答