How to get data from ArrayList to RecyclerView?
ModelCoba modelCoba = response.body();
for (int i = 0; i < modelCoba.getAcara_daftar().size(); i++){
judul[i] = response.body().getAcara_daftar().get(i).getJudul();
pemateri[i] = response.body().getAcara_daftar().get(i).getPemateri();
tanggal[i] = response.body().getAcara_daftar().get(i).getTgl();
}
ArrayAdapter arrayAdapter = new ArrayAdapter<String>(AcaraAdapter.this, android.R.layout.activity_list_item, );
Try custom adapter here
Try above given example and in your case adapter could be used as below
MyRecyclerViewAdapter adapter = new MyRecyclerViewAdapter(MainActivity.this, modelcoba);
mRecyclerView.setAdapter (adapter);
Related
I have a REST method that returns result as "Res:"2". Now i need to populate a spinner with values 1,2. If the REST method returns the result as "Res:"3", the spinner values must be 1,2,3.
This is the code that i have implemented. But the spinner shows only 2 for the
Integer[] items = new Integer[]{Integer.valueOf(user2.getString("Res"))};
ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(MainActivity.this, android.R.layout.simple_spinner_item, items);
cboFloorLevel.setAdapter(adapter);
Note: user2 is a JSONObject
try this
List<Integer> numbers = new ArrayList<>();
int item = Integer.valueOf(user2.getString("Res"));
for(int i=1; i<item+1; i++){
numbers.add(i)
}
ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(MainActivity.this,
android.R.layout.simple_spinner_item, numbers);
cboFloorLevel.setAdapter(adapter);
Try this code
int end_value = Integer.valueOf(user2.getString("Res"));
Integer[] items = new Integer[]{end_value};
for(int i=0;i<end_value;i++){
items[i] = i+1;
}
ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(MainActivity.this, android.R.layout.simple_spinner_item, items);
cboFloorLevel.setAdapter(adapter);
I understand from the question that if the json response is
{"Res:"3"} you need to populate the spinner with 1,2,3.
int item = Integer.valueOf(user2.getString("Res"));
ArrayList <Integer>items = new ArrayList<>();
for(int j=1; j < item+1 ; j++){
items.add(j);
}
ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(MainActivity.this,
android.R.layout.simple_spinner_item, items);
cboFloorLevel.setAdapter(adapter);
Found my solution:
You need to import Arrays from java8:
import java.util.Arrays;
and then you can convert string to int, create array and fill that array with lambda passed to Arrays.setAll():
int maxValue = Integer.parseInt(user2.getString("Res"));
int[] items = new int[maxValue];
Arrays.setAll(array, i -> i + 1);
ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(MainActivity.this, android.R.layout.simple_spinner_item, items);
cboFloorLevel.setAdapter(adapter);
I need to extract part of the string and display it in the spinner
I need that when the spinner display data example
For array entries like the following
"Equipo-001"
"Equipo-002"
Should show only:
"001"
"002"
Here's my code
private void rellenarSpinnerConFoliosDeMaquinasDelPunto(List<String> folios) {
maquinas = dbOn.getMaquinasDePunto(idPunto);
for (int i = 0; i < maquinas.size(); i++) {
foliosDeMaquinas.add(maquinas.get(i).getcFolioMaquina());
}
adaptadorFoliosMaquina = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, folios);
adaptadorFoliosMaquina.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spn_folioMaquina.setAdapter(adaptadorFoliosMaquina);
spn_folioMaquina.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
ArrayList<String> numberFolios = new ArrayList<>();
for(int j =0; j < folios.size(); j++){
numberFolios.add(folios.get(j).substring(8, 10));
}
adaptadorFoliosMaquina = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, numberFolios);
Check link for better reference on how to use substring.
You can use SPLIT function
ArrayList<String> data = new ArrayList();
foreach(String get:folios){
data.add(folios.split("-")[1]);
}
adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, data);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
If your result will be dynamic(The string length would vary in future) the below solution may work,
private void rellenarSpinnerConFoliosDeMaquinasDelPunto(List<String> folios) {
try{
maquinas = dbOn.getMaquinasDePunto(idPunto);
for (int i = 0; i < maquinas.size(); i++) {
foliosDeMaquinas.add(maquinas.get(i).getcFolioMaquina().toString().split("-")[1]);
}
} catch(ArrayIndexOutOfBoundsException e){
e.printStackTrace();
}
adaptadorFoliosMaquina = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, folios);
adaptadorFoliosMaquina.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spn_folioMaquina.setAdapter(adaptadorFoliosMaquina);
spn_folioMaquina.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
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.
EQName list calling from the service
JSONArray EQResponseArray = new JSONArray(EQSpecifCompanrison);
int EQResponseSize = EQResponseArray.length();
String[] EQName = new String[EQResponseSize];
for(int i = 0; i< EQResponseSize; i++)
{
JSONObject EQResponseObject = EQResponseArray.optJSONObject(i);
int EQ_ID = EQResponseObject.optInt("EquipmentStatusId");
EQID[i] = EQ_ID;
String EQ_Name = EQResponseObject.optString("EquipmentStatusDescription");
EQName[i] = EQ_Name;
int EQ_PlantID = EQResponseObject.optInt("equipmentParentStatusId");
EQPlantID[i] = EQ_PlantID;
}
I have problem in AddALL section. I think problem in to convert how it will be? Planet constructer takes list items but my list have array list.
ArrayList<Planet> planetList = new ArrayList<Planet>();
Object[] mStringArray = planetList.toArray();
planetList.addAll(EQName);
listAdapter = new PlanetArrayAdapter(getActivity(), planetList);
SpecificList.setAdapter( listAdapter );
i want to add data to listview but my data are as String[] and arraylist is Planet[]
and when my code is like this i add only one item.
String[] zapasyList3 = ligy.split("<br/>");
for (int i=0; i<5; i++){
planets = new Planet[] { new Planet (zapasyList3[i])
};
}
ArrayList<Planet> planetList = new ArrayList<Planet>();
planetList.addAll( Arrays.asList(planets) );
// Set our custom array adapter as the ListView's adapter.
listAdapter = new PlanetArrayAdapter(getBaseContext (), planetList);
mainListView.setAdapter( listAdapter );
listAdapter.notifyDataSetChanged();
Thanks
How about:
planets = new Planet[5];
for (int i=0; i<5; i++){
planets[i] = new Planet (zapasyList3[i]);
}
//.. add the planets array now
You can also declare planets as ArrayList instead or regular array.. up to you.