Im having problems with searching through a list array trying to find certain string and then storing that string into a variable called name
private List<String> listview_array1 = new ArrayList<String>();
private List<String> listview_array2 = new ArrayList<String>();
private List<String> listview_array3 = new ArrayList<String>();
String name1 = null;
if(listview_array1.contains("The")) {
//assign value to name1
System.out.println(name1);
}
Documentation is your friend. In general the first line of any question of how do I use a class is look up it's docs.
http://developer.android.com/reference/java/util/ArrayList.html
As to your question you will want the indexof method. Details on the page above.
Related
How can i put a list of strings, type string
in
String countryList[] = {a};
a is the variable that have the list
so, it will be
String countryList[] = {"heyyesGabon","heyyesGibraltar","heyyesGuinea"}; //and the others
this list comes from console.log in javascript
it always save the last item
From List to String array
String [] countryArray = countryList.toArray(new String[0]);
From String array to List
List<String> list = Arrays.asList(array);
You can use in-build function list.toArray(stringArray); it will return String[].
String countryList[] = new String[list.size()];
countryList = list.toArray(countryList);
Hope this will help you.
I am learning ArrayList in Android and i have an error for my scenario:
ArrayList<String> list=new ArrayList<>();
list.add("SuperMan");
list.add("BatMan");
list.add("Xman");
list.add("WhatEverMan");
TextView omg=(TextView)findViewById(R.id.textView);
for(String item:list){
omg.setText("You are "+item+" ");
}
I try to change to this also not working
omg.setText("You are "+item.toString()+" ");
It only display "WhatEverMan" in the TextView.
And another question is that what's the difference between
ArrayList<String> list=new ArrayList<>();
and
ArrayList<String> list=new ArrayList<String>();
I run these 2, both didn't prompt any errors, just that it only display "WhatEverMan"
You should use this:
TextView omg=(TextView)findViewById(R.id.textView);
for(String item:list){
omg.setText(omg.getText().toString()+item);
}
You can do like this:
String text = null;
for(String item:list){
text += (" " + item);
}
omg.setText(text);
ArrayList<String>() and ArrayList<>() is the same in your situation
setText() sets the final text in ArrayList while in loop. Better make String variable and append on them. then set the text.
ArrayList<String> list=new ArrayList<>();
list.add("SuperMan");
list.add("BatMan");
list.add("Xman");
list.add("WhatEverMan");
TextView omg=(TextView)findViewById(R.id.textview);
String s = "";
for(String item:list){
s+=item+"\n";
}
omg.setText(s);``
}
You will get last value, because every-time value will rewrite into TextView
so you can use StringBuilder or you have to append text.
example
omg.append(item);
We have an Array List consisting of many strings.
public final static ArrayList<String> MyListArrayList= new ArrayList<String>(){{}};
And we converted the array list into a comma separated string using below code
gson = new Gson();
listOfTestObject = new TypeToken<ArrayList<String>>(){}.getType();
MyListString = gson.toJson(Images.MyListArrayList, listOfTestObject);
Now we have a comma separated string like
"Item1,Item2,Item3,Item3.....Itemn"
and we are saving this string using the preference whenever the array list gets updated. So now we need to load this string at starting and then convert the string back into the array List.For that we are trying below code
List<String> Images.MyListArrayList= new ArrayList<String>(Arrays.asList(s.split(",")));
But its not possible to do it.But we can create a new List.. But we want to update the already existing Array List instead of creating new one. How to do it?
String value="Item1,Item2,Item3,Item3.....Itemn";
String[] arrayList = value.split(",");
ArrayList list = new ArrayList<String>();
for (int i = 0; i < arrayList.length; i++) {
list .add(arrayList[i]);
}
how to pass ArrayList index 0 value as parameter to another screen help me please
int counter=Category_name.size();
if(counter==1)
{
static ArrayList<String> Category_name = new ArrayList<String>();
static ArrayList<String> Menu_ID = new ArrayList<String>();
Intent iMenuList = new Intent(MenuGroup.this, thirdstep.class);
String s = Menu_ID[index0];
String t = Category_name[index0];
iMenuList.putExtra("Menu_ID",s);
iMenuList.putExtra("menu_group", t);
startActivity(iMenuList);
}
String s = Menu_ID.get(0);
String t = Category_name.get(0);
EDIT
You cant add a single value on any index but 0, here is a simple example
ArrayList<String> Category_name = new ArrayList<String>();
Category_name.add(5, "string");
console
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 5, Size: 0
at java.util.ArrayList.rangeCheckForAdd(Unknown Source)
at java.util.ArrayList.add(Unknown Source)
I believe that some null values are getting added in the list as you are reading from the JSON and you want to take the first non null values.
Solutions:
1. When you parse the JSON add only the non null values to the list and then use list.get(0).
Or
2. Once the list is formed, iterate through the list. Check for null condition, pick the first non null value and put in your string and break the loop.
I am adding a value to my arraylist but it is showing error. Before adding the value I am displaying it first. The value is displaying but it is not adding to the arraylist. Please help me regarding this...
My Code:
static ArrayList<String> allfirstids;
ArrayList<String> list = List.get(i);
UserBO user = new UserBO();
user.firstid = Integer.parseInt(list.get(0));
user.secondid = Integer.parseInt(list.get(1));
System.out.print("Hello this is first id");
System.out.print(list.get(0));
allfirstids.add(list.get(0));
System.out.println("first ids"+allfirstids);
Thanks in advance...
before adding, please initialize arraylist.
allfirstids = new ArrayList<String>();
You forgot to initialize your static ArrayList allfirstids, Just initialize it, before using..
static ArrayList<String> allfirstids = new ArrayList<String>();
ArrayList<String> list = List.get(i);
UserBO user = new UserBO();
user.firstid = Integer.parseInt(list.get(0));
user.secondid = Integer.parseInt(list.get(1));
System.out.print("Hello this is first id");
System.out.print(list.get(0));
allfirstids.add(list.get(0));
System.out.println("first ids"+allfirstids);