这里我声明了产品类变量并分配了它。
产品.java
public class Product {
String[] name= new String[100];
int price;
int image;
boolean box;
Product(String[] _describe, int _price, int _image, boolean _box) {
name = _describe;
price = _price;
image = _image;
box = _box;
}
}
这是我的产品类别。我应该在上面的编码中更改什么? 公共(public)类 MainActivity 扩展 Activity { String[] data =new String[] {"否:1","否:2","否:3","否:4","否:5","否:6"}; String[] columnTags = new String[] {"ProcessName", "IpItem", "IpColor", "OpItem","OpColor", "PlanQty", "DcQty", "RecQty", "RtQty"}; ArrayList 产品 = new ArrayList(); ListAdapter1 boxAdapter;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fillData();
boxAdapter = new ListAdapter1(this, products);
ListView lvMain = (ListView) findViewById(R.id.lvMain);
lvMain.setAdapter(boxAdapter);
}
void fillData() {
for (int i = 0; i <= 20; i++) {
products.add(new Product(columnTags, i * 100,i * 553
, false));
}
}
public void showResult(View v) {
String result = "Selected Product are :";
int totalAmount=0;
for (Product p : boxAdapter.getBox()) {
if (p.box){
result += "\n" + p.name;
totalAmount+=p.image;
}
}
String total="Total="+totalAmount;
// Toast.makeText(this, result+"\n"+"Total Amount:="+totalAmount, Toast.LENGTH_LONG).show();
Bundle bundle=new Bundle();
bundle.putString("res",result);
bundle.putString("tot",total);
Intent intent = new Intent(MainActivity.this,MainActivity2Activity.class);
intent.putExtras(bundle);
startActivity(intent);
}
}
最佳答案
add()
的第一个参数需要一个 String[]
,并且您向它传递一个 String
。
改变一下,
products.add(columnTags[i],i*100,i*553,false)
至
products.add(columnTags,i*100,i*553,false)
同时执行以下操作,
for(...)
{
String temp = columnTags[i];
products.add(temp,i*100,i*553,false)
}
编辑
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fillData();
ArrayAdapter boxAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, new ArrayList<String>(Arrays.asList(products.name)));
ListView lvMain = (ListView) findViewById(R.id.lvMain);
lvMain.setAdapter(boxAdapter);
}
void fillData() {
String temp;
for (int i = 0; i <columnTags.length; i++) {
temp = columnTags[i];
products.add(new Product(temp, i * 100,i * 553, false));
}
}
关于java - 如何解决 Product 中的以下错误 Product() 无法应用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34562992/