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.
Related
Hi I asked this question before.But i didn't got a proper solution. I have a spinner which will load data from json.After selecting any item from spinner then it will post zeroth postition value again to another API.Everyting works fine.But my problem is I want defaultly no slection for spinner.I added a string named "no selection" to to zeroth position but it is not working.Please help me to implement this.
My spinner
void getList(){
final Common common = new Common();
int a= 100;
String webService = "API/Employee/GetList";
String postData = "";
String[] dataColumns = {"ID",//0
"Code",//1
"Name" ,//2
};
Runnable postThread = new Runnable() {
#Override
public void run() {
//Spinner
int a= 0;
ArrayList<String> Names = new ArrayList<String>();
for (int i=0;i<common.dataArrayList.size();i++){
String[] data=new String[3];
data[0]=common.dataArrayList.get(i)[0];
data[1]=common.dataArrayList.get(i)[1];
data[2]=common.dataArrayList.get(i)[2];
List.add(data);
}
for(int i=0;i<List.size();i++){
Names.add(List.get(i)[1]+" - "+List.get(i)[2]);
}
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(Insert.this, R.layout.item_spinner_black, Names);
dataAdapter.setDropDownViewResource(R.layout.item_spinner);
Spinner =(Spinner)findViewById(R.id.spinner);
//I added below code but it not working
// dataAdapter.insert(getString(R.string.selectemp), 0);
Spinner.setAdapter(dataAdapter);
}
My Post Data
postData="{\"Title\":\""+title.getText().toString()
+"\",\"Spinnervalue\":\""+List.get(Spinner.getSelectedItemPosition())[0]}
I think its help you.
ArrayList<String> Names = new ArrayList<String>();
Names.add("no selection");
for (int i=0;i<common.dataArrayList.size();i++){
String[] data=new String[3];
data[0]=common.dataArrayList.get(i)[0];
data[1]=common.dataArrayList.get(i)[1];
data[2]=common.dataArrayList.get(i)[2];
List.add(data);
}
for(int i=0;i<List.size();i++){
Names.add(List.get(i)[1]+" - "+List.get(i)[2]);
}
The problem is the initialization of your adapter. So when you add later the data, it will not have pre-selected item.
Edit:
I reordered your current code.
void getList(){
final Common common = new Common();
int a= 100;
String webService = "API/Employee/GetList";
String postData = "";
String[] dataColumns = {"ID",//0
"Code",//1
"Name" ,//2
};
Runnable postThread = new Runnable() {
#Override
public void run() {
//Spinner
int a= 0;
ArrayList<String> Names = new ArrayList<String>();
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(Insert.this, R.layout.item_spinner_black, Names);
dataAdapter.setDropDownViewResource(R.layout.item_spinner);
Spinner =(Spinner)findViewById(R.id.spinner);
//I added below code but it not working
// dataAdapter.insert(getString(R.string.selectemp), 0);
Spinner.setAdapter(dataAdapter);
for (int i=0;i<common.dataArrayList.size();i++){
String[] data=new String[3];
data[0]=common.dataArrayList.get(i)[0];
data[1]=common.dataArrayList.get(i)[1];
data[2]=common.dataArrayList.get(i)[2];
List.add(data);
}
for(int i=0;i<List.size();i++){
Names.add(List.get(i)[1]+" - "+List.get(i)[2]);
}
}
I added a dummy value.And It worked fine.
ArrayList<String> Names = new ArrayList<String>();
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(Insert.this, R.layout.item_spinner_black, Names);
dataAdapter.setDropDownViewResource(R.layout.item_spinner);
Spinner =(Spinner)findViewById(R.id.spinner);
String[] data = new String[3];
data[0]="00000000-0000-0000-0000-000000000000";
data[1]="Select";
data[2]="Name";
List.add(data);
for (int i=0;i<common.dataArrayList.size();i++){
String[] data=new String[3];
data[0]=common.dataArrayList.get(i)[0];
data[1]=common.dataArrayList.get(i)[1];
data[2]=common.dataArrayList.get(i)[2];
List.add(data);
}
for(int i=0;i<List.size();i++){
Names.add(List.get(i)[1]+" - "+List.get(i)[2]);
}
Spinner.setAdapter(dataAdapter);
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);
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);
Is it possible to build a for loop that goes to 100 to populate an Android spinner? As opposed to manually adding to the ArrayList.
Something like the below:
List<Integer> age = new ArrayList<>();
for (int i = 1; i < 101; i++) {
age.add(i);
}
It's perfectly fine to programmatically populate your ArrayList; you just need to use an ArrayAdapter like this:
List age = new ArrayList<Integer>();
for (int i = 1; i <= 100; i++) {
age.add(Integer.toString(i));
}
ArrayAdapter<Integer> spinnerArrayAdapter = new ArrayAdapter<Integer>(
this, android.R.layout.simple_spinner_item, age);
spinnerArrayAdapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
Spinner spinner = (Spinner)findViewById(R.id.spinner);
spinner.setAdapter(spinnerArrayAdapter);
Hope this helps!
yes of coures you can use...
Like this --
List age = new ArrayList<>();
for (int i = 1; i < 101; i++) {
age.add(i);
}]
now you have to use Array\adapter-
ArrayAdapter<Integer> sa= new ArrayAdapter<Integer>(
this, android.R.layout.simple_spinner_item, age);
sa.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item );
Spinner s= (Spinner)findViewById(R.id.spinner);
s.setAdapter(sa);
I have the listview from the exel:
try {
Workbook wb = WorkbookFactory.create(getAssets().open("bos.xls"));
Sheet sheet = wb.getSheetAt(0);
for(int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) {
Row row = sheet.getRow(i);
Cell form1 = row.getCell(0);
Cell form2 = row.getCell(1);
Cell form3 = row.getCell(2);
IrrList = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> hm;
hm = new HashMap<String, Object>();
hm.put(Form1, form1.getStringCellValue());
hm.put(Form2, form2.getStringCellValue());
hm.put(Form3, form3.getStringCellValue());
IrrList.add(hm);
SimpleAdapter adapter = new SimpleAdapter(this, IrrList,
R.layout.list_item, new String[] { Form1, Form2, Form3 },
new int[] { R.id.text1, R.id.text2, R.id.text3 });
listView.setAdapter(adapter);
listView.setTextFilterEnabled(true);
}
} catch(Exception ex) {
return;
}
When I use it I have the one value in my listview.
How can i correct it?
I tried to add this before "HashMap hm;":
for(int i1 = 0, l = IrrList.size(); i1 < l; i1++){
"I dont know what write here"
}
Try this..
Initilize the ArrayList before the for loop and set the listview adapter after the for loop
IrrList = new ArrayList<HashMap<String, Object>>();
for(int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) {
Row row = sheet.getRow(i);
Cell form1 = row.getCell(0);
Cell form2 = row.getCell(1);
Cell form3 = row.getCell(2);
HashMap<String, Object> hm;
hm = new HashMap<String, Object>();
hm.put(Form1, form1.getStringCellValue());
hm.put(Form2, form2.getStringCellValue());
hm.put(Form3, form3.getStringCellValue());
IrrList.add(hm);
}
SimpleAdapter adapter = new SimpleAdapter(this, IrrList,
R.layout.list_item, new String[] { Form1, Form2, Form3 },
new int[] { R.id.text1, R.id.text2, R.id.text3 });
listView.setAdapter(adapter);
listView.setTextFilterEnabled(true);