arrayadapter for spinner keep error - java

I want to make spinner inside my Google Map class so i can change the view of googlemaps like HYBRID, SATELLITE so on.. I make a this spinner on fragment inside of fragment.
when i declare arrayaddapter it keep show "Unfortunately, --- has stopped"
public class GMaps extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.popup_map, null, false);
sview = (Spinner)getActivity().findViewById(R.id.spinner1);
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.getActivity(), android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sview.setAdapter(dataAdapter);
// ..
I think it is because the parameter context = this.getActivity()... I have try this.getView(), getActivity(), v.getContext, nothing works.

First of all you should change this
sview = (Spinner)getActivity().findViewById(R.id.spinner1);
to
sview = (Spinner)v.findViewById(R.id.spinner1);
it's becoz your Spinner view belong to you inflated layout view.
then try to set adapter like:
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sview.setAdapter(dataAdapter);

//your spinner is in popup_map.xml so you need to do
sview = (Spinner)getActivity().findViewById(R.id.spinner1);
instead of getActivity() you need to use View v
sview = (Spinner)v.findViewById(R.id.spinner1);

Related

I cannot use my arraylist in class fragment

I tried to use arraylist in fragment
my arraylist not work in fragment class.
please help me fix this
before I use this code in main activity, and then I added menu fragment I move this code to this fragment
and then my arraylist get error
this error " 'RelativeLayout(anroid.content.Context)' in 'android.widget.RelativeLayout' cannot be applied to '(com.cupaxxhd.mysurah.HomeFragment)' "
and this " 'MyAdapter(android.content.Context, java.util.Arraylist)' in 'com.cupaxxhd.mysurah.Myadapter' cannot be applied to '(com.cupaxxhd.mysurah.HomeFragment, java.util.Arraylist)'
public class HomeFragment extends Fragment {
RecyclerView mRecyclerView;
MyAdapter myAdapter;
public HomeFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_home, container, false);
mRecyclerView = v.findViewById(R.id.recyclerView);
mRecyclerView.setLayoutManager(new RelativeLayout(this));
myAdapter = new MyAdapter(this, getMyList());
mRecyclerView.setAdapter(myAdapter);
return v;
}
private ArrayList<Model> getMyList(){
ArrayList<Model> models = new ArrayList<>();
Model m = new Model();
m.setTitle("Al-Lahab");
m.setDescription("Surah Al-Lahab adalah surat ke-111 dalam Al-Qur'an.");
m.setDetails("تَبَّتْ يَدَا أَبِي لَهَبٍ وَتَبّ\n"
);
m.setImg(R.drawable.al_lahab);
models.add(m);
return models;
}
You need to pass Context instead of fragment instance and use LayoutManager instead of RelativeLayout. Like this:
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
new MyAdapter(getActivity(), getMyList());

Cannot resolve constructor ArrayAdapter (Listview , fragment )

I want to load a listview in my fragment and the problem with the adapter is the error - Cannot resolve constructor ArrayAdapter.
This is the onCreateView method in my class :
public class AndroidGUIModifier implements IMyComponentGUIModifier, IFragmentEvents {
#Override
public void onCreateView() {
tv1 = (TextView) fragment.findViewById("xtv1");
tv2 = (TextView) fragment.findViewById("xtv2");
lv=(ListView) fragment.findViewById("xdevices") ;
mydevices = new ArrayList<String>();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(AndroidGUIModifier.this,android.R.layout.simple_list_item_1,mydevices);// the problem here : Cannot resolve constructor ArrayAdapter
lv.setAdapter(adapter);
}
}
Obtain the Context from the fragment and just pass it to the ArrayAdapter :
ArrayAdapter<String> adapter = new ArrayAdapter<String>(fragment.getContext(), android.R.layout.simple_list_item_1, mydevices);
If it is inside a fragment, you need to pass context for the first argument usually like this I think?
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, mydevices);
If you are using AndroidGUIModifier as your fragment, can you try extending the Fragment class?
public class AndroidGUIModifier extends Fragment implements IMyComponentGUIModifier, IFragmentEvents {

Empty Spinner in fragment | Android

I am trying to show values in spinner from Arrayadapter in one of my fragments in the onCreateView in my public final class Manual extends Fragment:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View vista =inflater.inflate(R.layout.fragment_pag1,container,false);
fragment pag1
calcular= (Button)vista.findViewById(R.id.button);
etd=(EditText)vista.findViewById(R.id.editText);
resultadocp=(TextView)vista.findViewById(R.id.textView3);
lista = (Spinner)vista.findViewById(R.id.spinner);
String []opciones={"one","two","three","four","five"};
ArrayAdapter adapter = new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_1, opciones);
lista.setAdapter(adapter); return vista; }
i don´t understand why when runs aplication, this spinner shows empty
Declare The Array Like this:
private static final String[] opciones={"one","two","three","four","five"};
The Spinner Code Here Should be like:-
Spinner spinner=(Spinner)findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(this);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,
opciones);
adapter.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

How to get list in alert dialog?

I want to set an alert dialog with list of titles. I have database and table of time table in that.
Time table has column of title. I want to get the list of titles from time table.
I tried some code but it's not working. I have created query in TimeTableHelper to get list of table. But I don't know how to put it in alert dialog.
Can anyone help please?
selectTable.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TimeTableHelper th = new TimeTableHelper(getApplicationContext());
List<String> tables = new ArrayList<String>(th.getTitle());
AlertDialog.Builder alertDialog = new AlertDialog.Builder(AddEventActivity.this);
LayoutInflater inflater = getLayoutInflater();
View convertView = (View) inflater.inflate(R.layout.alertlistrow, null);
alertDialog.setView(convertView);
ListView lv = (ListView) convertView.findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, tables);
lv.setAdapter(adapter);
alertDialog.show();
}
});
I am getting error can not resolve constructor ArrayAdapter if I try to add list in this.
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, tables);
How t do this?
You need to pass activity context but inside onClick method this means the onClickListener interface.
Change
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, tables);
to
// Replace MyActivity with the activity you are currently in or getActivity() if inside a fragment
ArrayAdapter adapter = new ArrayAdapter(MyActivity.this, android.R.layout.simple_list_item_1, tables);

why setting an adapter to a listView doesn't show the list content?

I try to fill a listView with the following code:
private void getPhoneComments(View rootView) {
String phoneNumber = ((EditText) rootView.findViewById(R.id.phone_editText)).getText().toString();
Phone phone = phoneDal.getItem(phoneNumber);
if (phone != null) {
List<String> comments = commentDal.getAllItems(phone.id);
ListView lv = ((ListView) rootView.findViewById(R.id.comments_list));
// This is the array adapter, it takes the context of the activity as a
// first parameter, the type of list view as a second parameter and your
// array as a third parameter.
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(
getActivity(),
android.R.layout.simple_list_item_1,
comments);
lv.setAdapter(arrayAdapter);
}
}
but the list still isn't displayed to the screen.
1) When do I need to add invalidate data
or what else I need to change in order to code to work?
2) how can i disply an option popup when there was a long push on each list item?
Edit:
phone != null
comments != empty
lv is set OK:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_item_detail, container, false);
setCommentsVisibility(rootView, false);
lv = ((ListView) rootView.findViewById(R.id.comments_list));

Categories