hi dear here I am posting my whole class where I am getting problem. First I explain my problem. I am generating an arrayList "Items" using spinners and the elements in spinners and arrays are exactly same then After comparing array length and ArayList size I want to jump on next activity but problem is occoured in comparing arrays now please help me for this problem
Thanks here is my activity
package com.example.mine4.pantryrecipes;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import java.util.List;
import java.util.ArrayList;
import android.view.View;
import android.widget.Toast;
import android.widget.Button;
import android.widget.TextView;
public class AddItem extends Activity {
MultiSelectionSpinner spinner1,spinner2,spinner3,spinner4;
Button button1;
TextView tv;
private int count = 0;
List<String> Items=new ArrayList<>();
List<String> veg=new ArrayList<>();
List<String> spice=new ArrayList<>();
List<String> dairy=new ArrayList<>();
ArrayList<Boolean> subset = new ArrayList<>();
String[] arrayRecipe = { "chicken","vegetable oil","ginger",
"onion", "garlic","potatoes","tomatoes","roasted peanuts", "peanut butter"};
String[] arrayRecipe2 = { "chicken","garlic","vegetable oil", "tomatoes",
"Dijon mustard","breadcrumbs","Parmesan cheese","unsalted butter"};
String[] arrayRecipe3 = { "chicken", "vegetable oil","unsalted butter",
"sugar", "garlic", "sauce"};
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_item);
button1=(Button)findViewById(R.id.button1);
tv=(TextView)findViewById(R.id.tv);
String[] arrayItems = {"chicken"};
String[] arrayVeg = { "vegetable oil", "ginger",
"garlic","potatoes" ,"tomatoes","onion"};
String[] arraySpice = { "roasted peanuts", "sauce","Dijon mustard" };
String[] arrayDairy = { "breadcrumbs", "Parmesan cheese", "unsalted butter","sugar", "peanut butter" };
spinner1 = (MultiSelectionSpinner) findViewById(R.id.mySpinner1);
spinner2 = (MultiSelectionSpinner) findViewById(R.id.mySpinner2);
spinner3 = (MultiSelectionSpinner) findViewById(R.id.mySpinner3);
spinner4= (MultiSelectionSpinner) findViewById(R.id.mySpinner4);
spinner1.setItems(arrayItems);
spinner2.setItems(arrayVeg);
spinner3.setItems(arraySpice);
spinner4.setItems(arrayDairy);
}
public void onClick(View v)
{
Items=spinner1.getSelectedStrings();
veg=spinner2.getSelectedStrings();
spice=spinner3.getSelectedStrings();
dairy=spinner4.getSelectedStrings();
Items.addAll(veg);
Items.addAll(spice);
Items.addAll(dairy);
// for(int i=0;i<Items.size();i++)
// tv.append(Items.get(i));
compareArray();
//count++;
}
public void compareArray()
{
for(int j = 0 ; j < arrayRecipe.length ; j++)
{
for(int i = 0 ; i <Items.size() ; i++)
{
if((arrayRecipe[i].equals(Items.get(j))))
{
subset.add(true);
break;
}
if ((arrayRecipe2[i].equals(Items.get(j))))
{
subset.add(true);
break;
}
if ((arrayRecipe3[i].equals(Items.get(j))))
{
subset.add(true);
break;
}
}
}
if(subset != null) {
if ((arrayRecipe.length == subset.size())) {
Intent nextClass = new Intent();
nextClass.setClass(AddItem.this, TenIngreRec.class);
startActivity(nextClass);
finish();
Toast.makeText(getApplicationContext(), "arrayRecipe", Toast.LENGTH_LONG).show();
}
if ((arrayRecipe2.length == subset.size())) {
Intent nextClass = new Intent();
nextClass.setClass(AddItem.this, EightIngreRec.class);
startActivity(nextClass);
finish();
Toast.makeText(getApplicationContext(), "arrayRecipe2", Toast.LENGTH_LONG).show();
}
if ((arrayRecipe3.length == subset.size())) {
Intent nextClass = new Intent();
nextClass.setClass(AddItem.this, EightIngreRec.class);
startActivity(nextClass);
finish();
Toast.makeText(getApplicationContext(), "arrayRecipe3", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(getApplicationContext(), "Un-Matched", Toast.LENGTH_LONG).show();
}
}
}
#Override
public void onBackPressed()
{
// code here to show dialog
super.onBackPressed(); // optional depending on your needs
Intent intn = new Intent(AddItem.this,Exmain.class);
startActivity(intn);
}
}
and the major problem occured in this code which is array indexOutOfBound
for(int j = 0 ; j < arrayRecipe.length ; j++)
{
for(int i = 0 ; i <Items.size() ; i++)
{
if((arrayRecipe[i].equals(Items.get(j))))
{
subset.add(true);
break;
}
if ((arrayRecipe2[i].equals(Items.get(j))))
{
subset.add(true);
break;
}
if ((arrayRecipe3[i].equals(Items.get(j))))
{
subset.add(true);
break;
}
}
}
thanks again for helping me
array.Length() always count from 1 not form zero use
arrayRecipe.length -1 in your for loop
Use This
for(int j = 0 ; j < (arrayRecipe.length)-1 ; j++)
{
for(int i = 0 ; i <Items.size() ; i++)
{
if((arrayRecipe[i].equals(Items.get(j))))
{
subset.add(true);
break;
}
if ((arrayRecipe2[i].equals(Items.get(j))))
{
subset.add(true);
break;
}
if ((arrayRecipe3[i].equals(Items.get(j))))
{
subset.add(true);
break;
}
}
}
Two problems:
You're using the size of arrayRecipe to bound the index of arrayRecipe, arrayRecipe2 and arrayRecipe3 All three arrays are different in size, hence leading to your error. You need 3 different for loops, one for each arrayRecipe.
Your i and j are in the wrong places.
Items.get(j) should be Items.get(i) as you are using Items.size to limit the size of i. Likewise, arrayRecipe.get(i) should be arrayRecipe.get(j).
Also, it is true that getLength() and getSize() start counting from 1 rather than 0 so the index of the final slot is (getLenght() - 1), but by using < as the clause in your for loop handles that.
I custom listview with adapter extend BaseAdapter.
In method getView I have a button Add, and I set listener to it
item.btnAddBasket.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
showChooseSize(products.get(pos), pos);
DialogFragmentCustom.setPosition(pos);
}
});
And I have two method, which are call when I press button Add.
public void showChooseSize(ProductInfo productInfo, int pos) {
ArrayList<Stock> stocks = new ArrayList<Stock>();
stocks.addAll(productInfo.getStock());
ArrayList<String> colors = new ArrayList<String>();
ArrayList<String> sizes = new ArrayList<String>();
if (stocks != null && stocks.size() > 0) {
for (int i = 0; i < stocks.size() - 1; i++) {
colors.add(stocks.get(i).getColorName());
sizes.add(stocks.get(i).getSize());
}
}
if (sizes != null && sizes.size() > 0) {
HashSet<String> hs_size = new HashSet<String>(); // remove elements
// duplicate
hs_size.addAll(sizes);
sizes.clear();
sizes.addAll(hs_size);
if (sizes.size() == 1) {
size = sizes.get(0);
} else {
for (int j = 0; j < sizes.size() - 1; j++) {
size = size + sizes.get(j) + ", ";
}
size = size + sizes.get(sizes.size() - 1);
}
a_sizes = null;
if (size.split(",") != null) {
a_sizes = size.split(",");
}
if (a_sizes == null) {
a_sizes[0] = "This product do not have size";
}
if (a_sizes != null && a_sizes.length > 0) {
Config.isWishList = true;
((Activity) context).setTheme(R.style.ActionSheetStyleIOS7);
ActionSheetChooseSize.setProductInfo(productInfo);
ActionSheetChooseSize.createBuilder(context, ((FragmentActivity) context).getSupportFragmentManager()).setOtherButtonTitles(a_sizes)
.setCancelableOnTouchOutside(true).setListener(this).show();
isShowChooseSize = true;
} else {
addToBag(productInfo, pos);
}
}
Method add product into Bag when press button Add, it inside my adapter
public void addToBag(ProductInfo productInfo, int pos) {
// Config.productSaleInfos.add(products.get(pos));
Config.productSaleInfos.add(productInfo);
Config.numProductSale = Config.productSaleInfos.size();
Config.numProductWishlist = Config.numProductWishlist - 1 < 0 ? Config.numProductWishlist - 1 : 0;
//SlideMenuFragment.setNumWish(Config.numProductWishlist + "");
new DeletionItemWishlist(context).execute(products.get(pos));
products.remove(pos);
notifyDataSetChanged();
float sumaryPrice = 0;
for (int i = 0; i < products.size(); i++) {
String price = products.get(i).getSalePrice();
int index = price.indexOf(",");
if (index > -1) {
price = price.replace(",", "");
}
float p = Functions.getFloatFromString(price);
sumaryPrice = sumaryPrice + p;
}
basket.setText(Config.numProductSale + "");
subtotal.setText(Html.fromHtml("<b>Subtotal: </b>" + "<font color=\"#FF7F00\">" + sumaryPrice + " ฿</font>"));
Functions.showLog(context, "add bag success!");
}
But notifyDataSetChanged(); is not working in my code. Can you help me please?
I wanted to view the items selected in my listview with checkboxes. However, when I try to print out lv.getItemAtPosition(i).toString() it gives me a `` object. How can I convert this to a readable format?
public void blockCheckedItems(View view) {
// int cntChoice = lv.getCount();
checked = new ArrayList<String>();
unchecked = new ArrayList<String>();
int itempositions=adapter.getCount();
SparseBooleanArray sparseBooleanArray = lv.getCheckedItemPositions();
int countChoice = lv.getChildCount();
Log.d("" + countChoice, "CountChoice===============================");
Log.d("" + sparseBooleanArray,"sparseBooleanArray -------------------------");
for(int i = 0; i < countChoice; i++)
{
if(sparseBooleanArray.get(i) == true)
{
checked.add(lv.getItemAtPosition(i).toString());
}
else if(sparseBooleanArray.get(i) == false)
{
unchecked.add(lv.getItemAtPosition(i).toString());
}
}
for(int i = 0; i < checked.size(); i++){
Log.d("checked list&&&&&&&&&&&&&&&&&&&", "" + checked.get(i));
}
for(int i = 0; i < unchecked.size(); i++){
Log.d("in unchecked list&&&&&&&&&&&&&&&&&&&", "" + unchecked.get(i));
}
}
try something like this:
if(sparseBooleanArray.get(i) == true)
{
checked.add(Boolean.toString(sparseBooleanArray.get(i)));
}
else if(sparseBooleanArray.get(i) == false)
{
unchecked.add(Boolean.toString(sparseBooleanArray.get(i)));
}
I have solved the above questions by changing the following code and using Cursor and keeping everyother part of the above code the same.
for(int i = 0; i < countChoice; i++)
{
if(sparseBooleanArray.valueAt(i) == true)
{
Cursor cursor = (Cursor) lv.getItemAtPosition(i);
String name = cursor.getString(1);
checked.add(name);
}
else if(sparseBooleanArray.valueAt(i) == false)
{
Cursor cursor = (Cursor) lv.getItemAtPosition(i);
String name = cursor.getString(1);
unchecked.add(name);
}
}
I have many buttons(81) from the left and right part of my layout. All in all, I have 162 buttons. I put those buttons to Button[] and I handle it properly.
private int[] right_lung = { R.id.btn_right_106, R.id.btn_right_113,
R.id.btn_right_114, R.id.btn_right_115, R.id.btn_right_116, R.id.btn_right_121,
R.id.btn_right_122, R.id.btn_right_123, R.id.btn_right_124, R.id.btn_right_125,
R.id.btn_right_129, R.id.btn_right_130, R.id.btn_right_131, R.id.btn_right_132,
R.id.btn_right_133, R.id.btn_right_134, R.id.btn_right_137, R.id.btn_right_138,
R.id.btn_right_139, R.id.btn_right_140, R.id.btn_right_141, R.id.btn_right_142,
R.id.btn_right_143, R.id.btn_right_145, R.id.btn_right_146, R.id.btn_right_147,
R.id.btn_right_148, R.id.btn_right_149, R.id.btn_right_150, R.id.btn_right_151,
R.id.btn_right_152, R.id.btn_right_153, R.id.btn_right_154, R.id.btn_right_155,
R.id.btn_right_156, R.id.btn_right_157, R.id.btn_right_158, R.id.btn_right_159,
R.id.btn_right_160, R.id.btn_right_161, R.id.btn_right_162, R.id.btn_right_163,
R.id.btn_right_164, R.id.btn_right_165, R.id.btn_right_166, R.id.btn_right_167,
R.id.btn_right_168, R.id.btn_right_169, R.id.btn_right_170, R.id.btn_right_171,
R.id.btn_right_172, R.id.btn_right_173, R.id.btn_right_174, R.id.btn_right_175,
R.id.btn_right_176, R.id.btn_right_177, R.id.btn_right_178, R.id.btn_right_179,
R.id.btn_right_180, R.id.btn_right_181, R.id.btn_right_182, R.id.btn_right_183,
R.id.btn_right_184, R.id.btn_right_185, R.id.btn_right_186, R.id.btn_right_187,
R.id.btn_right_188, R.id.btn_right_189, R.id.btn_right_190, R.id.btn_right_191,
R.id.btn_right_192, R.id.btn_right_194, R.id.btn_right_195, R.id.btn_right_196,
R.id.btn_right_197, R.id.btn_right_198, R.id.btn_right_199, R.id.btn_right_200,
R.id.btn_right_205, R.id.btn_right_206, R.id.btn_right_207 };
private Button[] btn_right = new Button[right_lung.length];
private int[] left_lung = { R.id.btn_left_7, R.id.btn_left_13, R.id.btn_left_14,
R.id.btn_left_15, R.id.btn_left_16, R.id.btn_left_20, R.id.btn_left_21,
R.id.btn_left_22, R.id.btn_left_23, R.id.btn_left_24, R.id.btn_left_27,
R.id.btn_left_28, R.id.btn_left_29, R.id.btn_left_30, R.id.btn_left_31,
R.id.btn_left_32, R.id.btn_left_34, R.id.btn_left_35, R.id.btn_left_36,
R.id.btn_left_37, R.id.btn_left_38, R.id.btn_left_39, R.id.btn_left_40,
R.id.btn_left_41, R.id.btn_left_42, R.id.btn_left_43, R.id.btn_left_44,
R.id.btn_left_45, R.id.btn_left_46, R.id.btn_left_47, R.id.btn_left_48,
R.id.btn_left_49, R.id.btn_left_50, R.id.btn_left_51, R.id.btn_left_52,
R.id.btn_left_53, R.id.btn_left_54, R.id.btn_left_55, R.id.btn_left_56,
R.id.btn_left_57, R.id.btn_left_58, R.id.btn_left_59, R.id.btn_left_60,
R.id.btn_left_61, R.id.btn_left_62, R.id.btn_left_63, R.id.btn_left_64,
R.id.btn_left_65, R.id.btn_left_66, R.id.btn_left_67, R.id.btn_left_68,
R.id.btn_left_69, R.id.btn_left_70, R.id.btn_left_71, R.id.btn_left_72,
R.id.btn_left_73, R.id.btn_left_74, R.id.btn_left_75, R.id.btn_left_76,
R.id.btn_left_77, R.id.btn_left_78, R.id.btn_left_79, R.id.btn_left_80,
R.id.btn_left_81, R.id.btn_left_82, R.id.btn_left_83, R.id.btn_left_84,
R.id.btn_left_85, R.id.btn_left_86, R.id.btn_left_87, R.id.btn_left_88,
R.id.btn_left_89, R.id.btn_left_90, R.id.btn_left_91, R.id.btn_left_92,
R.id.btn_left_93, R.id.btn_left_94, R.id.btn_left_95, R.id.btn_left_98,
R.id.btn_left_99, R.id.btn_left_100, };
private Button[] btn_left = new Button[left_lung.length];
Whenever I click on the button, just like in mine sweeper game, many buttons are randomly opened. And when it is opened, I am changing its background into R.drawable.affected. The goal of the game is to open all the buttons in left and right. My question is this, how can I check if all of the buttons are set in R.drawable.affected? Because after that, I will execute a method that will congratulate the user. Thanks in advance.
EDIT:
for (int i = 0; i < btn_right.length; i++) {
final int a = i;
int counter_total_affected = 0;
btn_right[i].setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (counter == 1) {
right_lung.add(a);
btn_right[a].setBackgroundResource(R.drawable.affected);
counter_total_affected++;
} else if (counter == 2) {
for (int i = 0; i < 2; i++) {
int n = i;
right_lung.add(a + n);
}
try {
for (int i = 0; i < 2; i++) {
int n = i;
btn_right[a + n].setBackgroundResource(R.drawable.affected);
counter_total_affected++;
}
} catch (Exception e) {
e.printStackTrace();
}
} else if (counter == 3) {
right_lung.add(a);
btn_right[a].setBackgroundResource(R.drawable.affected);
counter_total_affected++;
} else if (counter == 4) {
for (int i = 0; i < 25; i++) {
int n = i;
right_lung.add(a + n);
}
try {
for (int i = 0; i < 25; i++) {
int n = i;
btn_right[a + n].setBackgroundResource(R.drawable.affected);
counter_total_affected++;
}
} catch (Exception e) {
e.printStackTrace();
}
} else if (counter == ...) {
//statements...
} else if (counter_total_affected == (btn_left.length + btn_right.length)) {
//CONGRATULATORY METHOD
}
counter++;
}
}
Increment the counter each time you change background of button and compare its value with length of button array. if both are same that means, all button backgrounds are set.
Try this :
for (int i = 0; i < btn_right.length; i++) {
final int b = i;
btn_right[i].setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if btn_right[i].getDrawable().getConstantState().equals
(getResources().getDrawable(R.drawable.affected).getConstantState()))
{
if(counter == btn_right.length){
//Congratulate user...
}
}else{
btn_right[b].setBackgroundResource(R.drawable.affected);
counter++;
}
}
}
U can set Tag:
for (int i = 0; i < btn_right.length; i++) {
final int b = i;
btn_right[i].setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (counter == 1) {
right_affected.add(b);
btn_right[b].setBackgroundResource(R.drawable.affected);
btn_right[b].setTag('1');
} else {
//some stuff here...
}
}
}
after congratulate the user set its Tag to 0.
EDIT:
ArrayList<Integer> arrayofId =new ArrayList<Integer>();
#Override
public void onClick(View v) {
if (counter == 1) {
right_affected.add(b);
btn_right[b].setBackgroundResource(R.drawable.affected);
arrayofId.add(b);//need to convert int to Integer.
} else {
//some stuff here...
}
}
To congratulate:
for(int i=0;i<arrayofId.size();i++)
{
// you can get here id of effected buttons
}