If-else doesn't work properly with my check box - android

I have a grid in my application and when I select an item it load the items prospectively. I want to allow to select the check box only when isMayoBaseAvailable returns true.
This is the code I used, when I used this code and debug it work correctly, but when I run the application and check it always go inside the false (In all the items). Why does it happens?
I have shown my logcat as well.
#Override
public void onTaskCompleted(JSONArray responseJson) {
try {
List<String> crust = new ArrayList<String>();
List<String> descriptionHalf = new ArrayList<String>();
final List<String> description = new ArrayList<String>();
List<String> extraDescription = new ArrayList<String>();
for (int i = 0; i < responseJson.length(); ++i) {
JSONObject object = responseJson.getJSONObject(i);
String isMayoBaseAvailable = object
.getString("IsMayoBaseAvailable");
mayoBaseCB = (CheckBox) findViewById(R.id.ch_mayobase);
if (isMayoBaseAvailable.contains("true")) {
mayoBaseCB.setEnabled(true);
} else {
mayoBaseCB.setEnabled(false);
}
Logcat shows some items has isMayoBaseAvailable as true and some has as false.

Related

How to put ArrayObjectAdapter items in order

I am displaying multiple rows in one header and there is 5 more categories like drama and I am doing the same job for them. My question is when I set the adapter categories displaying randomly.(assuming of request speed).For example I want the display Drama on the first line always. I gave them id of adapter's constructor but didn't work out. Code :
if (response.body().getItems().size() > 0) {
Log.d("Drama",""+response.body().getItems().size());
List<MovieItem> newMovieItems = response.body().getItems();
HeaderItem liveHeader = new HeaderItem("Drama");
LiveChannelPresenter liveChannelPresenter = new LiveChannelPresenter();
ArrayObjectAdapter liveRowAdapter = new ArrayObjectAdapter(liveChannelPresenter);
for (int i = 0; i < newMovieItems.size(); i++) {
movieItem2 = response.body().getItems().get(i);
liveRowAdapter.add(movieItem2);
}
mRowsAdapter.add(new ListRow(0,liveHeader,liveRowAdapter));
}
}
if (response.body().getItems().size() > 0) {
Log.d("Drama",""+response.body().getItems().size());
List<MovieItem> newMovieItems = response.body().getItems();
//sorted:
Arrays.sort(newMoviewItems, new Comparator() { some condition to sort });
HeaderItem liveHeader = new HeaderItem("Drama");
LiveChannelPresenter liveChannelPresenter = new LiveChannelPresenter();
ArrayObjectAdapter liveRowAdapter = new ArrayObjectAdapter(liveChannelPresenter);
for (int i = 0; i < newMovieItems.size(); i++) {
//note the change here \|/
movieItem2 = newMoviewItems.get(i);
liveRowAdapter.add(movieItem2);
}
mRowsAdapter.add(new ListRow(0,liveHeader,liveRowAdapter));
}

Skipping single item in a sorting method

I'm having an issue while i'm looking to sort my Variables to fees Menues, i mean i have an ArrayList with data as "WITH PEPERONI,1," or "CHAMPAGNE,1,2," where ,1, or ,1,2, means the menu of the variable so when i press on Menu 1 i have to see only variables that had ,1, or ,1,2, or ,1,2,3, (where there is 1) in their array.
And actually what i've done works but only with variables that has multiple menues i mean if a variable is in menu 1,2,3,4 and i press on 2 that will be visible but if the variable is just in one menu as 1 so ,1, in array that will be not visualized and i can't get why.
here is my code where i filter the variables and set them in a new Array:
public void FilterVariable() {
filteredVariable = new ArrayList<>();
for (VariantiConstructor varianti : variantiConstructors) {
String data = varianti.getMenu();
String[] items = data.split("," + positionMenu + ",");
try {
if (items[0].equals(data)) {
//
} else {
filteredVariable.add(varianti);
}
} catch (Exception e) {
//
}
}
}
While here is a screen from where i was debugging and where there was a ,2, and it skipped it insteam of adding in the ArrayList:
You need to use the following code for the shorting the ArrayList
ArrayList<String> YOUR_ARRAYLIST = new ArrayList<>();
private void searchDataFromList(String serachString) {
ArrayList<String> SEARCH_ARRAYLIST = new ArrayList<>();
for (int i = 0; i < YOUR_ARRAYLIST.size(); i++) {
if (serachString.contains(YOUR_ARRAYLIST.get(i))) {
SEARCH_ARRAYLIST.add(YOUR_ARRAYLIST.get(i));
}
}
}
On your click listener, u need to call this searchDataFromList() method as following
YOUR_CLICK.setOnClickListener(view -> {
String searchString ="WITH PEPERONI,1,";
String YOUR_SEARCH_STRING ="";
List<String> YOUR_SELECETD_LIST = Arrays.asList(searchString.split(","));
for (int i = 0; i <YOUR_SELECETD_LIST.size(); i++) {
if (YOUR_SELECETD_LIST.get(i).length()==1)
{
YOUR_SEARCH_STRING = YOUR_SELECETD_LIST.get(i);
System.out.println("VALUE IS ==>>>>> "+YOUR_SEARCH_STRING);
}
}
if (!YOUR_SEARCH_STRING.isEmpty())
{
searchDataFromList(YOUR_SEARCH_STRING);
}

How to add data in array on button click?

int size = mcq.size();
String arr[] = null;
int i;
{
for (i = 0; i < size; i++) {
if (op1.isPressed()) {
arr[i] = tv1.getText().toString();
// Log.e("Array",arr[i]);
} else if (op2.isPressed()) {
arr[i] = tv2.getText().toString();
//Log.e("Array",arr[i]);
} else if (op3.isPressed()) {
arr[i] = tv3.getText().toString();
// Log.e("Array",arr[i]);
} else if (op4.isPressed()) {
arr[i] = tv4.getText().toString();
//Log.e("Array",arr[i]);
}
I am trying to store the data in an array when the button is pressed,but it always shows null.And when the for loop is over I want to display my array.
here , Arrays in Java have a size so u cannot do like this. Instead of this
use list,
ArrayList<String> arr = new ArrayList<String>();
Inorder to do using String array :
String[] arr = new String[SIZEDEFNE HERE];
For ur answer :
ArrayList<String> arr = new ArrayList<String>();
int i;
{
for (i = 0; i < size; i++) {
if (op1.isPressed()) {
arr.add(tv1.getText().toString());
} else if (op2.isPressed()) {
arr.add(tv2.getText().toString());
} else if (op3.isPressed()) {
arr.add(tv3.getText().toString());
} else if (op4.isPressed()) {
arr.add(tv4.getText().toString());
}
Retrive value using
String s = arr.get(0);
This is because your string array is null. Declare it with the size as
String[] arr = new String[size];
Try Array List. use the following code in your main java
final ArrayList<String> list = new ArrayList<String>();
Button button= (Button) findViewById(R.id.buttonId);
final EditText editText = (EditText) findViewById(R.id.editTextId)
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
list.add(editText.getText().toString());
}
});
To avoid duplication in arraylist .You can use arraylist for faster then String array
ArrayList<String> list = new ArrayList<String>();
list.add("Krishna");
list.add("Krishna");
list.add("Kishan");
list.add("Krishn");
list.add("Aryan");
list.add("Harm");
System.out.println("List"+list);
HashSet hs = new HashSet();
hs.addAll(list);
list.clear();
list.addAll(hs);

Issue with android spinners - Loading distinct values in spinners

I'm using two Spinners to show the items I'm getting from the json response. I have 2 problems right now. When u check my logcat u can see there are items repeating (Right side list, u can see so many pan). I want to have 1 item only once in my Spinner. I want to use something similar to distinct we use in sql databases.
My second problem is,
Select pan in the 1 spinner then 2nd spinner should contain items related to pan. (select pan in 1st spinner and 2nd should display only Pan large, pan medium and personal pan)
#Override
public void onTaskCompleted(JSONArray responseJson) {
try {
List<String> crust = new ArrayList<String>();
List<String> description = new ArrayList<String>();
List<String> extraDescription = new ArrayList<String>();
for (int i = 0; i < responseJson.length(); ++i) {
JSONObject object = responseJson.getJSONObject(i);
if ((object.getString("MainCategoryID")).equals("1")
&& (object.getString("SubCategoryID")).equals("1")) {
JSONArray subMenuArray = object
.getJSONArray("SubMenuEntity");
for (int j = 0; j < subMenuArray.length(); ++j) {
JSONObject subMenuObject = subMenuArray
.getJSONObject(j);
Log.i("Crust", subMenuObject.getString("Crust"));
crust.add(subMenuObject.getString("Crust"));
Log.i("Description",
subMenuObject.getString("Description"));
description.add(subMenuObject.getString("Description"));
JSONArray extraItemEntityArray = subMenuObject
.getJSONArray("ExtraItemEntity");
}
}
crustSP = (Spinner) findViewById(R.id.sp_crust);
ArrayAdapter<String> dataAdapterCru = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, crust);
dataAdapterCru
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
crustSP.setAdapter(dataAdapterCru);
sizeSP = (Spinner) findViewById(R.id.sp_pizza_size);
ArrayAdapter<String> dataAdapterDes = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, description);
dataAdapterDes
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sizeSP.setAdapter(dataAdapterDes);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
Output of this
Call this method to get distinct descriptions and then set the adapter using the return value of this function...
public static ArrayList<String> removeDuplicatesFromList(ArrayList<String> descriptions)
{
ArrayList<String> tempList = new ArrayList<String>();
for(String desc : descriptions)
{
if(!tempList.contains(desc))
{
tempList.add(desc);
}
}
descriptions = tempList;
tempList = null;
return descriptions;
}
For instance
description = Utils.removeDuplicatesFromList(description);
ArrayAdapter<String> dataAdapterDes = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, description);
NOTE:
I would suggest you make a new class call it Utils.java and place the above method inside it and then call it i have mentioned above.
Like this...
import java.util.ArrayList;
public class Utils
{
private Utils()
{
//Its constructor should not exist.Hence this.
}
public static ArrayList<String> removeDuplicatesFromList(ArrayList<String> descriptions)
{
ArrayList<String> tempList = new ArrayList<String>();
for(String desc : descriptions)
{
if(!tempList.contains(desc))
{
tempList.add(desc);
}
}
descriptions = tempList;
tempList = null;
return descriptions;
}
}
I hope it helps.

Android listview add subitems

i want add subitem in a listview. This is my code
ArrayList<String> mAllData;
private String [] cognomi = {
//my string
};
public void setData() {
mAllData = new ArrayList<String>();
mAdapter = new MySimpleSearchAdapter(this);
for (int i = 0; i < cognomi.length; i++) {
mAdapter.addItem(cognomi[i]);
mAllData.add(cognomi[i]);
}
You can use a pre-defined android layout for this: http://www.javajirawat.com/2013/03/android-listview-tutorial_12.html

Categories

Resources