The following code gives me this error: The constructor ArrayAdapter(TrainingDetails, int, String[]) is undefined. How to rectify it? Please help. Here I am trying to retrieve the value of the selected spinner and store it in another string.
package com.example.fragments;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
import com.example.R;
public class TrainingDetails extends BaseFragment
{
View view;
private EditText nameEdt;
private Spinner trainingTypes;
private Spinner trainerProfile;
String data1[] = {"1", "2", "3"};
String data2[] = {"a", "b", "c"};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
view=inflater.inflate(R.layout.training_details, container,false);
nameEdt = (EditText) view.findViewById(R.id.name_edt);
trainingTypes = (Spinner) view.findViewById(R.id.trainingtypes_spn);
ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data1);
adapter_state.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
trainingTypes.setAdapter(adapter_state);
trainingTypes.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
int index = arg0.getSelectedItemPosition();
String selectedItem = data1[index];
}
#Override
public void onNothingSelected(AdapterView<?> arg0)
{
}
});
trainerProfile = (Spinner) view.findViewById(R.id.trainerprofile_spn);
return view;
}
}
change
ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data1);
to
ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, data1);
in Fragment you'll get Context by using getActivity()
For ArrayAdapter you need to pass Context
change this
ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data1);
into
ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, data1);
The first parameter of ArrayAdapter is a Context object not an instance of a Fragment. Change
new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data1);
with
new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, data1);
Related
I have a fragment that loads 3 spinners and a button. The list array of the second and third spinner is determined by the value of spinner 1 so when you change spinner 1, the arrays of spinners 2 and 3 change on select.
This works but I'm now struggling to retrieve the string value (and ultimately the relative numerical value) of spinner 2 on the click of the button.
In order to test my code I'm just using Toast to display the string.
The ultimate goal is to have a calculation in the background (e.g. spinner 2 multiplied by spinner 3) but when I use OnClick, the string of spinner 2 at time of onCreate is displayed.
I have tried to put a switch in OnItemSelectedListener to attribute this to both spinners but it's not worked.
Really, I want to click the button and the string of spinner 2 is displayed in toast.
Here's my code:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
public class Convertor extends Fragment {
public Convertor() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View view=inflater.inflate(R.layout.convertor_1, container, false);
String [] values_conversion = {"Convert1","Convert2",};
final Spinner spinner1 = view.findViewById(R.id.spinner1);
final Spinner spinner2 = view.findViewById(R.id.spinner2);
final Spinner spinner3 = view.findViewById(R.id.spinner3);
final ArrayAdapter adapter1 = ArrayAdapter.createFromResource(getActivity(), R.array.array1_array, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
final ArrayAdapter adapter2 = ArrayAdapter.createFromResource(getActivity(), R.array.array2_array, android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
final ArrayAdapter adapter3 = ArrayAdapter.createFromResource(getActivity(), R.array.array3_array, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
final ArrayAdapter adapter4 = ArrayAdapter.createFromResource(getActivity(), R.array.array4_array, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter1);
spinner3.setAdapter(adapter3);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, values_conversion);
adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner1.setAdapter(adapter);
spinner1.setSelection(0);
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, final View view, int position,
long id) {
switch (position) {
case 0:
spinner2.setAdapter(adapter1);
spinner3.setAdapter(adapter3);
break;
case 1:
spinner2.setAdapter(adapter2);
spinner3.setAdapter(adapter4);
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
final String from_unit1 = spinner2.getSelectedItem().toString(); // This gives the from string
final Button convert_btn = (Button) view.findViewById(R.id.convert_btn);
convert_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast toast = Toast.makeText(getContext(), from_unit1, Toast.LENGTH_SHORT);
toast.show();
}
});
return view;
}
}
try this
final Button convert_btn = (Button) view.findViewById(R.id.convert_btn);
convert_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String from_unit1 = spinner2.getSelectedItem().toString();
Toast toast = Toast.makeText(getContext(), from_unit1, Toast.LENGTH_SHORT);
toast.show();
}
});
I am new to android, I am trying to implement the cascading spinner. When I select the first spinner it should populate the second spinner choices based on the 1st spinner selection. I have almost done, but in second spinner I cannot select the value. When I select the value it is disappearing. Please help me in this.
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
public class DummyMain extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
public Spinner spinner1, spinner2;
public String[] spinner1Values = {"A", "B"}, valuesForA = {"1", "2"}, valuesForB = {"3", "4", "5", "1"};
public ArrayAdapter<String> adapterForSpinner1, adapterForSpinner2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dummy_main);
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
adapterForSpinner1 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, spinner1Values);
adapterForSpinner1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapterForSpinner1);
spinner1.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int i, long l) {
Toast.makeText(this, "Result "+spinner1.getSelectedItem().toString(), Toast.LENGTH_SHORT).show();
if (parent.getId() == R.id.spinner1) {
if (spinner1.getSelectedItem().toString().equals("A")) {
adapterForSpinner2 = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_spinner_item,valuesForA);
adapterForSpinner2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapterForSpinner2);
spinner2.setOnItemSelectedListener(this);
} else {
adapterForSpinner2 = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_spinner_item,valuesForB);
adapterForSpinner2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapterForSpinner2);
spinner2.setOnItemSelectedListener(this);
}
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
Found the issue, I have fixed it.
ArrayAdapter<String> adapterForSpinner2 = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_spinner_item, valuesForB);
I have changed the getApplicationConetxt() into this. It works :-)
ArrayAdapter<String> adapterForSpinner2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, valuesForB);
try adding this in your DummyMain
#Override
public void onPointerCaptureChanged(boolean hasCapture) {
}
I have a Fragment activity that I am using to make a tab bar. Inside this activity I am planning to put a table or ListViews with some random values.
Here's my code:
import com.example.mvaguimaraes.beetrackprov.R;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
public class Orders extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.orders, container, false);
ListView listView1 = (ListView) findViewById(R.id.listView1);
Order[] items = {
new Order(1, "Milk", 21.50),
new Order(2, "Butter", 15.99),
new Order(3, "Yogurt", 14.90),
new Order(4, "Toothpaste", 7.99),
new Order(5, "Ice Cream", 10.00),
};
ArrayAdapter<Order> adapter = new ArrayAdapter<Order>(this,
android.R.layout.simple_list_item_1, items);
listView1.setAdapter(adapter);
listView1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String item = ((TextView) view).getText().toString();
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
}
});
}
}
The problem is that I cannot extend my Orders class to Fragment and Activity so I'm getting this errors in my code:
Cannot resolve method findViewByID(int)
ListView listView1 = (ListView) findViewById(R.id.listView1);
Cannot resolve constructor
ArrayAdapter<Order> adapter = new ArrayAdapter<Order>(this,
android.R.layout.simple_list_item_1, items);
Cannot resolve method getBaseContext()
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
Probably because they inherit something from Activity. There's any way to make them work inside the Fragment? Sorry for my noob-ness I am new to Android Development.
To open another screen after clicking on one of the items of the listview I did this right after the onItemClick public void method:
String item = ((TextView) view).getText().toString();
Intent i = new Intent(getApplicationContext(), OrderDetails.class);
i.putExtra("new_variable_name",item);
Intent browserIntent =
new Intent(Orders.this, OrderDetails.class);
startActivity(browserIntent);
startActivity(i);
I tried to replace both "getApplicationContext()" and "Orders.this" with "getActivity()" but it didn't work.
Fragments do not have a findViewById() method like Activities. Instead you call findViewById() to lookup views on the View returned from inflating the layout in onCreateView(). Also, where you need a Context you typically use the Activity the fragment is attached to by calling getActivity().
public class Orders extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.orders, container, false);
ListView listView1 = (ListView) v.findViewById(R.id.listView1);
Order[] items = {
new Order(1, "Milk", 21.50),
new Order(2, "Butter", 15.99),
new Order(3, "Yogurt", 14.90),
new Order(4, "Toothpaste", 7.99),
new Order(5, "Ice Cream", 10.00),
};
ArrayAdapter<Order> adapter = new ArrayAdapter<Order>(getActivity(),
android.R.layout.simple_list_item_1, items);
listView1.setAdapter(adapter);
listView1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String item = ((TextView) view).getText().toString();
Toast.makeText(getActivity(), item, Toast.LENGTH_LONG).show();
}
});
return v;
}
}
I have two buttons tab1 and tab2.Within tab1 i have few edittext.I want to allow the user to click on the tab2 only when all the edittext in tab1 are filled,they should not be empty.
package com.megasys.roushfunway.survey;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import com.megasys.roushfunway.MasterFragment;
import com.megasys.roushfunway.MasterFragmentActivity;
import com.megasys.roushfunway.R;
public class SurveyFragment extends MasterFragment implements OnClickListener {
View view;
String[] seventPublic, syes, time, people,usa;
Button tab1,tab2;
LinearLayout layout1,layout2;
ArrayAdapter<String> eventyes;
ArrayAdapter<String> usaStates;
ArrayAdapter<String> eventTime;
ArrayAdapter<String> eventPublic;
ArrayAdapter<String> eventPeople;
boolean check=false;
EditText etNoEventDays=null, etMaterialQtyHanded, etEventItemType, etFeedback,
etCommentOpportunity, etHotelName, etHotelCity, etHotelZip,
etHotelRate, etHotelAddress, etHotelPhone;
Spinner spEventPublicPrivate, spEventContact, spPassMaterial,
spAddedEventItems, spEventTimeFrame, spPeopleAttached,
spIntroduceOnsiteContact, spState, spWasHotelBooked;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initView();
}
private void initView() {
etNoEventDays = (EditText) view.findViewById(R.id.et_no_of_events);
etMaterialQtyHanded = (EditText) view
.findViewById(R.id.et_material_quantity_handed);
etEventItemType = (EditText) view
.findViewById(R.id.et_event_items_type);
etFeedback = (EditText) view.findViewById(R.id.et_feedback);
etCommentOpportunity = (EditText) view
.findViewById(R.id.et_opportunity);
etHotelName = (EditText) view.findViewById(R.id.et_Hotel_Name);
etHotelCity = (EditText) view.findViewById(R.id.et_Hotel_City);
etHotelAddress = (EditText) view.findViewById(R.id.et_Hotel_Address);
etHotelZip = (EditText) view.findViewById(R.id.et_Hotel_Zip);
etHotelRate = (EditText) view.findViewById(R.id.et_Hotel_Rate);
spAddedEventItems = (Spinner) view
.findViewById(R.id.sp_event_added_items);
spEventPublicPrivate = (Spinner) view
.findViewById(R.id.sp_event_public_private);
spEventContact = (Spinner) view
.findViewById(R.id.sp_event_contact_prepared);
spEventTimeFrame = (Spinner) view
.findViewById(R.id.sp_event_time_frame);
spPassMaterial = (Spinner) view.findViewById(R.id.sp_pass_out_material);
spPeopleAttached = (Spinner) view
.findViewById(R.id.sp_introduce_yourself);
spPeopleAttached = (Spinner) view
.findViewById(R.id.sp_no_of_people_attended);
spState = (Spinner) view.findViewById(R.id.sp_state);
spIntroduceOnsiteContact = (Spinner) view
.findViewById(R.id.sp_introduce_yourself);
spWasHotelBooked = (Spinner) view.findViewById(R.id.sp_hotel_booked);
seventPublic = getResources().getStringArray(R.array.public_private);
eventPublic = new ArrayAdapter<String>(mContext,
android.R.layout.simple_spinner_item, seventPublic);
eventPublic
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spEventPublicPrivate.setAdapter(eventPublic);
syes = getResources().getStringArray(R.array.yes_no);
eventyes = new ArrayAdapter<String>(mContext,
android.R.layout.simple_spinner_item, syes);
eventPublic
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spEventContact.setAdapter(eventyes);
spAddedEventItems.setAdapter(eventyes);
spPassMaterial.setAdapter(eventyes);
spIntroduceOnsiteContact.setAdapter(eventyes);
spWasHotelBooked.setAdapter(eventyes);
time = getResources().getStringArray(R.array.time_frame);
eventTime = new ArrayAdapter<String>(mContext,
android.R.layout.simple_spinner_item, time);
eventTime
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spEventTimeFrame.setAdapter(eventTime);
people = getResources().getStringArray(R.array.people_attended);
eventPeople = new ArrayAdapter<String>(mContext,
android.R.layout.simple_spinner_item, people);
eventPeople
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spPeopleAttached.setAdapter(eventPeople);
usa=getResources().getStringArray(R.array.USA);
usaStates=new ArrayAdapter<String>(mContext, android.R.layout.simple_spinner_item,usa);
usaStates
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spState.setAdapter(usaStates);
tab1=(Button) view.findViewById(R.id.tab1);
tab2=(Button) view.findViewById(R.id.tab2);
tab1.setOnClickListener(this);
tab2.setOnClickListener(this);
layout1=(LinearLayout) view.findViewById(R.id.layout1);
layout2=(LinearLayout) view.findViewById(R.id.layout2);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.survey_addedit_fragment, container,
false);
return view;
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.tab1:
layout1.setVisibility(arg0.VISIBLE);
layout2.setVisibility(arg0.GONE);
break;
case R.id.tab2:
validation();
if(check){
layout2.setVisibility(arg0.VISIBLE);
layout1.setVisibility(arg0.GONE);
}else{
layout1.setVisibility(arg0.VISIBLE);
layout2.setVisibility(arg0.GONE);
}
default:
break;
}
}
public boolean validation(){
if(etNoEventDays==null){
etNoEventDays.setBackgroundColor(Color.RED);
MasterFragmentActivity.showprogressDialog(mContext, "Error",
"Please fill all the mandatory fields", true);
check=true;
}
return check;
}
}
I have two buttons tab1 and tab2.Within tab1 i have few edittext.I want to allow the user to click on the tab2 only when all the edittext in tab1 are filled,they should not be empty.
When the fragment is opened i.e the onCreateView method is called the spinner.setOnItemSelectedListener(new CustomOnItemSelectedListener()) is called even though no item has been selected yet.
Why is this and how would it be possible to change the code to make it so that the method is only called when an item is selected?
here is the code:
package com.petroc.nationaldiploma;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import com.actionbarsherlock.app.SherlockFragment;
public class AddGradesFragment extends SherlockFragment {
private GradesDataSource datasource;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Spinner spinner;
View view = inflater.inflate(R.layout.fragment_add_grades, container,
false);
spinner = (Spinner) view.findViewById(R.id.spinner);
List<String> list = new ArrayList<String>();
list.add("Pass");
list.add("Merit");
list.add("Distinction");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(
getActivity(), android.R.layout.simple_spinner_item, list);
dataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
spinner = (Spinner) view.findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(new CustomOnItemSelectedListener());
// EditText editText = (EditText)
// view.findViewById(R.id.editModuleText1);
return view;
}
}
Any help is appreciated thanks
when you set data into the spinner using list object and adapter it was select the default value as 0 on setting time so that's why you getting selected