I have to get a part of my content provider query in a String[] format. Currently I use:
String[] selectionArgs = {"",""};
selectionArgs[0] = Integer.toString(routeScheduleID);
selectionArgs[1] = runDate.toString();
But in this case I have an unknown number of elements.
How can I change the number at runtime (or use something like an Array and convert back to String[]. Is this possible?
You can use List<String> to get your data and then get the array out of it:
List<String> lst = new ArrayList<String>();
lst.add(Integer.toString(routeScheduleID);
lst.add(runDate.toString());
lst.add(...);
...
String[] selectionArgs = lst.toArray(new String[lst.size()]);
You can use List for this. A List of Strings like this - List<String> str = new ArrayList<String>();
You can use a list to populate the data then convert the list into an array:
List<String> list = new ArrayList<String>();
list.add(item1);
list.add(item2);
...
String[] array = list.toArray(new String[0]);
List<String> selectionArgs = new ArrayList<String>();
selectionArgs.add(Integer.toString(routeScheduleID));
selectionArgs.add(runDate.toString());
selectionArgs.add(...).
...........
String[] array= selectionArgs.toArray(new String[selectionArgs.size()]);
List<String> selectionArgsList = new ArrayList<String>();
selectionArgsList.add("string1");
selectionArgsList.add("string2");
String[] selectionArgs = new String[selectionArgsList.length];
selectionArgsList.toArray(selectionArgs);
Related
I am attempting to populate a dropdown from the controller by passing the iteration value to a list. The iteration is done successfully but I keep getting a type mismatch error
List<Skills> mskills = skillsService.getAll();
for(Skills skills : mskills ){
String nameVal = skills.getName();
List<String> matchName = nameVal; //having an issue here
}
return matchName;
how can pass the value of nameVal to the matchName. Kindly assist
There are a few mistakes. matchName list is not visible out of the for loop. Further more, you can't assign a String to a List reference.
Replace your code with:
List<Skills> mskills = skillsService.getAll();
List<String> matchName = new ArrayList<String>();
for(Skills skills : mskills ){
String nameVal = skills.getName();
matchName.add(nameVal); //having an issue here
}
return matchName;
You're trying to assign a String object to a List<String> object. You need to create a list and then add elements to it.
List<Skills> matchName = new ArrayList<>();
List<Skills> mSkills = skillService.getAll();
for(Skill s : mSkills) {
String nameVal = skills.getName();
matchName.add(nameVal);
}
return matchName;
Try this:
List<String> matchName = new ArrayList<String>();
for(Skills skills : mskills ){
String nameVal = skills.getName();
matchName.add(nameVal); //having an issue here
}
return matchName;
I have something like this :
List<Page> result = new ArrayList<Page>();
Page is a class with 3 string variables;
I have an array as such :
List<String[]> output = new ArrayList<String[]>();
Which is populated like this in a loop:
String[] out = new String[3];
out[0] = "";
out[1] = "";
out[2] = "";
then added to output: output.set(i, out);
How can I assign output (type:String) to result (type:Page)?
I am guessing you are looking for something like this (code requires Java 8 but can be easily rewritten for earlier versions using loop)
List<String[]> output = new ArrayList<String[]>();
// populate output with arrays containing three elements
// which will be used used to initialize Page instances
//...
List<Page> result = output.stream()
.map(arr -> new Page(arr[0], arr[1], arr[2]))
.collect(Collectors.toList());
I should create this:
listview.setAdapter(new yourAdapter(this, new String[] { "data1","data2" }));
But I don't know how to create new String[]{...} dynamically.
I have this code and I should use the CitazioniOutput array:
String[] citazioni = getResources().getStringArray(R.array.citazioni);
List<String> CitazioniOutput = new ArrayList<String>();
for (String value : citazioni) {
CitazioniOutput.add(value);
}
I tried:
listview.setAdapter(new yourAdapter(this, CitazioniOutput));
//and
ArrayAdapter<String> adapter= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,CitazioniOutput);
listview.setAdapter(new yourAdapter(this, adapter));
But nothing changed: it says
The constructor is undefined
How can I convert the List<String> into a String[]?
You can use a method from the List class: List.toArray(Object[]);
List<String> list = new List<String>();
String[] strings = new String[list.size()];
list.toArray(strings);
Is there any way to convert a normal Java array or ArrayList to a Json Array in Android to pass the JSON object to a webservice?
If you want or need to work with a Java array then you can always use the java.util.Arrays utility classes' static asList() method to convert your array to a List.
Something along those lines should work.
String mStringArray[] = { "String1", "String2" };
JSONArray mJSONArray = new JSONArray(Arrays.asList(mStringArray));
Beware that code is written offhand so consider it pseudo-code.
ArrayList<String> list = new ArrayList<String>();
list.add("blah");
list.add("bleh");
JSONArray jsArray = new JSONArray(list);
This is only an example using a string arraylist
example key = "Name" value = "Xavier" and the value depends on number of array you pass in
try
{
JSONArray jArry=new JSONArray();
for (int i=0;i<3;i++)
{
JSONObject jObjd=new JSONObject();
jObjd.put("key", value);
jObjd.put("key", value);
jArry.put(jObjd);
}
Log.e("Test", jArry.toString());
}
catch(JSONException ex)
{
}
you need external library
json-lib-2.2.2-jdk15.jar
List mybeanList = new ArrayList();
mybeanList.add("S");
mybeanList.add("b");
JSONArray jsonA = JSONArray.fromObject(mybeanList);
System.out.println(jsonA);
Google Gson is the best library http://code.google.com/p/google-gson/
This is the correct syntax:
String arlist1 [] = { "value1`", "value2", "value3" };
JSONArray jsonArray1 = new JSONArray(arlist1);
For a simple java String Array you should try
String arr_str [] = { "value1`", "value2", "value3" };
JSONArray arr_strJson = new JSONArray(Arrays.asList(arr_str));
System.out.println(arr_strJson.toString());
If you have an Generic ArrayList of type String like ArrayList<String>. then you should try
ArrayList<String> obj_list = new ArrayList<>();
obj_list.add("value1");
obj_list.add("value2");
obj_list.add("value3");
JSONArray arr_strJson = new JSONArray(obj_list));
System.out.println(arr_strJson.toString());
My code to convert array to Json
Code
List<String>a = new ArrayList<String>();
a.add("so 1");
a.add("so 2");
a.add("so 3");
JSONArray jray = new JSONArray(a);
System.out.println(jray.toString());
output
["so 1","so 2","so 3"]
Convert ArrayList to JsonArray
: Like these [{"title":"value1"}, {"title":"value2"}]
Example below :
Model class having one param title and override toString method
class Model(
var title: String,
var id: Int = -1
){
override fun toString(): String {
return "{\"title\":\"$title\"}"
}
}
create List of model class and print toString
var list: ArrayList<Model>()
list.add("value1")
list.add("value2")
Log.d(TAG, list.toString())
and Here is your output
[{"title":"value1"}, {"title":"value2"}]
I'm using a simple php API (that I wrote) that returns a JSON formatted string such as:
[["Air Fortress","5639"],["Altered Beast","6091"],["American Gladiators","6024"],["Bases Loaded II: Second Season","5975"],["Battle Tank","5944"]]
I now have a String that contains the JSON formatted string but need to convert it into two String arrays, one for name and one for id. Are there any quick paths to accomplishing this?
You can use the org.json library to convert your json string to a JSONArray which you can then iterate over.
For example:
String jsonString = "[[\"Air Fortress\",\"5639\"],[\"Altered Beast\",\"6091\"],[\"American Gladiators\",\"6024\"],[\"Bases Loaded II: Second Season\",\"5975\"],[\"Battle Tank\",\"5944\"]]";
List<String> names = new ArrayList<String>();
List<String> ids = new ArrayList<String>();
JSONArray array = new JSONArray(jsonString);
for(int i = 0 ; i < array.length(); i++){
JSONArray subArray = (JSONArray)array.get(i);
String name = (String)subArray.get(0);
names.add(name);
String id = (String)subArray.get(1);
ids.add(id);
}
//to convert the lists to arrays
String[] nameArray = names.toArray(new String[0]);
String[] idArray = ids.toArray(new String[0]);
You can even use a regex to get the job done, although its much better to use a json library to parse json:
List<String> names = new ArrayList<String>();
List<String> ids = new ArrayList<String>();
Pattern p = Pattern.compile("\"(.*?)\",\"(.*?)\"") ;
Matcher m = p.matcher(s);
while(m.find()){
names.add(m.group(1));
ids.add(m.group(2));
}