I started to learn android few weeks ago, sorry for wall of text, I tried to explain my problem as good as possible.
I'm using checkboxes in a listview created with Adapter to select ingredients. I want to pass selected items to next activity to use them at something else. The problem is that objects dosen't get there as they shoud, ingredients are not selected in ShowRecipes activity.
Here a part of my code
Ingredient class
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);
}
}
Adapter
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.
}
}
What am I doing wrong? How shoud I check which ingredients are selected and how shoud I pass them to next activity?
Aucun commentaire:
Enregistrer un commentaire