【问题标题】:Unable to put elements in the ArrayList using loops无法使用循环将元素放入 ArrayList
【发布时间】:2022-01-14 02:55:55
【问题描述】:

在这里,我尝试使用 for 循环在 ArrayList 中添加项目,但它不起作用。它返回一个空的 arraylist 。我的 ArrayListonCreate 方法之外初始化。第二个是代码,我超出了我的 SQLite 数据库并取出了将其存储在 arraylist 中的所有数据,并使用适配器和模型将其设置在 recyclerview 上,但它正在返回和空数组。不知道为什么?

ArrayList<String> array ;

for (int a=0; a<11; a++)
{
    array = new ArrayList<>();
    String str = "a";
    array.add(str);
}



` public ArrayList<productmodal> getAll(){
        ArrayList<productmodal> array = new ArrayList<>();
        String querySelect  = "SELECT image1,productName,productPrice FROM products";
       SQLiteDatabase db = this.getReadableDatabase();
       Cursor cursorSelect = db.rawQuery(querySelect,null);
       productmodal model ;
           while(cursorSelect.moveToNext()){
               int id = cursorSelect.getInt(0);
               byte[] image= cursorSelect.getBlob(1);
               String name= cursorSelect.getString(4);
               int price = cursorSelect.getInt(5);
             model = new productmodal(image , name , price);
               array.add(model);
           }
       return array;
   }`

 
productModal Class

package com.food.handtohand.modal;

public class productmodal {

    byte[] productImage;
    String productName;
    int productPrice;

    public productmodal(byte[] productImage, String productName, int productPrice) {
        this.productImage = productImage;
        this.productName = productName;
        this.productPrice = productPrice;
    }

    public byte[] getProductImage() {
        return productImage;
    }

    public void setProductImage(byte[] productImage) {
        this.productImage = productImage;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public int getProductPrice() {
        return productPrice;
    }

    public void setProductPrice(int productPrice) {
        this.productPrice = productPrice;
    }
}


productAdapter Class 

package com.food.handtohand.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.food.handtohand.MainActivity;
import com.food.handtohand.ProductDetail;
import com.food.handtohand.R;
import com.food.handtohand.modal.productmodal;

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

public class productAdapter extends RecyclerView.Adapter<productAdapter.viewholder>{
  ArrayList<productmodal> list;
    Context context;

    public productAdapter(ArrayList list , MainActivity context) {
        this.list = list;
        this.context = context;
    }

    @NonNull
    @Override
    public viewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.products , parent , false);
        return new viewholder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull viewholder holder, int position) {
        productmodal products = list.get(position);
        byte[] imgbyte = products.getProductImage();
        Bitmap imagebytes = BitmapFactory.decodeByteArray(imgbyte, 0, imgbyte.length);
        holder.productImage.setImageBitmap(imagebytes);
        holder.price.setText(products.getProductPrice());
        holder.name.setText(products.getProductName());


        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, ProductDetail.class);
               context.startActivity(intent);
            }
        } );
    }

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

    public class viewholder extends RecyclerView.ViewHolder {
        ImageView productImage;
        TextView name , price;
       /* Context contextt;*/
        public viewholder(@NonNull View itemView) {
            super(itemView);
            productImage = itemView.findViewById(R.id.productimg);
            name = itemView.findViewById(R.id.productname);
            price = itemView.findViewById(R.id.productprice);
          /*  contextt = itemView.getContext();*/
        }
    }
}


MainActivity Class

  productRecycler = findViewById(R.id.productrecycler);
    
  ArrayList<productmodal> list2 = AddProductDB.getAll();

 
            productAdapter adapter2 = new productAdapter(list2, this);
            productRecycler.setAdapter(adapter2);

            GridLayoutManager grid = new GridLayoutManager(this, 2);
            productRecycler.setLayoutManager(grid);
      

【问题讨论】:

  • 您正在用循环内的新列表替换列表。只需在外面初始化即可。

标签: java loops for-loop arraylist while-loop


【解决方案1】:

我注意到两个不同的问题和一个优化问题:

  1. 您正在尝试将ArrayList 容器初始化为Integer 并添加String 值。
  2. 由于您每次循环都重新初始化 ArrayList 容器,因此您之前添加的值将被删除。
import java.util.*;  

 public class Test
 {  
     public static void main(String args[])
     {  
          ArrayList<String> array = new ArrayList<String>();    
          String str = "a";
          
          for(int a = 0 ; a < 11 ; ++a)
          {
              array.add(str);
          }
          System.out.println(array);
     }  
}  

【讨论】:

  • 请再看一遍问题我已经编辑了问题....主要问题出在第二种方法
  • @nandiniSrivastava 我认为您应该将问题恢复到我的编辑并提出一个新问题。还可以在新问题中分享productmodal 课程内容。
  • @nandiniSrivastava 因为在最后一次编辑中,您可能没有正确地从数据库中读取数据。您是否确认可以在while 循环中从数据库中检索数据?
  • 我已经提供了所有需要的类代码,请分析并告诉我哪里错了??
  • 我已经直接在数据库中检查了查询,并且在那里工作正常
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-22
  • 2015-10-06
  • 2013-03-28
  • 1970-01-01
  • 1970-01-01
  • 2014-06-11
  • 2018-04-16
相关资源
最近更新 更多