没有找到ArrayList

时间:2017-08-07 16:58:09

标签: java android arraylist

我创建了一个ArrayList来填充CustomAdapter。到目前为止一切都有效,但ArrayList的文本不会出现。甚至按钮也会更新点击上的数字文本。如果我在ArrayList中放置一个断点,它不会在那里中断,所以找不到整个ArrayList。为什么,如果在错误的地方宣布,我应该把它放在哪里。

以下是MainActivity:

public class MainActivity extends AppCompatActivity {

private RecyclerView recyclerView;
public static ArrayList<Model> brandlist;
private CustomAdapter customAdapter;
private Button btnnext;
//public static ArrayList brandlist;

public static void onCreate(String[] args) {

    ArrayList brandlist = new ArrayList<>();

        brandlist.add("340ml NRB (85023)");
        brandlist.add("330ml Cans (85736)");
        brandlist.add("500ml Cans (85024)");
        brandlist.add("440ml NRB (86798)");
        brandlist.add("330ml RB (85556)");
        brandlist.add("750ml RB (85021)");
        brandlist.add("340ml NRB 12 pack (87009)");
        brandlist.add("500ml Cans 12 Pack (85022)");
  }

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    recyclerView = (RecyclerView) findViewById(R.id.recycler);
    btnnext = (Button) findViewById(R.id.next);

    brandlist = getModel();
    customAdapter = new CustomAdapter(this);
    recyclerView.setAdapter(customAdapter);
    recyclerView.setLayoutManager(new LinearLayoutManager     (getApplicationContext(), LinearLayoutManager.VERTICAL, false));

    btnnext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent (MainActivity.this,NextActivity.class);
            startActivity(intent);
        }
    });
}

private ArrayList<Model> getModel(){
    ArrayList<Model> list = new ArrayList<>();
    for(int i = 0; i < 8 ; i++){

        Model model = new Model();
        model.setNumber(0);
        model.setBrandlist(brandlist);
        list.add(model);
    }
    return list;
}
}

这是CustomAdapter:

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) MainActivity.brandlist.get(position).getBrandlist());
    holder.tvCases.setText(String.valueOf(MainActivity.brandlist.get(position).getNumber()));
    holder.tvPallets.setText(String.valueOf(MainActivity.brandlist.get(position).getNumber()));

}

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

class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

    protected Button btn_cases_plus, btn_cases_minus, btn_pallets_plus, btn_pallets_minus;
    private TextView tvBrand, tvCases,tvPallets;

    public MyViewHolder(View itemView) {
        super(itemView);

        tvBrand = (TextView) itemView.findViewById(R.id.brand_name);
        tvCases = (TextView) itemView.findViewById(R.id.cases_text_view);
        tvPallets = (TextView) itemView.findViewById(R.id.pallets_text_view);
        btn_cases_plus = (Button) itemView.findViewById(R.id.casePlus1);
        btn_cases_minus = (Button) itemView.findViewById(R.id.caseMinus1);
        btn_pallets_plus = (Button) itemView.findViewById(R.id.palletsPlus1);
        btn_pallets_minus = (Button) itemView.findViewById(R.id.palletsMinus1);

        btn_cases_plus.setTag(R.integer.btn_cases_plus_view, itemView);
        btn_cases_minus.setTag(R.integer.btn_cases_minus_view, itemView);
        btn_cases_plus.setOnClickListener(this);
        btn_cases_minus.setOnClickListener(this);
        btn_pallets_plus.setTag(R.integer.btn_pallets_plus_view, itemView);
        btn_pallets_minus.setTag(R.integer.btn_pallets_minus_view, itemView);
        btn_pallets_plus.setOnClickListener(this);
        btn_pallets_minus.setOnClickListener(this);

    }

    // onClick Listener for view
    @Override
    public void onClick(View v) {

        if (v.getId() == btn_cases_plus.getId()){

            View tempview = (View) btn_cases_plus.getTag(R.integer.btn_cases_plus_view);
            TextView tv = (TextView) tempview.findViewById(R.id.cases_text_view);
            int number = Integer.parseInt(tv.getText().toString()) + 1;
            tv.setText(String.valueOf(number));
            MainActivity.brandlist.get(getAdapterPosition()).setNumber(number);

        } else if(v.getId() == btn_cases_minus.getId()) {

            View tempview = (View) btn_cases_minus.getTag(R.integer.btn_cases_minus_view);
            TextView tv = (TextView) tempview.findViewById(R.id.cases_text_view);
            int number = Integer.parseInt(tv.getText().toString()) - 1;
            tv.setText(String.valueOf(number));
            MainActivity.brandlist.get(getAdapterPosition()).setNumber(number);
        }
    }

}
}

这是模型文件:

public class Model {

private int number;
private ArrayList brandlist;

public int getNumber() {
    return number;
}

public void setNumber(int number) {
    this.number = number;
}

public ArrayList getBrandlist() {
    return brandlist;
}

public void setBrandlist(ArrayList brandlist) {
    this.brandlist = brandlist;
}

public ArrayList getbrandlist() {
    return brandlist;
}
}

1 个答案:

答案 0 :(得分:2)

这里有一个非常混乱的设置,可能会导致问题。那就是说......

首先,此方法不执行任何操作:

public static void onCreate(String[] args) {
    ArrayList brandlist = new ArrayList<>();
    brandlist.add("340ml NRB (85023)");
    brandlist.add("330ml Cans (85736)");
    brandlist.add("500ml Cans (85024)");
    brandlist.add("440ml NRB (86798)");
    brandlist.add("330ml RB (85556)");
    brandlist.add("750ml RB (85021)");
    brandlist.add("340ml NRB 12 pack (87009)");
    brandlist.add("500ml Cans 12 Pack (85022)");
}

此签名没有生命周期方法,因此除非您手动调用它,否则永远不会调用它。您似乎复制了非Android程序的main()方法,并将名称更改为onCreate()

即使您手动调用此方法,它也不会执行任何操作。第一行(ArrayList brandlist = ...)正在创建一个列表并将其分配给局部变量。 初始化您的MainActivity.brandlist

真实的 onCreate()方法(第二个)中,您使用MainActivity.brandlist调用的结果初始化getModel()。在您调用此方法时,MainActivity.brandlist为空,因此您实际上构建了一个包含number = 0和brandlist = null的八个项目的列表。

从那里你创建你的适配器等,看起来都很好。

我建议退一步思考你想要完​​成的事情。可能你最终会重写这段代码的大部分内容。也许同事或老师可以帮助解决这个问题。