无法从两个类导入Arraylist ......现在怎么办?

时间:2017-08-29 11:50:04

标签: java class import

我是Java的新手,发现有点晚了我无法从多个类中导入ArrayList

仅使用一个类(BlackLabel)构建它,CustomAdapter正常工作。现在我想添加第二个品牌(Castle),但我发现我只能导入一个静态品牌。也许你们可以这么善良,并帮助我以另一种方式添加多个品牌来使用相同的适配器。

这是我的适配器:(只是开头)

import static com.example.android.sab.BlackLabel.modelArrayList;
//import static com.example.android.sab.CastleLager.modelArrayList;

/**
* Created by Carel on 2017/08/15.
*/
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {

   private LayoutInflater inflater;
   private Context ctx;

   public CustomAdapter(Context ctx) {
      inflater = LayoutInflater.from(ctx);
      this.ctx = ctx;
   }

   @Override
   public CustomAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
      View view = inflater.inflate(R.layout.rv_item, parent, false);
      MyViewHolder holder = new MyViewHolder(view);

      return holder;
   }

   @Override
   public void onBindViewHolder(final CustomAdapter.MyViewHolder holder, int position) {
      holder.tvBrand.setText((CharSequence) modelArrayList.get(position).getBrand());
      holder.tvCases.setText(String.valueOf(modelArrayList.get(position).getNumber()));
      holder.tvPallet_size.setText(String.valueOf(modelArrayList.get(position).getPallet_size()));
      holder.tvCases.setText(String.valueOf(modelArrayList.get(position).getNumber2()));
      holder.tvPallets.setText(String.valueOf(modelArrayList.get(position).getNumber()));
      holder.tvCases.setText(String.valueOf(modelArrayList.get(position).getNumber2()));
   }

这是BlackLabel cass,这是第一个:

import java.util.ArrayList;
//import static com.example.android.sab.CastleLager.modelArrayList;

public class BlackLabel extends brands {
   private RecyclerView recyclerView;
   //private String modelArrayList;
   //private Context ctx;
   public static ArrayList<Model> modelArrayList;
   private CustomAdapter customAdapter;
   //private Button btnnext;

   public String[] brandlist = new String[] {
      "Black Label 340ml NRB (85023)",
      "Black Label 330ml Cans (85736)",
      "Black label 500ml Cans (85023)",
      "Black Label 440ml NRB (86798)",
      "Black Label 330ml RB (85556)",
      "Black Label 750ml RB (85021)",
      "Black Label 340ml NRB 12 Pack (87009)",
      "Black Label 500ml Cans 12 Pack (85022)"
   };

   public int[] pallet_size = {84, 127, 81, 80, 120, 70, 132, 90};
   //int palletsize = Integer.parseInt(String.valueOf(pallet_size));

   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.order_by_brand);
      setTitle("Black Label");
      recyclerView = (RecyclerView) findViewById(R.id.recycler);
      //btnnext = (Button) findViewById(R.id.next);
      modelArrayList = getModel();
      customAdapter = new CustomAdapter(this);
      recyclerView.setAdapter(customAdapter);
      recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false));
   }

   private ArrayList<Model> getModel() {
      ArrayList<Model> list = new ArrayList<>();
      for (int i = 0; i < 8; i++) {
         Model model = new Model();
         model.setNumber(0);
         model.setNumber2(0);
         model.setBrand(brandlist[i]);
         model.setPallet_size(pallet_size[i]);
         list.add(model);
      }
      return list;
   }
}

现在我想使用第二类的arraylist:

public class CastleLager extends brands {

   private RecyclerView recyclerView;
   //private String modelArrayList;
   //private Context ctx;
   public static ArrayList<Model> modelArrayList;
   private CustomAdapter customAdapter;
   //private Button btnnext;
   public String[] castlebrandlist = new String[] {
      "Castle Lager 340ml NRB (85023)",
      "Castle Lager 330ml Cans (85736)",
      "Castle Lager 500ml Cans (85023)",
      "Castle Lager 440ml NRB (86798)",
      "Castle Lager 330ml RB (85556)",
      "Castle Lager 750ml RB (85021)",
      "Castle Lager 340ml NRB 12 Pack (87009)",
      "Castle Lager 500ml Cans 12 Pack (85022)"
   };
}

我知道它可能非常基本,但正如我所说,我对java很新,只是无法理解从哪里开始。谢谢。

1 个答案:

答案 0 :(得分:3)

解决方案很简单:避免使用“相对”名称。

含义:当您拥有xyZ和x2.y2.Z时,请不要在源代码中使用名称Z,而应使用完全限定名称x.y.Zx2.y2.Z。< / p>

是的,这有时不方便,但这是您在这里唯一的选择。其他语言,如python允许您“重命名”导入的东西,但Java不允许。当两个类具有相同名称时,避免名称冲突的唯一选择是切换到完全限定名称。

只是为了确定:代码中的真实问题是您希望允许“读/写”访问字段< / strong>在不同地方的一个班级。你看 - 字段代表你的类的内部状态。您努力隐藏来自外部世界的此类信息 - 因为这是一个外部没有人应该关心或了解的实施细节。

所以这里真正的答案是退后一步,考虑一个更强大的设计 not 基于向外界揭示一个字段!