0

我有一个像这样的简单汽车数据库:

db.addCar(new Car("Alfa Romeo","Giulietta","1.4 TB Distinctive","Benzine","1.4","120","2012","5","1465","4351","54000"));
        db.addCar(new Car("Audi","A6","2.0 TDi","Diesel","2.0","170","2012","6","1455","4915","183600"));
        db.addCar(new Car("BMW","7 Series","7.30 ID Comfort","Benzine","3.0","245","2012","6","1450","4835","528000"));
        db.addCar(new Car("Chevrolet","Captiva","2.0 LT AT","Diesel","2.0","163","2012","6","1756","4673","79950"));
        db.addCar(new Car("Citroen","C5","1.6 HDi Comfort","Diesel","1.6","112","2012","5","1458","4779","67430"));

我正在使用 3 个微调器来选择前三列(品牌、型号、类型),我也可以使用这种方法将它们发送到我的结果 xml 页面

b3.setOnTouchListener(new View.OnTouchListener(){
             @Override
                public boolean onTouch(View comparePage2, MotionEvent event) {

                 Intent intent = new Intent(getApplicationContext(), Compare2.class);
                    //Create a bundle object
                    Bundle car = new Bundle();

                    //Inserts a String value into the mapping of this Bundle
                    car.putString("make", spinner_make.getSelectedItem().toString());
                    car.putString("model", spinner_model.getSelectedItem().toString());
                    car.putString("type", spinner_type.getSelectedItem().toString());
                    car.putString("make2", spinner_make2.getSelectedItem().toString());
                    car.putString("model2", spinner_model2.getSelectedItem().toString());
                    car.putString("type2", spinner_type2.getSelectedItem().toString());


                    //Add the bundle to the intent.
                    intent.putExtras(car);


                    //start the DisplayActivity
                    startActivity(intent);

                return false;
                }

private void loadSpinnerData() {
        DatabaseHandler db = new DatabaseHandler(getApplicationContext());

        List <String> makers = db.getAllMakers();
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, makers);

        List <String> models = db.getAllModels();
        ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, models);

        List <String> types = db.getAllTypes();
        ArrayAdapter<String> dataAdapter3 = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, types);

        List <String> makers2 = db.getAllMakers();
        ArrayAdapter<String> dataAdapter4 = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, makers2);

        List <String> models2 = db.getAllModels();
        ArrayAdapter<String> dataAdapter5 = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, models2);

        List <String> types2 = db.getAllTypes();
        ArrayAdapter<String> dataAdapter6 = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, types2);


        spinner_make.getContext();

// Drop down layout style - list view with radio button
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

// attaching data adapter to spinner
        spinner_make.setAdapter(dataAdapter);
        spinner_model.setAdapter(dataAdapter2);
        spinner_type.setAdapter(dataAdapter3);
        spinner_make2.setAdapter(dataAdapter4);
        spinner_model2.setAdapter(dataAdapter5);
        spinner_type2.setAdapter(dataAdapter6);


}

但是我如何使用相同的方法将其他字段发送到该 xml 页面?

4

1 回答 1

1

简短的回答是:您需要Activity从数据库中检索要发送到下一个的所有数据。话虽这么说,我可能会做出以下更改来实现这一点:

1. 从数据库中获取所有(必需的)数据

在您的DatabaseHandler类中,通过执行您的第一个代码片段的逆操作,创建一个返回所有汽车的方法。或者至少在 next 中返回您需要的所有数据Activity。我假设您将在这里返回所有数据:

Cursor c = // TODO: implement: query database for car data
List<Car> cars = new ArrayList<Car>();
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
    cars.add(new Car(...)); // add every car
}

2.有Car工具Serializable

将类的定义更改Car为扩展Serializable。这应该没问题,假设该类只是简单字段的集合:

public class Car implements Serializable {
    private static final long serialVersionUID = 1234567890L;

    // constructor(s), fields, getters, setters below
}

3.从微调器中获取选定的汽车

为了这个示例,我将在步骤 1 中使用所选位置从原始集合中获取汽车并将其放入Intent(无需为此创建单独Bundle的):

Intent intent = new Intent(getApplicationContext(), Compare2.class);
Car selectedCar = cars.get(spinner.getSelectedItemPosition());
intent.putSerializableExtra("selected_car", selectedCar);

您可以对多辆单车甚至整个集合执行此操作(前提是该集合也实现了Serializable; ie ArrayList)。在接收端,您可以执行与上述相反的操作来检索数据:

Car selectedCar = (Car) getIntent().getSerializableExtra("selected_car");

现在,您的代码片段表明您将在下一个比较不同汽车的规格Activity。如果是这种情况,那么您可能不必对上述内容进行太多更改。我会考虑更改适配器以从cars集合中获取它们的数据,而不是再次查询数据库以获取您已经拥有的单列数据 - 这应该是一个简单的更改。

如果您正在进行某种过滤,基于对不同微调器的选择,那么您需要确保返回汽车所有数据的方法考虑到这种选择。我会让你自己弄清楚这些细节。

于 2013-01-03T01:40:44.810 回答