how to refresh Material Spinner in activity? - java

Using API through I was adding data in spinner that are added but that are not displayed in spinner at a time, so how can I refresh activity and newly added data to show at a time? i can used this link ....https://github.com/jaredrummler/MaterialSpinner/blob/master/README.md
(Schoolschool:userDetails.getSchools()) {
schoolList.add(school.getSchoolName());}
schoolDropDown.setItems(schoolList);
schoolDropDown.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener<String>() {
#Override
public void onItemSelected(MaterialSpinner view, int position, long id, String item) {
}
});

Use this method
adapter.notifyDataSetChanged();

You need to define the Adapter using setAdapter(...), for the Spinner, too. This defines the layout to be used for each individual item. i.e.
schoolDropDown.setAdapter(new ArrayAdapter(getContext(), android.R.layout.simple_dropdown_item_1line, schoolList));

You only need to call setAdapter() once and you call adapter.notifyDataSetChanged() to update the data.

You need to set adapter to the spinner for displaying data
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, schoolList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
schoolDropDown.setAdapter(dataAdapter);
After data change you need to call
dataAdapter.notifyDataSetChanged();

Try like this:
cityAdapter = new ArrayAdapter<>(frgmActivity, android.R.layout.simple_spinner_dropdown_item, listCityNames);
cityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spCitys.setAdapter(cityAdapter);
Or
Try below two lines :
String[] schoolsArr = schoolList.toArray(new String[schoolList.size()]);
schoolDropDown.setItems(schoolsArr);
Instead of this line: schoolDropDown.setItems(schoolList);

Related

how to get the items changed in spinner android studio

I want to change the value of the second spinner when the value of the first spinner is changed I tried the solutions but did not get an exact solution so anyone have a solution for that so please suggest or any question then please ask
ArrayAdapter<String> deptArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.Province));
deptArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerProvinceEyeTest.setAdapter(deptArrayAdapter);
deptArrayAdapter.notifyDataSetChanged();
Set a OnItemSelectedListener() first.
spinnerProvinceEyeTest.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
String selectedItem = parent.getItemAtPosition(position).toString(); //this is your selected item
}
public void onNothingSelected(AdapterView<?> parent)
{
}
});
In onItemSelected() of above Listener, place your condition and set resource for second Spinner as:
If(selectedItem == "YourText"){
//Here, you're initalizing the second `Spinner`
ArrayAdapter<String> secondArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.YourList));
secondArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
secondSpinner.setAdapter(secondArrayAdapter);
}
else{
//Use any other list with arrayAdapter in case of other item selected.
//Use can use If-else-if or Switch to select different resource for different item of first spinner
}
Reference - Official Documentations.

Issue with items of listview

i don't know why but when i add items to listview i get only the last item. E.G. if i write apple and than pear i get only pear and not apple and pear.
Why?
CODE:
holder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("IDDD:"+iddd);
System.out.println("IDDD:"+video2.getPic());
//holder.lw.setA
String commento= (holder.tw.getText().toString());
// holder.lw.setAdapter(adapter);
ArrayList<String> arrayList= new ArrayList<String>();
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, arrayList);
arrayList.add(commento);
// next thing you have to do is check if your adapter has changed
holder.lw.setAdapter(adapter);
adapter.notifyDataSetChanged();
System.out.println("LISTVIEW:"+arrayList);
}
});
in every click you decalre new list ArrayList<String> arrayList= new ArrayList<String>(); and it's false
you need to declate it out of the listener and in your listener just add the new items and use adapter.notifyDataSetChanged();
You are creating new list and adapter during every click so move the declaration and initialization of both, outside that function
Declare them outside getView(if Base or ArrayAdaoter) or createViewHolder (RecyclerAdapter)
initialize them inside constructor
add data to list and notify adapter
Better use view holder pattern in here where you use Existing instance of listview rather than recreating the new one all the time.
You don't need to do anything special, inside of onClick you need to get a reference to your ArrayAdapter and call the method add(T...) on it.
holder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("IDDD:"+iddd);
System.out.println("IDDD:"+video2.getPic());
String commento= (holder.tw.getText().toString());
ArrayAdapter adapter = (ArrayAdapter) holder.lw.getAdapter();
adapter.add(commento);
}
});
No need to even call notifyDataSetChanged because add already does that internally.
if you use this you can solve your problem
yourAdapter.(getCount()-1-position);
and this if you want to see the last items in front
public yourPoJo getItem(int position) {
return super.getItem(getCount()-1-position);
}

How can I fill the first line in a list view before filling it with my custom adapter?

I want to fill the first listview line with an int from another method in my databasehelper, and the fill the rest of the view.
All I can find googleing this is how to do it with an ArrayAdapter
this is how i fill the view now:
new Handler().post(new Runnable(){
#Override
public void run(){
cursorAdapter = new CustomCursorAdapter(MainActivity.this, contactDBHelper.listAll());
listView.setAdapter(cursorAdapter);
}
});
what I want to do is to fill the first line with the coming int from this method.
contactDBHelper.countContact();
Any ideas?
try using ListVew.addHeaderView(View view)
ListView lv=(ListView) findViewById(R.id.listView1);
TextView header=new TextView(getApplicationContext());
header.setText("Header");
lv.addHeaderView(header);
You can override getItemViewType and getViewTypeCount methods if you want to display different views at the ListView. Here is a short example.

Android: Dealing with CheckBox in a ListView

I'm using a SimpleAdapter to fill my ListView with below code.
SimpleAdapter adapter = new SimpleAdapter(this,
saleDriver.getOutstandings(clientId),
R.layout.outstanding_list_row, new String[] { "sale_id",
"sale_date", "invoice_number", "sale_total", },
new int[] { R.id.tt_check_box, R.id.tt_invoice_date,
R.id.tt_invoice_no, R.id.tt_invoice_tot });
setListAdapter(adapter);
According to above code, i have bind sale_id with CheckBox (R.id.tt_check_box) in the listview. When i run the program, value of checkboxes displayed right of the CheckBox as text. but i don't want to display them.
My actual need is, when user checked checkboxes, i need to get sale_ids bind with them.
How could i access sale_ids bind with checked checkboxes in my java programe ?
use
android.R.layout.simple_list_item_multiple_choice
I've attached code to obtain listview that works with multichoice checkboxes..
public class MultiChoiceActivity extends Activity {
String[] choice = { "Choice A",
"Choice B", "Choice C", "Choice D", "Choice E"};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
choiceList = (ListView)findViewById(R.id.list);
ArrayAdapter<String> adapter
= new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice,
choice);
choiceList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
choiceList.setAdapter(adapter);
}
Refernce Link
Override the getView() function in the SimpleAdapter and use the value of sale_id to check/uncheck the checkbox. Then use this custom adapter for your list.
EDIT:
In looking at another answer, my refined guess is that you need the Multiple Choice solution rather than this (since you need to find out the selections). This solution is more to display the checkbox based on existing data. If you still need this, let me know and I will post the sample code once I have access to my code.

I need help with my first Spinner!! Not Displaying values

This is the first time I am trying to use a spinner and I need some help.. I made a layout with a spinner object on it and then I also made a array.xml with the numbers that I would like in the spinner in it. I run the following code and the screen displays without any values in my spinner??
public class SpinnerExaple extends Activity {
private Spinner numbersSpinner;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.numbersSpinner = (Spinner) findViewById(R.id.Spinner01);
ArrayAdapter<String> numbersArray =
new ArrayAdapter<String>(this, R.layout.main,
getResources().
getStringArray(R.array.numbers));
}
}
You got the reference to the spinner, you got your AdapterArray set correctly- but you didn't attach the adapter to your spinner.
Add the line:
numbersSpinner.setAdapter( numbersArray );
You never set your numbers array into the spinner. Try adding the following at the end of the method:
numbersSpinner.setAdapter(numbersArray);
Also check out the hello-spinner tutorial.

Categories

Resources