I try to implement the items to a Spinner from a android.support.v4.app.Fragment class. I always get the compiler Error
The constructor ArrayAdapter(LayoutNext, int, String[]) is undefined
How can I fix this?
Here is the code:
public class LayoutNext extends Fragment
implements OnClickListener,OnItemSelectedListener{
TimePicker timepicker;
private Spinner spinner_next;
public ArrayAdapter<String> adapter;
public static Fragment newInstance(Context context) {
LayoutNext f = new LayoutNext();
return f;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.layout_next, null);
timepicker = (TimePicker) root.findViewById(R.id.timePicker1);
timepicker.setIs24HourView(true);
timepicker.setCurrentHour(0);
timepicker.setCurrentMinute(0);
String[] items_next = { "Next", "From to"};
spinner_next = (Spinner) root.findViewById(R.id.sp_next);
spinner_next.setOnItemSelectedListener(this);
// Here I get the error
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_spinner_item,
items_next);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_next.setAdapter(adapter);
return root;
}
Unlike Activities, Fragments are not subclasses of Context so you cannot use this where a Context is required. Simply use getActivity() instead.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getActivity(),
android.R.layout.simple_spinner_item,
items_next);
Related
enter image description hereBelow is the coding i have and it seems i have problem on the
AgentAdapter adapter = new AgentAdapter(getActivity(), R.layout.list_agent, member_names, profile_pics);
How do i use image and text together for the list adapter i have in my fragment? Please help.
public class AgentFragment extends Fragment{
String[] member_names;
TypedArray profile_pics;
ListView mylistview;
String[] agentname={"Robert","Shanni","Rachel","Mady","Nikhil"};
Integer [] imgid = {R.drawable.robert,R.drawable.shanni,R.drawable.rachael,R.drawable.maddy_pic,R.drawable.nikhil_pic};
public AgentFragment(){
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.list_layout1, null);
setupList(view);
return view;
}
private void setupList(View view){
AgentAdapter adapter = new AgentAdapter(getActivity(), R.layout.list_agent, member_names, profile_pics);
mylistview = (ListView) view.findViewById(R.id.listView);
mylistview.setAdapter(adapter);
}
}
Since its possible that the fragment has not been placed in any activity at runtime you can afford for this by changing the getActivity() to be the context from the view parameter:
private void setupList(View view){
AgentAdapter adapter = new AgentAdapter(view.getContext(), R.layout.list_agent, member_names, profile_pics);
mylistview = (ListView) view.findViewById(R.id.listView);
mylistview.setAdapter(adapter);
}
i am trying to get a list view working in android studio.
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
populateListView();
return inflater.inflate(R.layout.stand1,container,false);
}
private void populateListView() {
String[] pair = {"Pair1","Pair2","Pair3","Pair4","Pair5"};
//build adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.stand1,pair);
//populate
ListView list = (ListView) findViewById(R.id.Stand1list);
list.setAdapter(adapter);
}}
The errors i am getting are
Error:(31, 40) error: no suitable constructor found for ArrayAdapter(Stand1,int,String[])
constructor ArrayAdapter.ArrayAdapter(Context,int,int) is not applicable
(argument mismatch; Stand1 cannot be converted to Context)
constructor ArrayAdapter.ArrayAdapter(Context,int,String[]) is not applicable
(argument mismatch; Stand1 cannot be converted to Context)
constructor ArrayAdapter.ArrayAdapter(Context,int,List<String>) is not applicable
(argument mismatch; Stand1 cannot be converted to Context)
Error:(34, 36) error: cannot find symbol method findViewById(int)
stand1 is an xml file, Stand1List is the listview id inside the stand1.xml
I know its something simple , but for the life of me i dont know what...
For references Stand1.java is java file which is a fragment inside the activity_main. It is being called by a nav drawer.
Update:
Thanks to Jedil Answer, i have it kinda working. However i have gotten an error when i load the fragment saying text view needs an ID.
I have no put a textview inside the stand1.xml Below.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/Stand1list">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tex1"
/>
</ListView>
</FrameLayout>
And now i am getting error
java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
change your code to this instead:
View view = null;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.stand1,container,false);
populateListView(view);
return view;
}
private void populateListView(View mView) {
String[] pair = {"Pair1","Pair2","Pair3","Pair4","Pair5"};
//build adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), R.layout.stand1,pair);
//populate
ListView list = (ListView) mView.findViewById(R.id.Stand1list);
list.setAdapter(adapter);
}}
ListView listview; // define this on class level
View view = inflater.inflate(R.layout.stand1,container,false);
listview = view.findViewById(R.id.Stand1list);
populateListView();
retur view ;
And In populateListView() you set adapter in this way
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.stand1,pair);
You are calling populateListView() too early, because your view isn't created yet.
Try use populateListView() in
public void onViewCreated (View view, Bundle savedInstanceState)
Declare the ListView as a member variable of the fragment:
ListView list; // your ListView
And instead of passing this to your ArrayAdapter, you need to pass getActivity(), the host activity's context.
And finally, initialize the listView inside the onCreateView method.
list = (ListView) findViewById(R.id.Stand1list);
CODE:
ListView list; // your ListView
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.stand1,container,false);
list = (ListView) view.findViewById(R.id.Stand1list);
populateListView();
}
private void populateListView(View mView) {
String[] pair = {"Pair1","Pair2","Pair3","Pair4","Pair5"};
//build adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.stand1,pair);
list.setAdapter(adapter);
}
Your code is a fragment implementation code.
First error is about that ArrayAdapter needs Context as first argument.
So this in that place of code meansfragment object, but you need Context which could be Activity so instead of using this use getActivity() in those places
ArrayAdapter<String>(getActivity(), R.layout.stand1,pair);
and
ListView list = (ListView) getActivity().findViewById(R.id.Stand1list);
So how code should look like :
ListView mList;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.stand1,container,false);
mList = (ListView) root.findViewById(R.id.Stand1list);
return root;
}
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
populateListView();
}
private void populateListView() {
String[] pair = {"Pair1","Pair2","Pair3","Pair4","Pair5"};
//build adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.stand1,pair);
mList.setAdapter(adapter);
}}
I'm trying to implement a simple to do list using this solution Add user input into a ListView on button click, but when I implement the onCreate of the solution I get the following errors http://pastebin.com/9CBCMrjv I'm using fragments in this application and I'm wondering could this be causing the errors as maybe the constructors don't apply to fragments.
Can someone make sense of the errors or where I'm going wrong with this implementation? My understanding of the errors is that this solution can't be applied to my class as it is linked to a fragment instead of a plain activity.
This is the complete class to give you a better understanding of the problem:
public class TopRatedFragment extends Fragment implements OnClickListener {
ListView mListView;
EditText mValue;
Button mAdd;
ArrayList<String> list = new ArrayList<String>();
ArrayAdapter<String> adapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_top_rated, container, false);
return rootView;
}
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_top_rated);
mAdd = (Button)findViewById(R.id.newList);
mAdd.setOnClickListener(this);
mValue = (EditText)findViewById(R.id.listData);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, list);
// set the lv variable to your list in the xml
mListView=(ListView)findViewById(R.id.listView);
mListView.setAdapter(adapter);
}
public void onClick(View v)
{
String input = mValue.getText().toString();
if(input.length() > 0)
{
// add string to the adapter, not the listview
adapter.add(input);
// no need to call adapter.notifyDataSetChanged(); as it is done by the adapter.add() method
}
}
}
The errors
The method setContentView(int) is undefined for the type TopRatedFragment
The method findViewById(int) is undefined for the type TopRatedFragment
The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (TopRatedFragment)
The method findViewById(int) is undefined for the type TopRatedFragment
The constructor ArrayAdapter<String>(TopRatedFragment, int, ArrayList<String>) is undefined
The method findViewById(int) is undefined for the type TopRatedFragment
setContentView(R.layout.fragment_top_rated);
wrong
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_top_rated, container, false);
mAdd = (Button)rootView.findViewById(R.id.newList);
mAdd.setOnClickListener(this);
mValue = (EditText)rootView.findViewById(R.id.listData);
adapter = new ArrayAdapter<String>(getActivity, android.R.layout.simple_expandable_list_item_1, list);
// set the lv variable to your list in the xml
mListView=(ListView)rootView.findViewById(R.id.listView);
mListView.setAdapter(adapter);
return rootView;
}
You need to inflate a view in onCreateView and return the view
Also you use the inflated view object to initialize your views
The constructor ArrayAdapter<String>(TopRatedFragment, int, ArrayList<String>) is undefined
So change to
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_expandable_list_item_1, list);
get rid of the codes in onCreate
Hello I'm playing around with fragments as of now, and I can't seem to find a solution to this issue..
This is what I have tried so far:
FragmentReceivingStocksHeader.class
public class FragmentReceivingStocksHeader extends Fragment {
EditText etVanTransferDocument, etDocumentNumber;
Spinner spLocationFrom, spLocationTo;
Button btExit, btProcess;
ArrayAdapter<String> adapterFrom, adapterTo;
String[] locationsFromArray, locationsToArray;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
initControls();
View rootView =
inflater.inflate(R.layout.fragment_receivingstocks_transactionheader, container, false);
return rootView;
}
private void initControls() {
// TODO Auto-generated method stub
spLocationFrom = (Spinner) findViewById (R.id.spLocationFrom);
spLocationFrom.setEnabled(false);
spLocationTo = (Spinner) findViewById (R.id.spLocationTo);
spLocationTo.setEnabled(false);
locationsFromArray = getResources().getStringArray(R.array.locations);
locationsToArray = getResources().getStringArray(R.array.locationsto);
adapterFrom = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, locationsFromArray);
adapterTo = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, locationsToArray);
spLocationFrom.setAdapter(adapterFrom);
spLocationFrom.setSelection(1);
spLocationTo.setAdapter(adapterTo);
spLocationTo.setSelection(0);
}
But I have errors on this line:
spLocationFrom = (Spinner) findViewById (R.id.spLocationFrom);
Error Message:
The method findViewById(int) is undefined for the type FragmentReceivingStocksHeader
And also these lines:
adapterFrom = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, locationsFromArray);
adapterTo = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, locationsToArray);
Error Message:
The constructor ArrayAdapter<String>(FragmentReceivingStocksHeader, int, String[]) is undefined
What am I doing wrong in here? I need your help guys. Thanks.
In your initControls method, make the following modifications :
spLocationFrom = (Spinner) view.findViewById (R.id.spLocationFrom);
spLocationTo = (Spinner) view.findViewById (R.id.spLocationTo);
and
adapterFrom = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_dropdown_item, locationsFromArray);
adapterTo = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_dropdown_item, locationsToArray);
Explanation : findViewById is not available in Fragment like it is in Activity, so you need to use the View.findViewById method.
And the constructor or ArrayAdapter takes a Context as first argument, so Activity works but not Fragment.
The problem is, that Fragment class doesnt have method like findViewById, like Activity does. You need to use your inflated rootView.findViewById.
As for your adapters, similarly, the constructor doesnt accept Fragment type, but Context. Activity class extends Context, and you can access your activity from Fragment with getActivity() method.
See your code updated below:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView =
inflater.inflate(R.layout.fragment_receivingstocks_transactionheader, container, false);
initControls(rootView);
return rootView;
}
private void initControls(View view) {
// TODO Auto-generated method stub
spLocationFrom = (Spinner) view.findViewById (R.id.spLocationFrom);
spLocationFrom.setEnabled(false);
spLocationTo = (Spinner) view.findViewById (R.id.spLocationTo);
spLocationTo.setEnabled(false);
locationsFromArray = getResources().getStringArray(R.array.locations);
locationsToArray = getResources().getStringArray(R.array.locationsto);
adapterFrom = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_dropdown_item, locationsFromArray);
adapterTo = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_dropdown_item, locationsToArray);
spLocationFrom.setAdapter(adapterFrom);
spLocationFrom.setSelection(1);
spLocationTo.setAdapter(adapterTo);
spLocationTo.setSelection(0);
}
I am trying to add items to spinner in fragment. But i am having problem with the context. Because in fragment there is no context. Here how i am doing
public class DetailFrag extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View scrollView = inflater.inflate(R.layout.myscrollview , container, false);
LinearLayout linearLayout = (LinearLayout) scrollView.findViewById(R.id.mylayout1);
for (int i=0; i<questionList.size(); i++) {
View verticalLinearLayout = inflater.inflate(R.layout.mylistrow, null);
View horizontalLInearLaoyout = verticalLinearLayout.findViewById(R.id.questionRow);
TextView tv = (TextView) horizontalLInearLaoyout.findViewById(R.id.question);
Spinner spinner = (Spinner) horizontalLInearLaoyout.findViewById(R.id.spinner);
//Problem: how to define this in fragment createFromResource(this,...)
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.options_array, android.R.layout.simple_spinner_item);
EditText editText = (EditText) verticalLinearLayout.findViewById(R.id.txtMultiLine);
String question = questionList.get(i).question;
tv.setId(i);
tv.setText(i + question);
spinner.setId(i);
editText.setId(i);
linearLayout.addView(verticalLinearLayout);
}
return scrollView;
} //end of onCreateView()
} //end of class DetailFrag
In fragments, context is not available the way you expected in your code. Instead of this, use the following:
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
getActivity().getBaseContext(),
R.array.options_array,
android.R.layout.simple_spinner_item);
Since there we are in a Fragment,'this', won't work thus:
Using getActivity().getBaseContext() should be used to replace 'this';
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>
(getActivity().getBaseContext(),
android.R.layout.simple_spinner_dropdown_item,
listItems);
Note on older versions of Android there is getContext() which will return context of the fragment as well, but for new versions it might not return context. Thus using getActivity() you are turned a Activity which is a context.
We use getActivity() which gets the current activity you are in followed by getBaseContext() since there can be multiple Context initiated in the Activity. We want to get the one that the Activity itself uses.
do something like :
private Context myContext = null;
public DetailFrag(Context ctx){
myContext = ctx;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//...
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
myContext, R.array.options_array, android.R.layout.simple_spinner_item);
//
}