简体   繁体   中英

RecyclerView not displaying ArrayList correctly

For some reason the only thing displayed in my RecyclerView is com.stu54259.plan2cook.Model.Shopping_list@5cb7482 repeated with various end codes not the contents of the ArrayList. Any suggestions must be something with the recylerview adapter. Can add xml etc if need be but i'm sure I've just missed something stupid.

Shopping_List class

package com.stu54259.plan2cook.Model;

public class Shopping_List {

    private int id;
    private String ingredient_type;
    private String ingredient_name;
    private Double quantity;
    private String measurement_name;


    public Shopping_List() {

    }

    public Shopping_List(String ingredient_type, String ingredient_name, Double quantity, String measurement_name) {

        this.ingredient_type = ingredient_type;
        this.ingredient_name = ingredient_name;
        this.quantity = quantity;
        this.measurement_name = measurement_name;

    }
    public Shopping_List(int id, String ingredient_type, String ingredient_name, Double quantity, String measurement_name) {

        this.id = id;
        this.ingredient_type = ingredient_type;
        this.ingredient_name = ingredient_name;
        this.quantity = quantity;
        this.measurement_name = measurement_name;

    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getIngredient_type() {
        return ingredient_type;
    }

    public void setIngredient_type(String ingredient_type) {
        this.ingredient_type = ingredient_type;
    }

    public String getIngredient_name() {
        return ingredient_name;
    }

    public void setIngredient_name(String ingredient_name) {
        this.ingredient_name = ingredient_name;
    }

    public Double getQuantity() {
        return quantity;
    }

    public void setQuantity(Double quantity) {
        this.quantity = quantity;
    }

    public String getMeasurement_name() {
        return measurement_name;
    }

    public void setMeasurement_name(String measurement_name) {
        this.measurement_name = measurement_name;
    }

}

Activity

public class ShoppingList extends MainActivity {
ShoppingListAdapter adapterRecipe;
List<Shopping_List> shopList = new ArrayList<>();
RecyclerView listIngredient;
SQLiteDatabase db;
Cursor c;
EditText edittext;
String search;


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.shopping_list);
    edittext = findViewById(R.id.editPlanName);
    edittext.setOnKeyListener(new View.OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            search = edittext.getText().toString();
            Log.d("Search value", search);
            if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
                    (keyCode == KeyEvent.KEYCODE_ENTER)) {
                loadIngredient();
                adapterRecipe.notifyDataSetChanged();
                return true;
            }
            return false;
        }
    });
    BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.home:
                    Intent a = new Intent(ShoppingList.this,MainActivity.class);
                    startActivity(a);
                    break;
                case R.id.recipes:
                    Intent b = new Intent(ShoppingList.this,RecipeSearch.class);
                    startActivity(b);
                    break;
                case R.id.shoppingList:
                    Intent c = new Intent(ShoppingList.this, ShoppingList.class);
                    startActivity(c);
                    break;
                case R.id.mealPlan:
                    Intent d = new Intent(ShoppingList.this, MenuPlan.class);
                    startActivity(d);
                    break;
                case R.id.reminder:
                    Intent e = new Intent(ShoppingList.this, Reminders.class);
                    startActivity(e);
                    break;
            }
            return false;
        }
    });
    adapterRecipe = new ShoppingListAdapter(this, shopList);
    listIngredient = findViewById(R.id.listIngredient);
    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this,
            LinearLayoutManager.VERTICAL, false);
    listIngredient.setLayoutManager(mLayoutManager);
    listIngredient.setItemAnimator(new DefaultItemAnimator());
    listIngredient.setAdapter(adapterRecipe);
}
public void loadIngredient() {
    shopList.clear();
    db = (new DatabaseManager(this).getWritableDatabase());
    String RECIPE_SEARCH =
            "SELECT SUM(A.ingredient_quantity) quantity, A.ingredient ingredient_name, A.recipe, B.ingredient_type, B.measurement_name, C.id, D.plan_name " +
                    "FROM " + DatabaseManager.TABLE_QUANTITY + " AS A JOIN " + DatabaseManager.TABLE_INGREDIENTS + " AS B ON A.ingredient = B.ingredient_name " +
                    "JOIN " + DatabaseManager.TABLE_PLAN_RECIPES + " AS C ON A.recipe = C.recipe_name " +
                    "JOIN " + DatabaseManager.TABLE_MEAL_PLAN + " AS D ON C.id = D.plan_recipe " +
                    "WHERE D.plan_name LIKE ? GROUP BY A.ingredient";
    Log.d("Search query", RECIPE_SEARCH);
    c = db.rawQuery(RECIPE_SEARCH, new String[]{"%" + search + "%"});

    if (c.moveToFirst()) {
        do {
            Shopping_List shopping_list = new Shopping_List();
            shopping_list.setQuantity(c.getDouble(c.getColumnIndex("quantity")));
            shopping_list.setIngredient_name(c.getString(c.getColumnIndex("ingredient_name")));
            shopping_list.setIngredient_type(c.getString(c.getColumnIndex("ingredient_type")));
            shopping_list.setMeasurement_name(c.getString(c.getColumnIndex("measurement_name")));
            shopList.add(shopping_list);
        } while (c.moveToNext());
    }
    c.close();
    db.close();
}

}

