gpt4 book ai didi

java - 找不到 ArrayList

转载 作者:行者123 更新时间:2023-11-29 19:10:29 27 4
gpt4 key购买 nike

我制作了一个 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;
}
}

最佳答案

这里的设置非常困惑,很可能会导致以后出现问题。话虽这么说……

首先,这个方法什么都不做:

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

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

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

我建议退后一步,思考您要完成的目标。您可能最终会重写这段代码的大部分。也许同事或老师可以提供帮助。

关于java - 找不到 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45552064/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com