My data always gets size '1' after set to Spinner Adapter. I used SearchableSpinner to show data.
final SearchableSpinner spinnerProvince = selectLocation8v.getSpinnerProvince();
final ArrayList<String> arrayProvince = new ArrayList<>();
final ArrayList<String> arrayKota = new ArrayList<>();
final ArrayList<String> arrayKecamatan = new ArrayList<>();
final ArrayList<State> listProvinsi = lisaDbHelper.getStates();
for (int p = 0; p < listProvinsi.size(); p++) {
arrayProvince.add(listProvinsi.get(p).getName());
}
ArrayAdapter provinceAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_spinner_item, arrayProvince);
provinceAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerProvince.setAdapter(provinceAdapter);
System.out.println("Data count before set to adapter : " + provinceAdapter.getCount()); // 34
System.out.println("Data count after set to adapter : " + spinnerProvince.getAdapter().getCount()); // 1
If you want to know how many items are bound to spinner, you should call:
spinnerProvince.getAdapter().getCount()
in your log you trying to get number of item in spinner by spinnerProvince.getCount() but this will not return actual value.
replace this code
System.out.println("Data count after set to adapter : " + spinnerProvince.getCount());
To
System.out.println("Data count after set to adapter : " + spinnerProvince.getAdapter().getCount());
i got the reference from here and i found that there no such method for returning spinner item count.
try to do that
Adapter adapter= (Adapterspinner )listProvinsi.getadapter();
int count = adapter.getCount();
Hope it will help you!!
Related
String sessionId = getIntent().getStringExtra("numbers");
final String[] values = new String[]{sessionId};
ArrayList list = new ArrayList<>(Arrays.asList(values));
final ArrayList arrayList = new ArrayList();
for (int i = 0; i < values.length; i++)
{
Log.d(TAG, "listValue -" + values[i]);
arrayList.add(values[i]);
}
listView = (ListView)findViewById(R.id.textView2);
listView.setAdapter(new
ArrayAdapter(ListDisplayActivity.this,R.layout.list_display,R.id.text, arrayList));
How to fix listview value single string have multiple values for android?
You can pass values like
intent.putStringArrayListExtra("sessionIds",yourSessionIdList);
and get like
ArrayList<String> sessionIds = getIntent().getStringArrayListExtra("sessionIds");
Check the below link: You may need to use custom adapter to display multiple values in each row of listview:
Listview with custom adapter
And I also recommend you to use recyclerview instead of listview
How to get value to dynamically add multiple spinner where spinner id is same.i used 'String spin = parent.getSelectedItem().toString();' but i get all time last spinner value.Plz help me?
you can create array list of spinner
ArrayList<Spinner> listSpinner = new ArrayList<>();
listSpinner.add(spinner1);
listSpinner.add(spinner2);
listSpinner.add(spinner3);
for(int i=0;i<listSpinner.size();i++){
String p1 = listSpinner.get(i).getSelectedItem();
}
get different value for all spinner out side for loop and store it in arraylist.
ArrayList<Spinner> listSpinner = new ArrayList<>();
ArrayList<ArrayList<String>> status_list = new ArrayList<>();
status_data.add("" + snapshot.getValue());// getting data
status_list.add(status_data);
for (int i = 0; i < 10; i++) {
View v = LayoutInflater.from(AssetCodingDetails.this).inflate(R.layout.custom_asset_coding_label, null);
Spinner spinner_status = (Spinner) v.findViewById(R.id.spinner_status);
ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(), R.layout.spinner_item, status_list.get(i));
spinner_status.setAdapter(adapter);
listSpinner.add(spinner_status);
}
for(int i=0;i<listSpinner.size();i++){
String p1 = listSpinner.get(i).getSelectedItem().toString();
}
I have a Listadapter wherein there are 4 different strings, and storing them to my listview. Now I want to get all the items from one of those strings and parse it to "date", so how can I able to populate my calendar dates from my listadapter?
Following the codes:
// Get User records from SQLite DB
ArrayList<HashMap<String, String>> eventsList = controller.getAllevents();
// If users exists in SQLite DB
if (eventsList.size() != 0) {
// Set the User Array list in ListView
ListAdapter adapter = new SimpleAdapter(CalendarActivity.this, eventsList, R.layout.calendar_event_list, new String[]{TAG_PID,
TAG_EVENTTITLE,TAG_EVENTSTART,TAG_EVENTEND},
new int[]{R.id.pid, R.id.eventname, R.id.eventstart, R.id.eventend});
ListView myList = (ListView) findViewById(android.R.id.list);
myList.setAdapter(adapter);
String eventstart = adapter.toString(); //I got null exception here..
Date edate = ParseDate(eventstart);
if (caldroidFragment != null) {
caldroidFragment.setBackgroundResourceForDate(R.color.Green, edate);
caldroidFragment.refreshView();
}
}
If you want to fetch an item from your adapter you can use
adapter.getItem(position);
which will return the item at the specified position. In your case, that method will return the HashMap<String, String> at the specified position:
Example:
/* adapter.getCount() returns the count of how many items
(HashMaps, in your case) that is represented in this adapter. */
for(int i = 0; i < adapter.getCount(); i++){
HashMap<String, String> myHashMap = adapter.getItem(i);
//"myKey" is the key that you provided, when mapping your key-values
String myVal = myHashMap.get("myKey");
Date edate = ParseDate(myVal);
/* Handle your Date object here. I'm just printing it to the console
in this example. */
System.out.println("Item at position " + i + " " + edate.toString());
}
Problem solved! Just simply put inside the loop and change the 'position' variable into the variable that handles the loop e.g for (int i = 0; i < eventsList.size(); i++) change adapter.getItem(position) to adapter.getItem(i)
I am having a problem updating a TextView in real time. I want to update the TextView of a ListView with a custom adapter in real time. I have my socket I/O handler on which I receive a JSON message. I want to parse this JSON and put that text into the particular list row with setText(). How do I get the index of that ListView row and update its TextView?
responseid=object.getString("ResponseID");
if(responseid!=null){
for(int j=0;j<countryList.size();j++){
//If the countryList row match then i want to update the textView of that particular row
if(countryList.get(j).getName().equals(caseid)) {
int oldcount = Integer.parseInt((dataAdapter.findViewById(R.id.replycount)).getText().toString());
int total=oldcount+1;
(dataAdapter.findViewById(R.id.replycount)).setText(Integer.toString(total));
dataAdapter.notifyDataSetChanged();
}
}
}
Here is my solution:
for(int j=0;j<countryList.size();j++)
{
if(countryList.get(j).getName().equals(caseid))
{
String oldcount = countryList.get(j).getCount();
int oldcountint = Integer.parseInt(oldcount);
int newcount = oldcountint + 1;
countryList.get(j).setCount(Integer.toString(newcount));
dataAdapter.notifyDataSetChanged();
break;
}
}
You should alter the items in your ListAdapter, and then call notifyDataSetChanged().
Example:
//Creating and adding ListAdapter to ListView
MyObject myFirstObject = new MyObject("Test1");
MyObject mySecondObject = new MyObject("Test2");
MyAdapter myAdapter = new MyAdapter();
myAdapter.add(myFirstObject);
myAdapter.add(mySecondObject);
myListView.setAdapter(myAdapter);
When updating a particular position:
myAdapter.getItem(position).text = "My updated text";
myAdapter.notifyDataSetChanged();
i have a array list for using it in spinner ,i have first value i spinner as title and i want to sort array list from second item in the spinner but i dont know how to do this i am using below trick but it sort whole array list including first item which is title so how to statr sorting from second item...
my code is below...
// this is my title ie. "provincia"
String select2= "Provincia";
if(!estado1.contains(select2)){
estado1.add(select2);
}
for (int i = 0; i < sitesList1.getEstado().size(); i++)
{
if(!estado1.contains(sitesList1.getEstado().get(i)))
{
estado1.add(sitesList1.getEstado().get(i));
Collections.sort(estado1);
}
use below code for show it in spinner...
final ArrayList<String> estado1 = MainMenu.barrio1;
final Spinner estado11 = (Spinner) findViewById(R.id.Spinner04);
ArrayAdapter<String> adapterbarrio = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, estado1)
estado11.setAdapter(adapterbarrio);
Why not remove the title / only add the title after the list has been sorted?
How about this
List<String> list = new ArrayList<String>();
// Fill list
String title = list.get(0);
list.remove(0);
Collections.sort(list);
list.add(0, title);
One way would be to split the arraylist like
estado1.subList(1,estado1.size()-1);
This would return a sublist excluding your title.
Use bubble sort! and start at index = 1!
final ArrayList<String> estado1;
for(int i=1; i<estado1.size() ; i++) {
for(int c=i; c<estado.size() ; c++) {
if(estado1.get(i).compareTo(estado1.get(c)))
{
String temp = estado1.get(i);
estado1.remove(i);
estado1.add(i, estado1.get(c));
estado1.remove(c);
estado1.add(c, temp);
}
}
}
PS: very bad performance