ArrayAdaptor constructor not being found - android

I am working to display a list of all devices that have been connected to an Android phone. I would really like to keep using the fragments to help with easy navigation. I am getting an error on the ArrayAdapter adapter1 code that states Cannot resolve constructor 'ArrayAdapter(com.henryjarend.test.Connect2, int, java.util.List<java.lang.String>)'
I have looked at the constructor on the android developer APIs and it seems like it should be working.
Here is the code:
public class Connect2 extends ListFragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.connect2, container, false);
}
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
BluetoothAdapter myBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices = myBluetoothAdapter.getBondedDevices();
List<String> previousDevices = new ArrayList<String>();
for(BluetoothDevice bt : pairedDevices){
previousDevices.add(bt.getName());
}
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, R.layout.connect2, previousDevices);
//setListAdapter(new ArrayAdapter<String>(this, android.R.id.list, previousDevices));
}
}
the commented out section was another example I had found online but it had the same issue that the uncommented one is having.

The problem :
ArrayAdapter<String> adapter1 =
new ArrayAdapter<String>(this, R.layout.connect2, previousDevices)
this here is a ListFragment, get activity of this fragment for the context.
Solution :
ArrayAdapter<String> adapter1 =
new ArrayAdapter<String>(getActivity(), R.layout.connect2, previousDevices)

Related

converting a activity to fragment it shows some errors on array adapter [duplicate]

This question already has answers here:
Cannot resolve constructor ArrayAdapter
(3 answers)
how to call ArrayAdapter constructer from Fragment in Android
(2 answers)
Getting a constructor error with ArrayAdapter in a ListFragment on Android
(1 answer)
Context for ArrayAdapter in Fragment Class
(3 answers)
Cannot resolve constructor ArrayAdapter (Listview , fragment )
(2 answers)
Closed 4 years ago.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnScan = (Button)findViewById(R.id.scan);
listViewIp = (ListView)findViewById(R.id.listviewip);
bar = (ProgressBar) findViewById(R.id.pbar);
bar.setVisibility(View.INVISIBLE);
ipList = new ArrayList();
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, ipList);
listViewIp.setAdapter(adapter);
btnScan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new ScanIpTask().execute();
}
});
convert it to fragment
ArrayList ipList;
ArrayAdapter adapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_scan_ip, container, false);
btnScan = (Button)rootView.findViewById(R.id.scan);
listViewIp = (ListView)rootView.findViewById(R.id.listviewip);
bar = (ProgressBar) rootView.findViewById(R.id.pbar);
bar.setVisibility(View.INVISIBLE);
ipList = new ArrayList();
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, ipList);
listViewIp.setAdapter(adapter);
btnScan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new ScanIpTask().execute();
}
});
return rootView;
}
it shows error in this line
adapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, android.R.id.text1, ipList);
Replace the code with
adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1, ipList);
ArrayAdapter constructor's first argument is Context. And Fragment
is not a child of Context. You need to pas the Context of the parent Activity.
ArrayAdapter(Context context, int resource, int textViewResourceId)
Use getActivity() to get the context of parent Activity.
adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, android.R.id.text1, ipList);
listViewIp.setAdapter(adapter);
getActivity() inside onCreateView() can return null sometimes cause fragment not attached yet. To clear the concept pls read This post or similar.

Adding items on a spinner using a cutom function in a fragment

How do i add items to a spinner in a fragment using a custom method since i have many spinners and setting each on the create view makes the code have unprofessional looking
This is what i have tried but returns an error
I had also declared the provincespinner
private Spinner provincespinner, districtspinner;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_basicinfo, container, false);
addItemsOnProvinceSpinner(rootView); //this is what i would like to add items
...i have other methods to add other spinners here
return rootView;
}
This is the method that should add but returns an error
public void addItemsOnProvinceSpinner(View rootview) {
provincespinner = (Spinner) rootview.findViewById(R.id.fivms_farmerprovince);
List<String> list = new ArrayList<String>();
list.add("list 1");
list.add("list 2");
list.add("list 3");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, list); //This returns an error
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
provincespinner.setAdapter(dataAdapter);
}
How do i go about this
Modify your line like this:
ArrayAdapter<String> dataAdapter = new
ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item, list);
the first parameter is context and you are passing this which works for activity

Android add view to fragment and bind adapters to views

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

ListView data not showing in fragment

Here's the code that's producing unexpected results. If anyone can please guide me to what I'm doing wrong here
public static class LaunchpadSectionFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_section_launchpad, container, false);
ListView moviesList= (ListView) rootView.findViewById(R.id.movies_list);
ArrayAdapter<String> moviesAdapter;
List<String> Testing = new ArrayList<String>();
Testing.add("Hey");
Testing.add("Hey");
Testing.add("Hey");
Testing.add("Hey");
moviesAdapter= new MyAdapter(getActivity(),android.R.layout.simple_list_item_1,Testing);
moviesList.setAdapter(moviesAdapter);
return rootView;
}
Ok after playing around a Lot going through documentation I finally found a workaround to this.
This is what I did
#Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
super.onActivityCreated(savedInstanceState);
Log.d("CreatedActivity", "LaunchpadFragment");
ListView moviesList= (ListView) getActivity().findViewById(R.id.movies_list);
// Gets the ListView from the View list of the parent activity
// Gets a CursorAdapter
ArrayAdapter<String> moviesAdapter;
List<String> Testing = new ArrayList<String>();
Testing.add("Hey");
Testing.add("Hey Do");
Testing.add("Hey It");
Testing.add("Hey Please");
moviesAdapter= new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, Testing);
moviesList.setAdapter(moviesAdapter);
}
Note the over-ridden method here, it is
onActivityCreated
Android documentation states
If your view of the fragment is a listview that is being filled by a data adapter, you should override the onActivityCreated method and do the filling in there.
I don't have a documentation link to this blockquote though:|

How to set ArrayAdapter for spinner in Fragment

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

Categories

Resources