我尝试使用复选框显示ListView,但未调用方法getBox()

时间:2015-12-30 07:26:25

标签: java android

这是我的MainActivity文件,在这里我使用了getBox()方法,我得到'无法解决方法'错误。如何解决?

public class MainActivity extends Activity {

ArrayList<Product> products = new ArrayList<Product>();
ListAdapter 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 = 1; i <= 20; i++) {
        products.add(new Product("Product " + i, i * 100,
                R.drawable.abc_ic_search, 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.price;
        }
    }
    Toast.makeText(this, result+"\n"+"Total Amount:="+totalAmount, Toast.LENGTH_LONG).show();
}
}

这是我的ListAdapter1 java文件 - 这里我定义了方法fox getBox()

这里我已经为getBox定义了methode =

ArrayList<Product> getBox() {
    ArrayList<Product> box = new ArrayList<Product>();
    for (Product p : objects) {
        if (p.box)
            box.add(p);
    }
    return box;
   } 

请清除此错误并解释为什么我的代码中出现了这样的错误。

1 个答案:

答案 0 :(得分:0)

使用ListAdapter boxAdapter;

更新ListAdapter1 boxAdapter;