CardView和RecycleView的周期性错误

时间:2016-03-03 09:55:46

标签: java android android-recyclerview android-cardview

我有这个奇怪的周期性错误,当我启动应用程序时,onCreate被调用,但CardView没有显示任何内容。其他时候,我做同样的事情,启动应用程序,CardView正好显示它应该是什么。

我似乎无法找到错误..

主要课程:

package lassebjoerklund.easyfridge;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


public class BootActivity extends Activity {
    //TODO Make get method for table with barcode.
    //TODO Update postRequest so that it macthes the barcode to the database, and getting a json object. object -> toString
    private ArrayList<Products> productsToDisplayInlist;
    private String TAG = BootActivity.class.getSimpleName();
    private String getDataurl = "http://XXX.XXX.XXX.XXX/selectAll.php";
    private static String insertDataUrl = "http://XXX.XXX.XXX.XXX/insertAll.php";
    private JsonArrayRequest jsonArrayRequest;
    private RecyclerView.Adapter mAdapter;
    private String jsonRespons = "";
    private static RequestQueue rQueue;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        //Layout
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_boot_layout);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.cardList);
        recyclerView.setHasFixedSize(false);
        recyclerView.setLayoutManager(linearLayoutManager);

        //RequestQueue
        rQueue = Volley.newRequestQueue(this);
        productsToDisplayInlist = new ArrayList<>();
        getRequest();

        Log.d(TAG, productsToDisplayInlist.toString());
        mAdapter = new ProductCardAdapter(productsToDisplayInlist);
        recyclerView.setAdapter(mAdapter);

        //Intent to switch to activity
        //final Intent dispalyProductsIntent = new Intent(BootActivity.this, ProductViewController.class);
        //final Intent scanBarcodeIntent = new Intent(BootActivity.this, BarcodeTracking.class);


        //final ImageButton updateViewButton = (ImageButton) findViewById(R.id.bViewProducts);
        /*updateViewButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                startActivity(dispalyProductsIntent);
            }

        });*/

       /* final ImageButton addNewEntry = (ImageButton) findViewById(R.id.addEntry);
        addNewEntry.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v){
                startActivity(scanBarcodeIntent);
            }
        });*/

    }

    @Override
    protected void onResume() {
        Log.d("onResume", "onResume");
        /*Toast toast = Toast.makeText(this, "onResume", Toast.LENGTH_LONG);
        toast.show();*/
        super.onResume();
    }

    @Override
    protected void onRestart() {
        Log.d("onRestart", "onRestart");
        /*Toast toast = Toast.makeText(this, "onRestart", Toast.LENGTH_SHORT);
        toast.show();*/
        super.onRestart();
    }

    @Override
    protected void onStop() {
        Log.d("onStop", "onStop");
        // productsToDisplayInlist.clear();
        super.onStop();
    }

    @Override
    protected void onPause() {
        Log.d("onPause", "onPause");
        super.onPause();
    }

    //TODO make error msg if there is no connection.
    public void getRequest() {
        productsToDisplayInlist.clear();
        jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, getDataurl, null, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                //Log.d(TAG, response.toString());
                try {
                    for (int i = 0; i < response.length(); i++) {
                        JSONObject productToBuild = (JSONObject) response.get(i);
                        jsonRespons = productToBuild.toString();
                        String barcode = productToBuild.getString("code");
                        String name = productToBuild.getString("name");
                        String type = productToBuild.getString("type");
                        int id = productToBuild.optInt("id");
                        String rmDate = productToBuild.getString("rm_date");
                        String exDate = productToBuild.getString("ex_date");
                        String placementDate = productToBuild.getString("add_date");
                        Products product = new Products(barcode, name, placementDate, exDate, rmDate, type);
                        productsToDisplayInlist.add(product);
                        Log.d("Product", product.toString());
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d("Volly", "Error");
                error.printStackTrace();
            }
        });

        rQueue.add(jsonArrayRequest);
    }

    public static void postRequest(final Products product) {
        try {

            StringRequest stringRequest = new StringRequest(Request.Method.POST, insertDataUrl, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    VolleyLog.d("onResponse", response);

                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d("Volley insertData", "ERROR");
                    Log.d("error: ", error.getCause().toString());
                    error.printStackTrace();
                }
            }) {
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    Map<String, String> perameters = new HashMap<>();
                    perameters.put("barcode", product.getBarcode());
                    perameters.put("addDate", product.getDayOfadd());
                    perameters.put("exDate", product.getExDate());
                    return perameters;
                }
            };

            rQueue.add(stringRequest);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

这里我初始化布局并为RecycleView添加适配器。 productsToDisplayInList方法中填充了getRequest()。但不知何故,它只是定期填充......

适配器类:

package lassebjoerklund.easyfridge;

import android.graphics.Color;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


public class ProductCardAdapter extends RecyclerView.Adapter<ProductHolder> {
    private CardView cardView;
    private List<Products> productses;
    private Calendar c;

    public ProductCardAdapter(ArrayList<Products> items) {
        productses = items;
    }

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

    @Override
    public void onBindViewHolder(ProductHolder holder, int position) {
        Products product = productses.get(position);
        Date returnDate = Util.parseDateString(product.getExDate());
        if (Util.getCurrentDate().after(returnDate)) {
            cardView.setCardBackgroundColor(Color.parseColor("#FF000D"));
            holder.cardName.setText(product.getName());
            holder.cardAddDate.setText(product.getDayOfadd());
            holder.cardExDate.setText(product.getExDate());
            holder.cardType.setText(product.getType());
            Log.d("Product Card", product.toString());
        } else {
            holder.cardName.setText(product.getName());
            holder.cardAddDate.setText(product.getDayOfadd());
            holder.cardExDate.setText(product.getExDate());
            holder.cardType.setText(product.getType());
            Log.d("Product Card", product.toString());
        }
    }

    @Override
    public ProductHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardlayout, parent, false);
        cardView = new CardView(parent.getContext());
        return new ProductHolder(itemView);
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }
}

持有人类:

package lassebjoerklund.easyfridge;

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

public class ProductHolder extends RecyclerView.ViewHolder {

    protected TextView cardName;
    protected TextView cardAddDate;
    protected TextView cardExDate;
    protected TextView cardType;

    public ProductHolder(View itemView) {
        super(itemView);
        cardName = (TextView) itemView.findViewById(R.id.cardName);
        cardAddDate = (TextView) itemView.findViewById(R.id.cardAddDate);
        cardExDate = (TextView) itemView.findViewById(R.id.cardExDate);
        cardType = (TextView) itemView.findViewById(R.id.cardType);
    }
}

0 个答案:

没有答案