ListView data not showing in fragment - android

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:|

Related

basic List View - Find by ID not working

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

Some1 know why my listview data is not changing?

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

spinner won't populate from my array [duplicate]

This question already exists:
Spinner will not populate and won't set arrayadapter
Closed 8 years ago.
My app redirects me to a different screen from my home with the tap of a button. Once, i reach my city screen my spinner is visible. But it doesn't contain any elements from my array. Nothing force closes or anything, but it doesn't work.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_city);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_city, container, false);
String[] test = {"test1", "test2"};
Spinner spin = (Spinner) v.findViewById(R.id.countries);
ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, test);
adapt.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapt);
return v;
}
You need to have the below.
public static PlaceHolderFragment extends Fragment
{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_city, container, false);
String[] test = {"test1", "test2"};
Spinner spin = (Spinner) v.findViewById(R.id.countries);
ArrayAdapter<String> adapt = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, test);
adapt.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapt);
return v;
}
}
Also use getActivity() to get the context of hosting activity.

ListView Not Updating After Calling notifyDataSetChanged, invalidateViews

I tried following the recommendations here (How to refresh Android listview?) to update my ListView, but to no avail.
The relevant code is below. Please note that I call runOnUiThread in my MainActivity to refresh the ListView of this (fragment).
protected ArrayAdapter<String> adapter;
protected View view;
protected ArrayList<String> theList;
protected ListView listView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
view = inflater.inflate(R.layout.fragment_1, container, false);
listView = (ListView) view.findViewById(R.id.listview);
theList = new ArrayList<String>();
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, theList);
listView.setAdapter(adapter);
if(view != null) { return view;}
((ViewGroup) listView.getParent()).removeView(listView);
container.addView(listView);
return view;
}
protected void updateListViewWithPath(String resultFromServerRead) {
//TODO: Somehow split the information..
theList.add("yo");
theList.add("hey");
adapter.clear();
adapter.addAll(theList);
listView.invalidateViews();
adapter.notifyDataSetChanged();
listView.refreshDrawableState();
}
You already have the reference to the arraylist list so you dont need to add it again to the listView just simply add the string to the arraylist and notifyDataSetChanged;
example:
protected void updateListViewWithPath(String resultFromServerRead) {
//TODO: Somehow split the information..
theList.add("yo");
theList.add("hey");
adapter.notifyDataSetChanged();
}

ListFragment content changing

I have a simple MyListFragment fragment which shows one simple array of data on the screen.
public class MyListFragment extends ListFragment {
private ArrayAdapter<String> adapter;
public ArrayAdapter<String> getAdapter() {
return adapter;
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String[] values = new String[] {"data", "data", "data"};
adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return super.onCreateView(inflater, container, savedInstanceState);
}
}
I wonder how can I change this data dynamically from my Activity? Please tell me if there is a simple way how to do this! I've tried but have failed...
UPD: I added a function changeContent to MyListFragment:
public void changeContent(String[] value) {
adapter.addAll(value);
adapter.notifyDataSetChanged();
}
}
As you see, adapter is a global variable and it's initialized inside onActivityCreated, but my program crashes because inside changeContent in equals null! I can't get why!
You could write a function that changes your values variable and then once you have filled it with your new data just call
adapter.notifyDataSetChanged();
to fill your list with the new data.

Categories

Resources