【问题标题】:CheckBox selected items are not the same as I move on to next activityCheckBox 所选项目与我继续下一个活动不同
【发布时间】:2016-05-15 06:39:20
【问题描述】:

几周前我开始学习android,对于文字墙感到抱歉,我试图尽可能好地解释我的问题。

我在使用 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;
    }
}

主活动

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

显示食谱

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

我做错了什么?我应该如何检查选择了哪些成分以及如何将它们传递给下一个活动?

【问题讨论】:

    标签: android listview checkbox android-checkbox


    【解决方案1】:

    查看 Android Intent 文档here

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

    试试这样的:

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

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

    【讨论】:

    • 我在 i.putExtra(com.example.radu.fridgecheck+".data", new DataWarper(ingredients)) 上收到“错误:(58, 31) 错误:找不到符号类 radu”;
    • 现在我在 ArrayList list = dw.getIngredients(); 的 ShowRecipe 类中收到 Null 指针异常;
    • 您记得更改检索数据的字符串,对吧?你必须匹配你调用putExtra时使用的字符串。
    • 对不起,现在可以了,但是在 ShowRecipe 中我仍然没有与 listview 中相同的选定项目,也许问题出在其他地方。 :(
    • 也许现在问题出在其他地方,但您仍然可以将原始问题标记为我的回答已回答。 ;) 同时,我建议您提出一个不同的问题来解决您的新问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-15
    • 2012-04-25
    • 1970-01-01
    相关资源
    最近更新 更多