Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <...>
- public List<ChatsEntity> listContents = new ArrayList<>();
- private ViewHolder mViewHolder = new ViewHolder();
- private ChatsEntity mChatsEntity;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- listContents = new ArrayList<>();
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- final View view = inflater.inflate(R.layout.fragment_list, container, false);
- this.mContext = view.getContext();
- this.mViewHolder.mRecyclerViewContacts = view.findViewById(R.id.recycler_contacts);
- this.mViewHolder.mTextHead = view.findViewById(R.id.head_list);
- this.mViewHolder.mTextOffline = view.findViewById(R.id.txt_offline);
- this.mViewHolder.mTextOffline.setVisibility(View.GONE);
- RetrieveDataAPI retrieveDataAPI = new RetrieveDataAPI(mContext,"");
- retrieveDataAPI.retornaValoresRetornados();
- return view;
- }
- <...>
- //Method that get JSON from RetrieveDataAPI
- public void recebeDados(String recebido) throws JSONException {
- recebidoAPI = recebido;
- JSONObject jsonObject = new JSONObject(recebido);
- for (int i = 0; i < jsonObject.length(); i++) {
- String id = jsonObject.getString("id");
- String name = jsonObject.getString("name");
- String description = jsonObject.getString("description");
- Log.d("ListFragment", "id: " + id);
- Log.d("ListFragment", "name: " + name);
- Log.d("ListFragment", "description: " + description);
- retornaValores(id, name, description);
- }
- }
- private void retornaValores(String id_valor, String name_valor, String desc_valor) {
- ChatsEntity mChatsEntity;
- mChatsEntity = new ChatsEntity(id_valor, name_valor, desc_valor);
- mChatsEntity.setName(name_valor);
- mChatsEntity.setDescription(desc_valor);
- if(id_valor == null){
- Log.d("ERRONULO1","null id");
- }else{
- listContents.add(mChatsEntity);
- listAux = listContents;
- }
- retornaList();
- }
- private void retornaList() {
- if (listContents.size() == 0) {
- Log.e("listContents.size()","" + listContents.size());
- } else {
- this.mGuestListAdapter = new ContactsAdapter(listContents, this.mChatsListener);
- this.mViewHolder.mRecyclerViewContacts.setLayoutManager(new LinearLayoutManager(getContext()));
- this.mViewHolder.mRecyclerViewContacts.setHasFixedSize(true);
- this.mViewHolder.mRecyclerViewContacts.setAdapter(this.mGuestListAdapter);
- this.mGuestListAdapter.notifyDataSetChanged();
- }
- }
- private static class ViewHolder {
- RecyclerView mRecyclerViewContacts;
- TextView mTextHead;
- TextView mTextOffline;
- }
- public class RetrieveDataAPI {
- private ViewHolder mViewHolder = new ViewHolder();
- private Context mContext;
- public List<ChatsEntity> list_API;
- public RetrieveDataAPI(Context context, String table_name) {
- this.mViewHolder.table_API = table_name;
- this.mContext = context;
- }
- private void loadChats() {
- final ProgressDialog progressDialog = new ProgressDialog(mContext);
- progressDialog.setTitle("Loading list");
- progressDialog.setMessage("Loading...");
- progressDialog.show();
- Runnable progressRunnable = new Runnable() {
- @Override
- public void run() {
- progressDialog.cancel();
- }
- };
- Handler pdCanceller = new Handler();
- pdCanceller.postDelayed(progressRunnable, 5000);
- StringRequest stringRequest = new StringRequest(com.android.volley.Request.Method.GET,
- ChatConstants.URL_PASTEL, new Response.Listener<String>() {
- @Override
- public void onResponse(String response) {
- progressDialog.dismiss();
- try {
- JSONArray jsonArray = new JSONArray(response);
- for (int i = 0; i < jsonArray.length(); i++) {
- JSONObject jo = jsonArray.getJSONObject(i);
- mViewHolder.valores = jo.toString();
- ListFragment listFragment = new ListFragment();
- listFragment.recebeDados(mViewHolder.valores);
- carregaValoresRetornados(mViewHolder.valores);
- }
- } catch (JSONException e) {
- Toast.makeText(mContext, e.getMessage() + "Problema ao carregar JSON!", Toast.LENGTH_LONG).show();
- e.printStackTrace();
- }
- }
- }, new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- Toast.makeText(mContext, "Error to load data", Toast.LENGTH_LONG).show();
- }
- });
- RequestQueue requestQueue = Volley.newRequestQueue(mContext);
- requestQueue.add(stringRequest);
- }
- public class ViewHolder{
- private String table_API;
- private String valores;
- }
- public void carregaValoresRetornados(String valoresAPI) {
- mViewHolder.valores = valoresAPI;
- }
- public String retornaValoresRetornados() {
- loadChats();
- return mViewHolder.valores;
- }
Add Comment
Please, Sign In to add comment