从控制器类填充回收适配器

时间:2022-10-22 19:41:15

I'm trying to populate a recycle adapter from a controller class, to split the logic in the app. But I can't seem to get it working.. Do to the request is running on another thread, the view is not updated before it is shown. What am I doing wrong ?

我正在尝试从控制器类填充回收适配器,以拆分应用程序中的逻辑。但我似乎无法让它工作..请求在另一个线程上运行,视图在显示之前不会更新。我究竟做错了什么 ?

FragmentClass:

FragmentClass:

package lassebjoerklund.homecontrol.views.fragments;

import android.app.Fragment;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.Response;
import com.android.volley.VolleyError;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import lassebjoerklund.homecontrol.R;
import lassebjoerklund.homecontrol.adapters.FridgeFragmentOverViewAdapter;
import lassebjoerklund.homecontrol.controllers.FridgeController;
import lassebjoerklund.homecontrol.decorations.DividerItemDecoration;
import lassebjoerklund.homecontrol.model.Product;
import lassebjoerklund.homecontrol.utils.FragmentUtil;


/**
 * Created by zigge on 29-Mar-16.
 */
public class FridgeFragmentOverView extends Fragment {
    private  final FridgeController fridgeController = new FridgeController();
    private RecyclerView recyclerView;
    private LinkedList<String> types;
    private FridgeFragmentOverViewAdapter adapter;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_overview_layout, container, false);

        recyclerView = (RecyclerView) v.findViewById(R.id.productOverviewRecycleView);
        recyclerView.setHasFixedSize(true);
        RecyclerView.ItemDecoration itemDecoration = new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST);
        recyclerView.addItemDecoration(itemDecoration);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(layoutManager);
        adapter = new FridgeFragmentOverViewAdapter(fridgeController.getProductsTypes());
        recyclerView.setAdapter(adapter);
        return v;
    }

    @Override
    public void onResume() {
        super.onResume();
        fridgeController.refresh(getContext(), adapter);
    }


}

Controller Class:

控制器类:

package lassebjoerklund.homecontrol.controllers;

import android.content.Context;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import lassebjoerklund.homecontrol.adapters.FridgeFragmentOverViewAdapter;
import lassebjoerklund.homecontrol.interfaces.OnSucsess;
import lassebjoerklund.homecontrol.model.Product;

/**
 * Created by zigge on 29-Mar-16.
 */
public class FridgeController implements OnSucsess {
    private RequestQueue rQueue;
    private ArrayList<Product> products;
    private List<String> productsTypes;


    public ArrayList<Product> getProducts() {
        return products;
    }

    public List<String> getProductsTypes() {
        return productsTypes;
    }


    public void refresh(Context context, final FridgeFragmentOverViewAdapter adapter) {
        String fetchProducts = "http://87.52.28.25:900/index.php";
        rQueue = Volley.newRequestQueue(context);
        final JsonArrayRequest getProductsRequest = new JsonArrayRequest(fetchProducts, new Response.Listener<JSONArray>() {

            @Override
            public void onResponse(JSONArray response) {
                products = new ArrayList<>();
                productsTypes = new ArrayList<>();
                try {
                    for (int i = 0; i < response.length(); i++) {
                        JSONObject jsonObject = (JSONObject) response.get(i);
                        String barcode = jsonObject.getString("code");
                        String name = jsonObject.getString("name");
                        String ex_date = jsonObject.getString("ex_date");
                        String add_date = jsonObject.getString("add_date");
                        String type = jsonObject.getString("type");
                        Product product = new Product(barcode, name, ex_date, add_date, type);
                        products.add(product);
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
                if (products.size() != 0) {
                    for (int i = 0; i < products.size(); i++) {
                        if (!productsTypes.contains(products.get(i).getType())) {
                            productsTypes.add(products.get(i).getType());
                        }
                    }
                }
                getProductsOnsucsess(adapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }

        });
        rQueue.add(getProductsRequest);
    }

    @Override
    public void getProductsOnsucsess(FridgeFragmentOverViewAdapter adapter) {
        adapter.refreshAdapter(productsTypes);
    }
}

Adapter:

适配器:

package lassebjoerklund.homecontrol.adapters;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import lassebjoerklund.homecontrol.R;
import lassebjoerklund.homecontrol.model.Product;

/**
 * Created by zigge on 29-Mar-16.
 */
public class FridgeFragmentOverViewAdapter extends RecyclerView.Adapter {

    private List<String> types;

    public FridgeFragmentOverViewAdapter(List<String> types) {
        this.types = types;
    }

    @Override
    public int getItemCount() {
        return 0;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_overview_card_layout, null);
        return new ProductsGroupedHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

    }

    public class ProductsGroupedHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private TextView tvTypeOfProduct, tvCount, tvExpired;

        public ProductsGroupedHolder(View itemView) {
            super(itemView);
            tvTypeOfProduct = (TextView) itemView.findViewById(R.id.tvTypeOfProduct);
            tvCount = (TextView) itemView.findViewById(R.id.tvCount);
            tvExpired = (TextView) itemView.findViewById(R.id.tvExpired);

        }

        @Override
        public void onClick(View v) {

        }
    }

    public void refreshAdapter(List<String> types) {
        if(this.types != null) {
            this.types.clear();
            this.types = types;
            notifyDataSetChanged();
        }else {
            this.types = types;
            notifyDataSetChanged();
        }
    }
}

2 个解决方案

#1


1  

Simple looks like you have set the getItemType() method return 0. That method determines how much item your RecyclerView have.

简单看起来你已经设置了getItemType()方法返回0.该方法确定您的RecyclerView具有多少项。

@Override
public int getItemCount() {
    return type.size();
}

#2


2  

Try calling:

试着打电话:

adapter.notifyDataSetChanged();

after updating it.

更新后。

Edit 1:

编辑1:

Return actual size of adapter items in getItemCount() as @prathamkesarkar said.

返回getItemCount()中适配器项的实际大小,如@prathamkesarkar所述。

#1


1  

Simple looks like you have set the getItemType() method return 0. That method determines how much item your RecyclerView have.

简单看起来你已经设置了getItemType()方法返回0.该方法确定您的RecyclerView具有多少项。

@Override
public int getItemCount() {
    return type.size();
}

#2


2  

Try calling:

试着打电话:

adapter.notifyDataSetChanged();

after updating it.

更新后。

Edit 1:

编辑1:

Return actual size of adapter items in getItemCount() as @prathamkesarkar said.

返回getItemCount()中适配器项的实际大小,如@prathamkesarkar所述。