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 {
Related
My autoCategory_book variable does not work from a ViewHolder.
The errors are:
error: cannot find symbol method getResources()
Category_book = getResources().getStringArray(R.array.category_book);
error: cannot infer type arguments for ArrayAdapter<>
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1, Category_book);
Code
public class ViewHolder extends RecyclerView.ViewHolder{
AutoCompleteTextView autoCategory_book;
String[] Category_book;
public ViewHolder(#NonNull View itemView) {
super(itemView);
autoCategory_book = itemView.findViewById(R.id.autoCategory_book);
Category_book = getResources().getStringArray(R.array.category_book);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1, Category_book);
autoCategory_book.setAdapter(adapter);
}
}
you can try this for fix it:
Category_book = itemView.getResources().getStringArray(R.array.category_book);
ArrayAdapter<String> adapter = new ArrayAdapter<>(itemView.getContext(),android.R.layout.simple_list_item_1, Category_book);
The ArrayAdapter constructor required a context, and you pass a reference for the view holder. And for access to the android resource you need a context too.
This is my method in CheckActivity.java
public void check5(View view) {
lv = (ListView) findViewById(R.id.listView1);
modelItems[5] = new Model("Item", 1);
CustomAdapter adapter = new CustomAdapter(this, modelItems);
lv.setAdapter(adapter);
}
This how I call method in non-activity class
CheckActivity mActivity = new CheckActivity();
mActivity.check5();
But this is the error I got.
I alt-entered and android studio create new method in CheckActivity.java
public void check5()
{
lv = (ListView) findViewById(R.id.listView1);
modelItems[5] = new Model("Item", 1);
CustomAdapter adapter = new CustomAdapter(this, modelItems);
lv.setAdapter(adapter);
}
But my code will not work with (View view) how do I fix that?
Edit - Some told me that this is bad idea to do. So if this is impossible how do I copy my method into non-activity class?
You need to pass Context of CheckActivity Activity in your non Activity Class and then you call Activity method from Non Activity Class by casting Context in Activity name.
((CheckActivity)context).check5(view);
pass view with just like you have called member function , so you can access variables of class CheckActvity declared as public .
Move your adapter initialization inside onCreate. You need to set the adapter to the ListView using setAdapter. Call noftifyDataSetChanged after populating the dataset arr.
Try this code,
private MyAdapter ma;
onCreate(){
...
setContentView(R.layout.activty_main2);
lv = (ListView) findViewById(R.id.lv_1);
ma = new MyAdapter(this, arr);
lv.setAdapter(ma);
initData();
}
initData(){
for(;;){
...
arr.add(n);
}
ma.noftifyDataSetChanged();
}
You not having any blank constructor in MyAdapter,
you have to pass context and ArrayList to the adapter like,
MyAdapter ma = new MyAdapter(Main2Activity.this,arr);
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);
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);