繁体   English   中英

RecyclerView 未正确显示 ArrayList

[英]RecyclerView not displaying ArrayList correctly

出于某种原因,我的 RecyclerView 中显示的唯一内容是 com.stu54259.plan2cook.Model.Shopping_list@5cb7482 重复了各种结束代码而不是 ArrayList 的内容。 任何建议都必须与 recylerview 适配器有关。 如果需要,可以添加 xml 等,但我确定我刚刚错过了一些愚蠢的事情。

Shopping_List 类

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;
    }

}

活动

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();
}

}

适配器

    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);
    }
}

在您的Shopping_List类中添加此方法,因此当您对Shopping_List实例使用toString() ,您将获得其所有属性以空格分隔:

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

您可以更改属性的顺序。

您在 onBindViewHolder 方法中有错误的代码。 您应该使用 Shopping_List 对象中的某些字段设置文本:

@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());
        }
    }

你还没有把Shopping_List对象放在这里,但如果你有这样的东西:

public class Shopping_List {

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

}

那么你应该做这样的事情:

@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());
        }
    }

虽然它没有给出直接的解决方案,但我建议您使用 groupie 库。 它很可能会消除您的错误并减少样板代码和复杂性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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