AutoCompleteTextView does not work in ViewHolder - java

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.

Related

setAdapter(T) cannot be applied to (android.widget.ListAdapter) when using fragment

I am getting an error at autoCompleteTextView it says setAdapter(T) cannot be applied to (android.widget.ListAdapter)
public class HomeFragmnet extends Fragment {
TextInputLayout textInputLayout;
AutoCompleteTextView autoCompleteTextView;
ListView listView;
ListAdapter listAdapter;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstancesState){
View v =inflater.inflate(R.layout.fragment_home, null);
textInputLayout = (TextInputLayout) v.findViewById(R.id.menu_dropdown);
autoCompleteTextView = (AutoCompleteTextView) v.findViewById(R.id.drop_items);
String [] items={"Pune", "Mumbai", "Nashik"};
listAdapter = new ArrayAdapter<String>(getActivity() , R.layout.items_list, items );
autoCompleteTextView.setAdapter( listAdapter );
return v;
}
}
According to the docs of AutocompleteTextView its setAdapter method takes a <T extends ListAdapter & Filterable>, meaning the adapter must both extend ListAdapter and implement Filterable.
ListAdapter itself does not implement Filterable, but some subclasses such as ArrayAdapter do.

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 {

How to Use Parse.com Lists in Tabs?

I've got the following function:
public static class ListFragment extends Fragment {
private ParseQueryAdapter<ParseObject> mainAdapter;
private ListView listView;
public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState )
{
View rootView = inflater.inflate(R.layout.fragment_list, container, false );
mainAdapter = new ParseQueryAdapter<ParseObject>( this, "Todo" );
mainAdapter.setTextKey("title");
mainAdapter.setImageKey("image");
// Initialize ListView and set initial view to mainAdapter
listView = (ListView) findViewById(R.id.list);
listView.setAdapter(mainAdapter);
mainAdapter.loadObjects();
return rootView;
}
}
The errors returned are:
The constructor ParseQueryAdapter(MainActivity.ListFragment, String) is undefined MainActivity.java
Cannot make a static reference to the non-static method findViewById(int) from the type Activity MainActivity.java
I can assume that the first one due to the change of the object of type this but I would like a more seasoned input on the correct fix.
The second error though thoroughly confounds as it appears to be valid to my eyes.
Appreciate any input.
1) Change the instantiation of ParseQueryAdapter as follows. The code is in a Fragment, but ParseQueryAdapter requires a Context object.
mainAdapter = new ParseQueryAdapter<ParseObject>( this.getActivity(), "Todo" );
2) Remove the static modifier from your class definition.

arrayadapter for spinner keep error

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

Custom ArrayAdapter not displaying results

This one has me quite stumped. I'm sure it is just something simple I am missing, but I cant seem to find out what...
When I run the program, it opens up the dialog box and displays the AutoCompleteTextView I have initialized. When I try to type something into it, nothing drops down or is displayed other than the text I type in. I have created a similar system in another part of my program with the same mechanics, but using a regular ArrayAdapter and it works fine so the interface is not the problem.
Here is where I initialize my custom ArrayList. I have been trying to use just strings to make it simpler.
final Dialog weaponDialog = new Dialog(BattleScreen.this);
weaponDialog.setContentView(R.layout.weapon_selection_dialog);
weaponDialog.setTitle("Add a Weapon");
weaponDialog.setCancelable(true);
String[] weaponStringArrayList = ConstantEquipmentHelper.getCondensedWeaponString();
WeaponArrayAdapter weaponAdapter = new WeaponArrayAdapter(this, R.layout.weapon_list_item, weaponStringArrayList);
weaponDialogAcTextView = (AutoCompleteTextView) weaponDialog.findViewById(R.id.weaponSelectionAutoCompleteTxt);
weaponDialogAddButton = (Button) weaponDialog.findViewById(R.id.weaponSelectionAddButton);
weaponDialogWeaponInfo = (TextView) weaponDialog.findViewById(R.id.weaponSelectionInformationTxt);
...
...
...
Here is my custom ArrayAdapter Class
public class WeaponArrayAdapter extends ArrayAdapter<String> {
private Context context;
String[] objects;
public WeaponArrayAdapter(Context context, int textViewResourceId, String[] objects) {
super(context, textViewResourceId);
this.objects = objects;
this.context = context;
}
private class WeaponItemHolder {
TextView weaponName;
TextView weaponCat;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//return super.getView(position, convertView, parent);
final WeaponItemHolder holder;
if (convertView == null) {
//Sets up a new holder to temporaraly hold the listeners that will be assigned to the binded variables
holder = new WeaponItemHolder();
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.weapon_list_item, null);
//Find the IDs! Find them!!!!
holder.weaponName = (TextView) convertView.findViewById(R.id.weaponListItemName);
holder.weaponCat = (TextView) convertView.findViewById(R.id.weaponListItemCategory);
//"Sets the tag associated with this view. A tag can be used
//to mark a view in its hierarchy and does not have to be unique within the hierarchy."
convertView.setTag(holder);
} else {
holder = (WeaponItemHolder) convertView.getTag();
}
String spellName = objects[position];
String[] weaponInfo = spellName.split("\\:");
weaponInfo[1] = weaponInfo[1].trim();
holder.weaponName.setText(weaponInfo[0]);
holder.weaponCat.setText(weaponInfo[1]);
return convertView;
}
}
Additional Info: I have tried debugging it and it never reaches getView. This makes sense of course, as its not displaying anything.
Thanks,
-Andrew
EDIT: I have found out how to implement the above problem:
I used a SimpleAdapter with a custom layout.
However, now I can not select any of the items... onItemClick is not even called when I try to click it. It probably has to do with using the SimpleAdapter??
LINK: http://lemonbloggywog.wordpress.com/2011/02/15/customer-autocomplete-contacts-android/
ArrayList<Map<String, String>> weaponStringArrayList = ConstantEquipmentHelper.getCondensedWeaponString();
//The adapter that recieves the layout type from android and the array creatd by the above function.
SimpleAdapter simpleAdapter = new SimpleAdapter(this, weaponStringArrayList, R.layout.weapon_list_item ,new String[] {"name", "category"}, new int[] { R.id.weaponListItemName, R.id.weaponListItemCategory});
//Find the view blah blah blah...
weaponDialogAcTextView = (AutoCompleteTextView) weaponDialog.findViewById(R.id.weaponSelectionAutoCompleteTxt);
weaponDialogAddButton = (Button) weaponDialog.findViewById(R.id.weaponSelectionAddButton);
weaponDialogWeaponInfo = (TextView) weaponDialog.findViewById(R.id.weaponSelectionInformationTxt);
//Set that adapter!
weaponDialogAcTextView.setAdapter(simpleAdapter);
You have to implement getCount() and set the count of your data, i.e. objects.length.
You also have to set the adapter to the view using the method setAdapter().
Hope this helps!

Categories