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);
Related
I want to send data from the list as input to execute a stored procedure. In code below, all variable list which contains to be sent as an input parameter.
public void onClick$btnSend() throws Exception {
Workbook workbook = new Workbook("D:/excel file/Mapping Prod Matriks _Group Sales Commercial.xlsx");
com.aspose.cells.Worksheet worksheet = workbook.getWorksheets().get(0);
com.aspose.cells.Cells cells = worksheet.getCells();
Range displayRange = cells.getMaxDisplayRange();
List<String> ParaObjGroup = new ArrayList<String>();
List<String> ParaObjCode = new ArrayList<String>();
List<String> ParaProdMatrixId = new ArrayList<String>();
List<String> ParaProdChannelId = new ArrayList<String>();
List<String> ParaProdSalesGroupId = new ArrayList<String>();
List<String> ParaCustGroup = new ArrayList<String>();
List<String> ParaSlsThroughId = new ArrayList<String>();
List<Integer> Active = new ArrayList<Integer>();
for(int row= displayRange.getFirstRow()+1;row<displayRange.getRowCount();row++){
ParaObjGroup.add(displayRange.get(row,1).getStringValue());
ParaObjCode.add(displayRange.get(row,3).getStringValue());
ParaProdMatrixId.add(displayRange.get(row,5).getStringValue());
ParaProdChannelId.add(displayRange.get(row,7).getStringValue());
ParaProdSalesGroupId.add(displayRange.get(row,9).getStringValue());
ParaCustGroup.add(displayRange.get(row,11).getStringValue());
ParaSlsThroughId.add(displayRange.get(row,13).getStringValue());
Active.add(displayRange.get(row,14).getIntValue());
}
System.out.println(ParaObjGroup);
System.out.println(ParaObjCode);
System.out.println(ParaProdMatrixId);
System.out.println(ParaProdChannelId);
System.out.println(ParaProdSalesGroupId);
System.out.println(ParaCustGroup);
System.out.println(ParaSlsThroughId);
System.out.println(Active);
lovService.coba(ParaObjGroup,ParaObjCode,ParaProdMatrixId,ParaProdChannelId,ParaProdSalesGroupId,ParaCustGroup,ParaSlsThroughId,Active);
}
and below code for execute stored procedure
#Transactional(propagation = Propagation.REQUIRED, rollbackFor = {SQLException.class, Exception.class })
public void executeSPForInsertData(DataSource ds,String procedureName,Map<String[], Object[]> inputParameter){
SimpleJdbcCall jdbcCall = new SimpleJdbcCall(paramsDataSourceBean).withProcedureName(procedureName);
jdbcCall.execute(inputParameter);
}
But I have a problem cannot set the list type as a parameter in method put:
#ServiceLog(schema = ConstantaVariable.DBDefinition_Var.PARAMS_DB_SCHEMA, sp = ConstantaVariable.PARAMSProcedure_VAR.PR_SP_FAHMI)
#Transactional(propagation=Propagation.REQUIRED, rollbackFor={Exception.class,SQLException.class})
public void coba(List<String> params1,List<String> params2,List<String> params3,List<String> params4,List<String> params5,
List<String> params6,List<String> params7,List<Integer> params8){
Map<String[], Object[]> mapInputParameter = new LinkedHashMap<String[], Object[]>();
mapInputParameter.put("P_OBJT_GROUP", params1);
mapInputParameter.put("P_CODE", params2);
mapInputParameter.put("P_PROD_MATRIX_ID", params3);
mapInputParameter.put("P_PROD_CHANNEL_ID", params4);
mapInputParameter.put("P_PROD_SALES_GROUP_ID", params5);
mapInputParameter.put("P_CUST_GROUP", params6);
mapInputParameter.put("P_SLS_THROUGH_ID", params7);
mapInputParameter.put("P_ACTIVE", params8);
ParamsService.getService().executeSPForInsertData(null,ConstantaVariable.PARAMSProcedure_VAR.PR_SP_FAHMI,mapInputParameter);
}
The type Map<String[], Object[]> is not compatible with what you try to put in: The key is String and the value is List<String>.
There are two solutions:
Change the map to be compatible with the inserted parameters.
Map<String, List<String>> mapInputParameter = new LinkedHashMap<>();
If you need to use the original map type, then you have to change the way you put the parameters into the map.
Map<String[], Object[]> mapInputParameter = new LinkedHashMap<>();
mapInputParameter.put(new String[] { "P_OBJT_GROUP" }, new Object[] { params1 });
mapInputParameter.put(new String[] { "P_CODE" }, new Object[] { params2 });
The drawback is that in further processing you have to check if the array is not empty and cast explicitly from Object to List<String>.
If you want something "more universal and more generic", I'd go for Map<String, List<Object>>. In any way, I find no reason to use an array in the map unless it is explicitly required (I have no information about the executeSPForInsertData method.
I have two arrays
Android
PriceWeight
where Android is whole view and PriceWeight I want to show in spinner (Weight or Price).
I have done fetching it just problem is fetching data in spinner.
JSON Link
http://navsarimarket.com/API/getProductPriceWeight.php?Category=77
you can iterate over your JSON and make a string list something like below
JSONObject obj = new JSONObject(jsonString);
JSONArray androidArray = obj.getJSONArray("Android");
for(int count=0; count<androidArray.length(); count++){
JSONObject inner = androidArray.get(count);
JSONArray weight = inner.getJSONArray("PriceWeight");
List<String> spinnerArrayList = new ArrayList<String>();
for(int i=0; i<weight.length(); i++){
JSONObject temp = weight.get(i);
String value = temp.getString("Weight");
value = value + " - " + temp.getString("Price");
//pass this to your recyclerview model for showing spinner per row.
spinnerArrayList.add(value);
}
}
Inside your onBindViewHolder add below code where list is list of data you get from JSON response which will display in your spinner.
List<String> list = new ArrayList<String>();
list.add("TEST");
ArrayAdapter<String> dataAdapter =new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
holder.siteSpinner = (Spinner) findViewById(R.id.siteSpinner);
holder.siteSpinner.setAdapter(dataAdapter);
}
I have a code populating a listView:
JSONArray data = responseData.getJSONArray("data");
String[] values = new String[data.length()];//I wanna get rid of this
LinkedHashMap<String, String> helpData = new LinkedHashMap();
for (int i = 0; i < data.length() ; i++) {
String header = data.getJSONObject(i).getString("glossary_header");
String description = data.getJSONObject(i).getString("gloassary_description");
helpData.put(header, description);
values[i] = header;
Log.d("mylog", "counter" + i);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
I want to pass the keys to Arrayadapter, I was hoping to find a getKeys() method that could magically return an array of key from the map.
KeySet() was close but did not work, what is the proper way to do this. I don't want to use string array. I want to have my pair values together.
You can get like this
Collection<String> values = helpData.keySet();
for (String string : values) {
//
}
Set<String> keys = myArray.keySet();
String[] keysAsArray = keys.toArray(new String[0]);
More detail on the toArray method can be found at http://docs.oracle.com/javase/7/docs/api/java/util/Set.html#toArray(T[])
for (final String key : helpData.keySet()) {
// print data...
}
or
final Iterator<String> cursor = helpData.keySet().iterator();
while (cursor.hasNext()) {
final String key = cursor.next();
// Print data
}
My Code:
// Reading all contacts from database
List<BNICorporateBean> contacts = db.getAllInfo();
// Each row in the list stores country name, currency and email
ArrayList<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
for (BNICorporateBean cn : contacts)
{
if(!memId.trim().equalsIgnoreCase(cn.getBNIMemID().trim()))
{
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("name", cn.getMemName());
hm.put("email", cn.getMemEmail() );
hm.put("mem_id", cn.getBNIMemID());
Log.d("Result", cn._MemName+"\n"+cn._MemEmail+"\n"+cn._BNIMemID);
aList.add(hm);
}
memId= cn.getBNIMemID();
//infoArry.add(cn);
}
// Keys used in Hashmap
//String[] from = { "name","email"};
// Ids of views in listview_layout
//int[] to = { R.id.mem_name,R.id.mem_email};
// Instantiating an adapter to store each items
// R.layout.listview_layout defines the layout of each item
SimpleAdapter adapter = new SimpleAdapter(LoginActivity.this, aList, R.layout.list_member, new String[] { "name","email"},
new int[]{ R.id.mem_name,R.id.mem_email});
/** Setting the adapter to the listView */
autoComplete.setAdapter(adapter);
Takes value from database and my log text show that it returns single value, but don't know why it shows same value two times in list suggestion . Note: for some values it shows once.
I have also print my list and gives perfect result no duplicate values
for(int k=0;k<aList.size();k++)
{
System.out.println(""+aList.get(k));
}
Use it.This may help you
do{
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("name", cn.getMemName());
hm.put("email", cn.getMemEmail() );
hm.put("mem_id", cn.getBNIMemID());
Log.d("Result", cn._MemName+"\n"+cn._MemEmail+" \n"+cn._BNIMemID);
aList.add(hm);
memId= cn.getBNIMemID();
}while(!memId.trim().equalsIgnoreCase(cn.getBNIMemID().trim()));
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);