Adapter

    public class ShoppingListAdapter extends RecyclerView.Adapter<com.stu54259.plan2cook.Adapters.ShoppingListAdapter.ViewHolder> {

    private List<Shopping_List> shopList;
    private LayoutInflater mInflater;
    private com.stu54259.plan2cook.Adapters.RecyclerViewAdapter.ItemClickListener mClickListener;

    // data is passed into the constructor
    public ShoppingListAdapter(Context context, List<Shopping_List> data) {
        this.mInflater = LayoutInflater.from(context);
        this.shopList = data;
    }

    // inflates the row layout from xml when needed
    @Override
    public com.stu54259.plan2cook.Adapters.ShoppingListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.fragment_item, parent, false);
        return new com.stu54259.plan2cook.Adapters.ShoppingListAdapter.ViewHolder(view);
    }

    // binds the data to the TextView in each row
    @Override
    public void onBindViewHolder(com.stu54259.plan2cook.Adapters.ShoppingListAdapter.ViewHolder holder, int position) {
        if(shopList.get(position) != null)
        {
            holder.myTextView.setText(shopList.get(position).toString());
        }
    }

    // total number of rows
    @Override
    public int getItemCount() {
        return shopList.size();
    }


    // stores and recycles views as they are scrolled off screen
    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView myTextView;

        ViewHolder(View itemView) {
            super(itemView);
            myTextView = itemView.findViewById(R.id.quantity);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
        }
    }


    // allows clicks events to be caught
    void setClickListener(com.stu54259.plan2cook.Adapters.RecyclerViewAdapter.ItemClickListener itemClickListener) {
        this.mClickListener = itemClickListener;
    }

    // parent activity will implement this method to respond to click events
    public interface ItemClickListener {
        void onItemClick(View view, int position);
    }
}

Add this method in your Shopping_List class, so when you use toString() for a Shopping_List instance you will get all its properties separated by spaces:

public String toString() { 
    return ingredient_name + " " + ingredient_type + " " + quantity + " " + measurement_name; 
}

You can change the order of the properties.

You have wrong code in onBindViewHolder method. You should set text with some field from Shopping_List object:

@Override
    public void onBindViewHolder(com.stu54259.plan2cook.Adapters.ShoppingListAdapter.ViewHolder holder, int position) {
        if(shopList.get(position) != null)
        {
            holder.myTextView.setText(shopList.get(position).toString());
        }
    }

You haven't put the Shopping_List object here, but if you have something like this:

public class Shopping_List {

    public String title;
    
    public String getTitle() {
        return title;
    }

}

Then you should do something like this:

@Override
    public void onBindViewHolder(com.stu54259.plan2cook.Adapters.ShoppingListAdapter.ViewHolder holder, int position) {
        if(shopList.get(position) != null)
        {
            holder.myTextView.setText(shopList.get(position).getTitle());
        }
    }

Although it doesn't give a direct solution, I would suggest that you use the groupie library. It will most likely remove your error and reduce boilerplate code and complexity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM