constructor cannot resolve Array adapter in android - java

I don't know what error I did. But it shows me can not resolve constructor Arrayadapter on my array adapter code.Help me to fix it out.( I have used getActivity() because I have written this code on my fragment section) and it gets crashes after it.I have tried almost all issue fixing methods in stack overflow .(Before down voting suggest me some solutions )
spinner = (Spinner) view.findViewById(R.id.spinner3);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,getActivity(),android.R.layout.simple_spinner_item,def);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
here is my error log

Try this, if you use a fragment:
1. This code is designed to fetch a static array
ArrayAdapter myAdapter = ArrayAdapter.createFromResource(
getActivity(), R.array.my_array, android.R.layout.simple_spinner_item);
myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mySpinner.setAdapter(myAdapter);
2. If you are fetching a dynamic array, then the below sample code would work.
In this sample, I've fetched the list from db. You can change upon your requirement.
Spinner maritalSpinner = (Spinner) view.findViewById(R.id.marital_spinner);
List<MaritalStatus> maritalStatusList = new ArrayList<>();
List<String> maritalStatusArray = new ArrayList<>();
maritalStatusList = dbConnection.getMaritalStatus(maritalStatusDao);
maritalStatusArray.clear();
maritalIdArray.clear();
maritalStatusArray.add("~Select~");
maritalIdArray.add(0l); // this is number '0' and alphabet 'l'(small 'L') not number '1'
for (MaritalStatus marital : maritalStatusList) {
String maritalStatus = marital.getMaritalStat();
long maritalId = marital.getMaritalId();
maritalStatusArray.add(maritalStatus);
maritalIdArray.add(maritalId);
}
maritalAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_spinner_item, maritalStatusArray);
maritalAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
maritalAdapter.notifyDataSetChanged();
maritalSpinner.setAdapter(maritalAdapter);

your are passing to Context to your adapter so just pass one context
if your spinner in fragment than just change your adapter like this
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item,def);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);

Related

Cannot fill ListView with Items in Android

I am trying to fill a ListView with Items. Everything seems to work perfectly but ListView doesn't show Items. I am wondering if I miss a step here.
Please take a look at my code below;
ListView listView = (ListView) myView.findViewById(R.id.listViewLocations);
List<WMS_Location> list = (List<WMS_Location>) response.body();
String listViewItems[] = new String[list.size()];
for (int i=0;i<list.size();i++)
{
listViewItems[i]= list.get(i).getCode().toString().toUpperCase();
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, listViewItems);
listView.setAdapter(adapter);
NOTE: I am sure that listViewItems is not empty and it has 5 string object in it.

how to refresh Material Spinner in activity?

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);

Error comming on maketext in toast

public void onClick(View v) {
{
if (db==null) db = new DB(AddStation.this);
if(v.getId()==R.id.ADD ) {
String code = Scode.getText().toString().trim();
String name = SName.getText().toString().trim();
String fac = SFac.getText().toString().trim();
if(name.equals("")){
Scode.setError("Invalid name");
return;
}
if (code.equals("")){ Scode.setError("Invalid email");
return;
}
if (db.addStudent(code, name, fac))
Toast.makeText(AddStation.this, "Student added", Toast.LENGTH_SHORT).show();
else if (v.getId()==R.id.See) {
Toast.makeText(AddStation.this, db.getAllStudents(), Toast.LENGTH_LONG).show();
//Log.v("EditText", db.getAllStudents().toString());
}
}
}
db.close();
Toast.makeText(AddStation.this, db.getAllStudents(), Toast.LENGTH_SHORT).show();
}
}
AddStation is my Fragment name. how to solve this ?
this toast is retriving database and can u guys tell me how to bring data into a dropdown box or gridview instead of a toast. thanks!
Look at the last two lines. First you called db.close();, then called db.getAllStudents(), so ARE YOU KIDDING?
But if not this caused error, then post you logcat trace
PS:check your AddStation, is it extended from Activity or its descendants? If not, extend your AddStation from Activity or its descendants.
You can display this data in List/Spinner, for this you had to add these widgets in your layout. You can populate this data as
1. ListView
ListView listView = (ListView) findViewById(R.id.mylist);
List<String> listRecords = db.getAllStudents();
ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item, listRecords);
listView.setAdapter(arrayAdapter);
2. Spinner
Spinner spinner = (Spinner) findViewById(R.id.spinner);
List<String> listRecords = db.getAllStudents();
ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, listRecords);
spinner.setAdapter(arrayAdapter);

Using 1 spinner to dynamically change 2 spinners with if functions and notifyDataSetChanged?

Pretty new to Java in general. I have 3 Spinners in my code, and my 2 spinners would display lists depending on 1 main spinner ( which has 2 selections). After reading a few threads I read about refreshing the lists using notifySetDataChanged(); but the spinner lists never changed. Few questions :
Am I using the notifySetDataChanged correctly?
Is there another way to populate the lists?
Is the IF function the appropriate method?
Here's the code upto the onCreate method.
public class MainActivity extends Activity {
private Spinner spinner1, spinner2, spinner3, spinner4;
private Button convertButton;
private EditText from;
private List <String> list1 = new ArrayList<String>();
private List <String> list2 = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
from = (EditText) findViewById(R.id.amount);
//spinners for units
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
spinner4 = (Spinner) findViewById(R.id.spinner4_main);
List<String>list4 = new ArrayList<String>();
list4.add("Distance");
list4.add("Weight");
//adapter for main scale
ArrayAdapter<String> dataAdapter4 = new ArrayAdapter <String> (this,
android.R.layout.simple_spinner_dropdown_item, list4);
dataAdapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner4.setAdapter(dataAdapter4);
//adapter for "from" currency
ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list1);
dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(dataAdapter1);
//adapter for "to" currency
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list2);
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(dataAdapter2);
Object choice = spinner4.getSelectedItemPosition();
if (choice.equals("Weight")) {
//units to convert from
dataAdapter1.clear();
dataAdapter1.add("Milligrams");
dataAdapter1.add("Grams");
dataAdapter1.add("Kilograms");
dataAdapter1.add("Metric Ton");
//units to convert to
dataAdapter2.clear();
dataAdapter2.add("Milligrams");
dataAdapter2.add("Grams");
dataAdapter2.add("Kilograms");
dataAdapter2.add("Metric Ton");
}
else //(spinner4.getSelectedItem().toString().equals("Distance"))
{
//spinner1 = (Spinner) findViewById(R.id.spinner1);
dataAdapter1.clear();
dataAdapter1.add("Millimeter");
dataAdapter1.add("Centimeter");
dataAdapter1.add("Meter");
dataAdapter1.add("Kilometer");
dataAdapter2.clear();
dataAdapter2.add("Millimeter");
dataAdapter2.add("Centimeter");
dataAdapter2.add("Meter");
dataAdapter2.add("Kilometer");
}
dataAdapter1.notifyDataSetChanged();
dataAdapter2.notifyDataSetChanged();
}`
If anyone could explain what's wrong please enlighten this newbie. =)
U are approaching this wrong. Here is what you should do..
Define Spinner 1, 2 and 3.
Define arrayAdapters for spinner 1,2,3.
Populate ArrayAdapter and define this as the adapter for Spinner 1 (spinner1.setadapter(arrayadapter1)
Then call the spinner1.setOnItemSelectedListener. In the method onItemSelected, populate the logic to populate the array for spinner 2 and spinner 3 as per your requirement. Then call spinner2.setadapter(arrayadapter2) and spinner3.setadapter(arrayadapter3).
This should work.
So initially, spinner2 and spinner3 will not have any content. even if user clicks, there will be nothing in the dropdown. But once Spinner1 is selected by user, spinner2 and spinner3 will have dropdown values.
Please read up on setOnItemSelectedListener. Lots of tutorials are available. You can refer here to get a start.
If this works for you, please accept my answer.
Object choice = spinner4.getSelectedItemPosition();
This can't work.
getSelectedItemPosition()
returns an int (documentation) which won't equal your String as in
if (choice.equals("Weight")
Never ever (except in rare occasions) use the Object class as a reference, as any and every object in Java will be an Object (duh!) so you probably compare apples with oranges (which both are fruits). Seems like you're coming from a more weakly typed language, eh? ;)
Solution: Compare the positions:
if (spinnerX.getSelectedItemPosition == 0)...
Also, are you sure you want to change the content of spinner1 if something is selected on spinner4?

how to create dynamic Spinners in android?

this code is working fine but its creating only one spinner dynamically i want to get value from database and want to run the loop till database values and generate dynamic Spinner inside the loop overhere i have mentioned the code of FOR LOOP but its not working and as well as i want to load different item in different spinner please give me idea how to do this?
public class DisciplineActivity extends Activity
{ ArrayList<String> selectDisciplineArrayList,disciplineInfoArrayList;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.discipline_view);
for(int i=0;i<20;i++)
{
disciplineInfoArrayList.add("select location item:"+i);
}
// for(int i=0;i<5;i++)
//{
Spinner disciplineInfoSpinner = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item,disciplineInfoArrayList);
spinnerArrayAdapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
disciplineInfoSpinner = (Spinner) findViewById(R.id.disciplineinfo_spinner);
disciplineInfoSpinner.setAdapter(spinnerArrayAdapter);
}//
}
You are creating a new Spinner and doing nothing with it.
You need to create an empty LinearLayout on your "discipline_view" layout, and then add your created Spinners on that LinearLayout, like this:
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear);
for(int i=0;i<5;i++) {
Spinner disciplineInfoSpinner = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item,disciplineInfoArrayList);
spinnerArrayAdapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
disciplineInfoSpinner.setAdapter(spinnerArrayAdapter);
linearLayout.addView(disciplineInfoSpinner);
}

Categories

Resources