For loop last item issue - android

I am using a for loop in another for loop, Problem is I want Left options, Right Options and answers, but only getting last item of each. how will I get that? Please help me out
for (int i = 0; i < jsonArray.length(); i++) {
DataModel model = new DataModel();
jsonObject = jsonArray.getJSONObject(i);
LeftOpts = jsonObject.getString("LQUESTION");
RightOpts = jsonObject.getString("RQUESTION");
Ans = jsonObject.getString("TRUE_ANS");
String[] leftItem = LeftOpts.split("#");
String[] rightItem = RightOpts.split("#");
String[] ansItem = Ans.split("#");
for (int j = 0; j < leftItem.length; j++) {
item1 = leftItem[j];
Log.v("Left", item1);
model.setLOpt1(item1);
item2 = rightItem[j];
model.setROpt1(item2);
item3 = ansItem[j];
model.setAns1(item3);
myList.add(model);
}

You could try this:
for (int j = 0; j < leftItem.length; j++) {
DataModel model = new DataModel(); //move to here
item1 = leftItem[j];
Log.v("Left", item1);
model.setLOpt1(item1);
item2 = rightItem[j];
model.setROpt1(item2);
item3 = ansItem[j];
model.setAns1(item3);
myList.add(model);
}

Related

Android Json Handling issue

I want to display updated_at data to user only if value of password in folder Array is null.
JSONArray jsonArray = object.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject dataa = jsonArray.getJSONObject(i);
JSONArray jsonArray2 = dataa.getJSONArray("folder");
for (int i2 = 0; i2 < jsonArray.length(); i2++)
{
JSONObject dataa2 = jsonArray2.getJSONObject(i2);
String password = dataa2.getString("password");
if (password.equals("null") || password.equals(null))
{
//Display data
}
else
{
// dont Display data
}
}
Is there a way to do it?
Rectify your inner loop condition section.
JSONArray jsonArray2 = dataa.getJSONArray("folder");
for (int i2 = 0; i2 < jsonArray2.length(); i2++)
{
JSONObject dataa2 = jsonArray2.getJSONObject(i2);

Getting repeating loop infinit

I am using nested for loop for getting from arraylist and that loop run infinte
private ArrayList prepareData()
{
ArrayList arrayList = new ArrayList<>();
for(int i=0;i<android_image_urls.length;i++){
Androidversionclass androidversion = new Androidversionclass();
androidversion.setAndroid_image_url(android_image_urls[i]);
arrayList.add(androidversion);
for (int k= 0; k < android_image_second.length; k++) {
Androidversionclass androidversionclass1 = new Androidversionclass();
androidversion.setAndroid_image_second(android_image_second[k]);
arrayList.add(androidversion);
}
for (int l = 0; l < android_image_thirds.length; l++) {
Androidversionclass androidversionclass2 = new Androidversionclass();
androidversion.setAndroid_image_thirds(android_image_thirds[l]);
arrayList.add(androidversion);
}
for (int m = 0; m < android_image_fourth.length; m++) {
Androidversionclass androidversionclass3 = new Androidversionclass();
androidversion.setAndroid_image_fourth(android_image_fourth[m]);
arrayList.add(androidversion);
}
}
return arrayList;
}

display Multidimensional Arrays android

I'm using android studio and with opencv 3.4.0. I want to display a Multidimensional Array in text view. I have an a array "candidats_result" and I copied her values into a Multidimensional array " finale". after runing my code I got this result in emulator display. what I have to change in my code to get the text displayed
double [][] R_finale = new double[20][20];
int ZZ = 0;
int ZE = 0;
int EE = 0;
for(int i=0;i<19;i++){
for(int j=0;j<19;j++){
R_finale[i][j] = candidats_result[ZZ];
ZZ++;
}
}
String [][] finale = new String [20][20];
//showing the array in android
for(int i=0;i<19;i++) {
for (int j = 0; j < 19; j++) {
finale[i][j] = Double.toString(R_finale[i][j]);
}
}
String [][] details = new String[20][20];
StringBuilder builder = new StringBuilder();
for ( int i = 0 ; i<20;i++) {
for (int j = 0; j < 20; j++) {
details[i][j] = String.valueOf(R_finale [i][j]);
builder.append(details + ";");
}
}
textView.setText(String.valueOf(builder));
Notice below lines:
for ( int i = 0 ; i<20;i++) {
for (int j = 0; j < 20; j++) {
details[i][j] = String.valueOf(R_finale [i][j]);
builder.append(details + ";"); //should be builder.append(details[i][j] + ";");
}
}

Create a arraylist from output of a for loop

I am trying to get a data set as [{a,b,c,d} {e,f,g,h}] but I'm getting it as [a,b,c,d,e,f,g,h] using below mentioned code. What should I do to achieve that. any help will be highly appreciated. I'm new to android so please be kind enough to answer me. thanks in advance
List<String> CartItemEntityList = new ArrayList<String>();
for (int i = 0; i < mCartList.size(); i++) {
if (!mCartList.isEmpty()) {
Product product = mCartList.get(i);
int quantity = product.quantity;
double subTotal = product.subTotal;
CartItemEntity = product.items;
for (int j = 0; j < CartItemEntity.size(); j++) {
Item item = CartItemEntity.get(j);
subMenuCode = item.subMenuCode;
mainMenuCode = item.mainMenuCode;
price = item.price;
CartItemEntityList.add(mainMenuCode);
CartItemEntityList.add(String.valueOf(price));
CartItemEntityList.add(String.valueOf(quantity));
CartItemEntityList.add(subMenuCode);
}
}
System.out.println("CartItemEntityList " + CartItemEntityList);
}
I was able to achieve this using json serialization and this is my answer
JSONObject jsonObj = new JSONObject();
jsonObj.put("MainMenuCode",
item.getMainMenuCode());
jsonObj.put("Price", item.getPrice());
jsonObj.put("Quantity", product.getQuantity());
jsonObj.put("SubMenuCode",
item.getSubMenuCode());
String a = jsonObj.toString();
CartIddtemEntityList.add(a);
you have to create array of arraylist.
Try something like below:
ArrayList<ArrayList<String>> group = new ArrayList<ArrayList<String>>(mCartList.size());
for (int i = 0; i < mCartList.size(); i++) {
List<String> CartItemEntityList = new ArrayList<String>();
if (!mCartList.isEmpty()) {
Product product = mCartList.get(i);
int quantity = product.quantity;
double subTotal = product.subTotal;
CartItemEntity = product.items;
for (int j = 0; j < CartItemEntity.size(); j++) {
Item item = CartItemEntity.get(j);
subMenuCode = item.subMenuCode;
mainMenuCode = item.mainMenuCode;
price = item.price;
CartItemEntityList.add(mainMenuCode);
CartItemEntityList.add(String.valueOf(price));
CartItemEntityList.add(String.valueOf(quantity));
CartItemEntityList.add(subMenuCode);
}
}
System.out.println("CartItemEntityList " + CartItemEntityList);
group.add(CartItemEntityList);
}

I want to delete a null column from a 2d array in android?

i have problem in data binding in expandable list view. here i use
ArrayList<ExpandListGroup> list = new ArrayList<ExpandListGroup>();
ExpandListGroup for data binding. but in sum 2d array there is null value.Data is coming dynamically .
eg:
String [] [] array1 = [[one,two,three,null],[seven,six,null]] ;
I want to remove the null column from this two dimensional array
You need to take a intermediate arraylist and result array and follows the below steps.
First copy the source array(containing null) into arraylist.
Then remove null from arraylist
Then create new array and copy data from arraylist to array.
private void remove_nulls()
{
String [] [] array1 = {{"one","two","three",null},{"seven","six",null}} ;
ArrayList<ArrayList<String>> contact = new ArrayList<ArrayList<String>>();
for(int i=0;i<array1.length;i++)
{
ArrayList<String> con = new ArrayList<String>();
for(int j=0;j<array1[i].length;j++)
{
if(array1[i][j]!=null)
con.add(array1[i][j]);
}
if(con.size()>0)
contact.add(con);
}
String [] [] array2 = new String[array1.length][];
for(int i=0;i<contact.size();i++)
{
array2[i]=new String[contact.get(i).size()];
for(int j=0;j<contact.get(i).size();j++)
{
array2[i][j]=contact.get(i).get(j);
}
}
}
Unless there is some trick to the question...
String[][] array1 = {{"one", "two", "three", null}, {"seven", "six", null}};
List<String[]> newList = new ArrayList<>();
for (int i = 0; i < array1.length; ++i) {
List<String> currentLine = new ArrayList<>();
for (int j = 0; j < array1[i].length; ++j) {
if (array1[i][j] != null) {
currentLine.add(array1[i][j]);
}
}
//create the array in place
newList.add(currentLine.toArray(new String[currentLine.size()]));
}
//no need to use an intermediate array
String[][] array2 = newList.toArray(new String [newList.size()][]);
//And a test for array2
for (int i = 0; i < array2.length; ++i) {
for (int j = 0; j < array2[i].length; ++j) {
System.out.print(array2[i][j] + " ");
}
System.out.println();
}
System.out.println("Compared to...");
//Compared to the original array1
for (int i = 0; i < array1.length; ++i) {
for (int j = 0; j < array1[i].length; ++j) {
System.out.print(array1[i][j] + " ");
}
System.out.println();
}

Categories

Resources