CheckBox选中的项目与我继续下一个活动不一样

时间:2016-05-14 20:44:26

标签: android listview checkbox android-checkbox

几个星期前我开始学习安卓,对不起文字墙,我尽力解释我的问题。

我在使用Adapter创建的列表视图中使用复选框来选择成分。我想将所选项目传递给下一个活动,以便在其他地方使用它们。问题是物体在它们响起时没有到达那里,在ShowRecipes活动中没有选择成分。

这是我的代码的一部分

成分类

public class Ingredient implements Serializable {
    String code;
    String nameI;
    boolean selected;    
    public Ingredient(String code, String nameI) {
        this.code = code;
        this.nameI = nameI;
    }    

    public boolean isSelected() {
        return selected;
    }    
    public void setSelected(boolean selected) {
        this.selected = selected;
    }
}

MainActivity

public class MainActivity extends AppCompatActivity {

    public static ArrayList<Ingredient> ingredients = new ArrayList<>();
    private Adapter m_adapter;
    private Activity activity;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView listView = (ListView) findViewById(R.id.listView1) ;

        Ingredient ing = new Ingredient("1","Cheese");
        ingredients.add(ing);
        ing = new Ingredient("2","Eggs");
        ingredients.add(ing);
        ing = new Ingredient("3","Pastas");
        ingredients.add(ing);
        ing = new Ingredient("4","Bacon");
        ingredients.add(ing);
        ing = new Ingredient("5","Tomatos");
        ingredients.add(ing);
        ing = new Ingredient("6","Watermalon");
        ingredients.add(ing);

        m_adapter = new Adapter(this, R.layout.list_item, ingredients);
        listView.setAdapter(m_adapter);
    }

    public void showRecipes(View view) {
        Intent i = new Intent(this, ShowRecipes.class);
        i.putExtra("data", new DataWarper(ingredients)); //I'm using DataWarper class to pass ingredients arraylist through intent
        startActivity(i);
    }
}

适配器

public class Adapter extends ArrayAdapter<Ingredient> {
    public LayoutInflater inflater;
    public ArrayList<Ingredient> listIngredients;
    private Activity activity;

    public Adapter(Activity activity, int textResourceId, ArrayList<Ingredient> ingredients) {
        super(activity, textResourceId, ingredients);
        this.activity=activity;
        this.listIngredients=ingredients;

        try {
            inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public int getCount() {
        return listIngredients.size();
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;

        if (convertView == null) {
            v = inflater.inflate(R.layout.list_item, null);
        }

        final Ingredient ingredients = getItem(position);

        final TextView display_ingredients = (TextView) v.findViewById(R.id.name);
        display_ingredients.setText(ingredients.getNameI());

        final CheckBox checkBox = (CheckBox) v.findViewById(R.id.checkBox);

        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!ingredients.isSelected()) {
                    checkBox.setChecked(true);
                    ingredients.setSelected(true);    
                }
                else{
                    checkBox.setChecked(false);
                    ingredients.setSelected(false);
                    String s="";
                    if(ingredients.isSelected())
                }
            }
        });
        return v;
    }
}

ShowRecipes

public class ShowRecipes extends AppCompatActivity {
    private ArrayList<Recipe> recipes= new ArrayList<>();
    private AdapterRecipes m_adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recipies);

        Intent intent = getIntent();
        DataWarper dw = (DataWarper) getIntent().getSerializableExtra("data");
        ArrayList<Ingredient> list = dw.getIngredients();

        ListView listView = (ListView) findViewById(R.id.listViewRecipies);
        ArrayList<String> selectedIng = new ArrayList<>();
        for (Ingredient ing : list)
        {
            if(ing.isSelected())
                selectedIng.add(ing.getCode());
        }
        // here, none of my items are selected even if checkboxes are checked.
    }
}

我做错了什么?我如何检查选择了哪些成分以及我将它们传递给下一个活动的方式是多么轻松?

1 个答案:

答案 0 :(得分:0)

查看Android Intent文档here

具体来说,使用Intent#putExtra时的The name must include a package prefix, ...

尝试这样的事情:

i.putExtra(PACKAGE_NAME + ".data", new DataWarper(ingredients));

其中PACKAGE_NAME等于您的应用程序的程序包名称。