如何解决以下错误Product中的Product()无法应用?

时间:2016-01-02 05:39:34

标签: java android

enter image description here

这里我已经声明了产品类变量并分配了它。

product.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;
    }
}

这是我的产品类。我应该在上面的编码中改变什么? 公共类MainActivity扩展Activity {  String [] data = new String [] {" no:1"," no:2"," no:3"," no:4& #34;" NO:5"" NO:6"};     String [] columnTags = new String [] {" ProcessName"," IpItem"," IpColor"," OpItem"," OpColor"," PlanQty"," DcQty"," RecQty"," RtQty"};     ArrayList products = 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);
}

}

1 个答案:

答案 0 :(得分:0)

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));
  }
}