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);
}}
Related
I want to set autocomplete text in my application. The suggestion values will fetch from the database. But for a trial I made a string array for the suggestion values. It works fine when I implement it on an activity but not working in fragment.
In place of getActivity function I tried:
1) getContext()
2) this.getActivity()
3) (Search_Bus)getActivity()
but none work...
it's giving me error:
Attempt to invoke virtual method on a null object reference
Here's my code...
public class Bus_Schedule_tab3 extends Fragment {
AutoCompleteTextView autoCompleteTextView;
ArrayList<String> array;
ArrayAdapter<String> adapter;
String[] stop_names;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.bus_schedule_tab3, container, false);
stop_names = new String[] {"usa","china","russia","bangladesh"};
autoCompleteTextView = (AutoCompleteTextView)rootView.findViewById(R.id.autoCompleteTextView2);
adapter = new ArrayAdapter<String>(getActivity(),R.layout.support_simple_spinner_dropdown_item,stop_names);
autoCompleteTextView.setAdapter(adapter);
return rootView;
}
Its working for me...You missed to set threshold
AutoCompleteTextView autoCompleteTextView;
ArrayAdapter<String> adapter;
String[] stop_names;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_test, container, false);
stop_names = new String[]{"usa", "china", "russia", "bangladesh"};
autoCompleteTextView = rootView.findViewById(R.id.autoCompleteTextView2);
adapter = new ArrayAdapter<>(getActivity(), R.layout.support_simple_spinner_dropdown_item, stop_names);
autoCompleteTextView.setThreshold(1);
autoCompleteTextView.setAdapter(adapter);
return rootView;
}
Set in your xml file android:completionThreshold.
<AutoCompleteTextView
android:id="#+id/id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:completionThreshold="1"
android:layout_weight="1"/>
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);
}
My listview gets filled on the start but after i cant find a way to make changes
pls see the code bellow
public class myFragment extends android.support.v4.app.Fragment
{
ListView list;
ArrayList<String> restorants=new ArrayList<String>();
Manager m=new Manager();
ArrayAdapter<String> adapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_, container, false);
list=(ListView) rootView.findViewById(R.id.myList);
for(Restorant r : m.getResByCity("nyc"))
{
restorants.add(r.getName());
}
adapter=new ArrayAdapter<String>(getActivity(), R.layout.item_list, R.id.restorantItem,restorants);
list.setAdapter(adapter);
return rootView;
}
public void makeChange(String s)
{
restorants.remove(0);
((ArrayAdapter)list.getAdapter()).notifyDataSetChanged();
}
}
While calling the method makeChange() i have checked that the restorants items are removing so that there is no problem with calling the method
makeChange() is called from my mainActivity that contains viewpager where this fragment is contained in the viewpager.
you need to remove the element from the dataset you provided to the adapter,
adapter.remove(s)
Here you can find the documentation
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
I want a custom row, so I am using a List View in an xml and inflating into a fragment. I am very confused of how to set the adapter for the list View.
I created a new adapter which extends Base Adapter. In the getView method, I really don't know what context to pass while inflating the row.xml layout.
How do I set the adapter for the list view and where?
public class ResultsFragment extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.results_layout, container, false);
listView = (ListView)v.findViewById(R.id.results);
return v;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
loadPage(dataBean.getWhat(), dataBean.getWhere(), dataBean.getPageStart());
//resultsAdapter.setRssData(rssData);
//setListAdapter(resultsAdapter);
}
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Context context = getActivity().getApplicationContext();
resultsAdapter = new ResultsAdapter(context);
}
/**
* Set List Adapter
*/
private void setAdapter(){
if(listView.getAdapter() == null){
listView.setAdapter(resultsAdapter);
}
else{
resultsAdapter.notifyDataSetChanged();
}
}
}
You must extend Listfragment (instead of Fragment), and using its ListFragment.setListAdapter to set your adapter. In the adapter getView() inflate your row.. that s all
If you do not want to change your extended class, you should use listview.setAdapter(...) method. As you see in my example :
ListView productList= (ListView) getActivity().findViewById(R.id.product_list);
SampleAdapter adapter = new SampleAdapter(getActivity());
adapter.add(new SampleItem(
"Sunny LCD TV 2\" SN022L66-T1 Full HD",
R.drawable.product_sample_pic);
productList.setAdapter(adapter);