Android ListView in Fragment - android

I am trying to create a list of items which contains an image and some description for the image in each individual. After which, the list will be place in a fragment inside the application. Can anyone guide me to create it? I am not too sure how can I do it without the ListActivity.

It seems like your Fragment should subclass ListFragment (not ListActivity). onCreateView() from ListFragment will return a ListView you can then populate.
Here's more information on populating lists from the developer guide: Hello, ListView

Here's a code snippet to help you display ListView in a fragment.:
public class MessagesFragment extends ListFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_messages, container,
false);
String[] values = new String[] { "Message1", "Message2", "Message3" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
return rootView;
}
}
And, this is how the xml would look like:
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
If you do not use 'list' as ID or do not include a ListView into your layout, the application crashes once you try to display the activity or the fragment.
Reference: http://www.vogella.com/tutorials/AndroidListView/article.html#listfragments

Related

getListView is missing

I have created a ListView to list all the activities but when I wanna call the method, I can't find getListView
My activity_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/ListActivity"
android:layout_width="wrap_content"
android:layout_height="match_parent">
</ListView>
</RelativeLayout>
and in my MainActivity.java seems like there's an error with
getListView
method, I've no idea why
You can use getListView() when working with ListActivity not the usual Activity. In your code you got it wrong and made the ListView with id "a special identifier to the view" ListActivity, so to access it in your code you simply
ListView lv = (ListView) findViewById(R.id.ListActivity);
Your activity must inherit from a ListActivity. Your class should start with
public class MyListAdapter extends ListActivity {
...
}
Then you can use the method
getListView()
This is the official documentation
Second solution
If you use a fragment, then you can access your ListView by using
ListView lv = (ListView) rootView.findViewById(R.id.id_of_your_listView);
The rootView is the view you get in your onCreateView Method of the Fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.example_fragment, container, false);
return rootView;
}
Try using (ListView)findViewById(R.id.ListActivity) instead of getListView()
If you have the ListView in the fragment you can use:
ListView list = (ListView)findViewById(android.R.id.list);
But if you want to use getListView directly you must call the ListView's id as
android:id="#android:id/list"
Hope this help.
also note that you dont pass your ListView to the adapter but you pass the adapter to the ListView
ListView list = (ListView)findViewById(R.id.ListActivity);
list.setAdapter(adapter);
You can't extend ListActivity because 1) You are wanting to use Fragments and 2) there isn't a method setSupportActionBar for the ListActivity class...
Anyways... in your question
My activity_fragment.xml
<!-- XML code -->
You can't use findViewById to get that ListView within the Activity class because your ListView isn't within activity_main.xml.
You have to do it from within the Fragment.
public class YourFragment extends Fragment {
private ListView yourListView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_fragment, container, false);
this.yourListView = (ListView) v.findViewById(R.id.listView);
// adapter stuff...
return v;
}
}

Unable to add window -- token null is not for an application from ListFragment

I have this ListActivity that is populated by json data from web server, but suddenly I want to try to use ViewPager on my app.
It seems like ListActivity and ListFragment has almost the same function. So what I did was to copy-paste the codes to ListFragment, I've change a little bit of code to get rid of errors though.
What I did was to put all the codes from onCreate(ListActivity) to onCreateView(ListFragment) like this:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_movies, container, false);
examinationList = new ArrayList<HashMap<String, String>>();
ListView lv = getListView();
new GetContacts().execute();
return rootView;
}
That created an Content view not yet created exception. So what I did was create an onViewCreated method and remove all the unnecessary codes from onCreateView and transfer it to onViewCreated
#Override
public void onViewCreated (View view, Bundle savedInstanceState) {
examinationList = new ArrayList<HashMap<String, String>>();
ListView lv = getListView();
new GetContacts().execute();
}
that removed the Content view not yet created exception but now another exception appeared and I can't solve it.
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
EDITED
fragment_movies.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<!-- Main ListView
Always give id value as list(#android:id/list)
-->
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Solve it! The problem was on the showing of the ProgressDialog on onPreExecute, it seems like getting the context on normal activity is different from getting the context on fragments.

Tabs with Listview in fragments

I am beginner in android.
I was having a requirement similar to what is discussed here:
Separate Back Stack for each tab in Android using Fragments.
Now, I am using that project given here gitHub
What I want is to add listview to a fragment..Further on clicking a particular item in list another fragment is visible.
But I am facing a problem in doing it.
For eg here,I get error in using setListAdapter(adapter) function.
Plz help.
public class AppTabAFirstFragment extends BaseFragment {
private Button mGotoButton;
//private String[] characters= {"shs","sds","sdss","sdsd"};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.app_tab_a_first_screen, container, false);
mGotoButton = (Button) view.findViewById(R.id.id_next_tab_a_button);
mGotoButton.setOnClickListener(listener);
// ArrayAdapter<String> adapter = new ArrayAdapter<String>(inflater.getContext(),R.layout.app_tab_d_first_screen, characters);
// setListAdapter(adapter);
return view;
}
In this way,it doesn't allow us to use setListAdapter() function reason being I can't extend ListActivity. I am all confused.
Kindly help.
I haven't checked your links, but as I can see from the little code you posted I think this might be the problem:
You need to lookup your listview from the xml file (if you have defined it there) and set the adapter to that object. For instance:
ListView listView = (ListView) view.findViewById(R.id.my_list_view);
listView.setAdapter(adapter);

combining paging and list of android in one view

I am new in programming to android now my work is paused as I am not getting how to accomplish the following I want to create a view which will have paging capability and in the view there would be textview to display the name of person and a list of name of all his relatives .... And the list can be long so it should be scrollable... I know to do paging and list independently but do not know how to combine the both in one view.... I hope I am clear with my question... So guys help me how to accomplish this...
I know to do paging and list independently but do not know how to combine the both in one view
So you can create xml like this :
Linear Layout/ Relative layout
Text View
List View
Put this XML in fragment
EDIT :
public class Fragment1test extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
//This layout contains your list view
View view = inflater.inflate(R.layout.your_view_xml, container, false);
//now you must initialize your list view
Listview listview =(Listview)view.findViewById(R.id.your_listview);
//Here items must be a List<Items> according to your class instead of String[] array
ListAdapter listadapter = new ListAdapter(getActivity(), android.R.layout.simple_list_item_1, items)
ListView.setAdapter( listAdapter());
//getActivty is used instead of Context
return view;
}
}

onListItemClick not called in ListFragment

I have a ListFragment which contains a list however onListItemClick is never called. I am not using getListView() which I suspect is the problem. I am pulling my list view from the xml as such:
list = (ListView) getActivity().findViewById(android.R.id.list);
And then setting the adapter like this:
list.setAdapter(new CustomAdapter(getActivity(), R.layout.title, mCursor, new String[]{"title"}, new int[]{R.id.my_title}) );
Since I need to set the adapter on the list, I do not use setListAdapter() either. Is it not possible to pull the list from the xml and use onListItemClick? I would like to keep my list view in the xml so I do not have to set all of the properties programmatically.
If this is not possible, how can I select items in my list?
Thanks
OK. Try this answer. Keep your layout file as it is. Override the following method in your FragmentList class:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
return inflater.inflate(R.layout.your_fragment_layout, container, false);
}
Make sure that your layout define a ListView with the right id, as follows:
<ListView android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
Then, just call FragmentList::setListAdapter:
setListAdapter(new CustomAdapter(getActivity(), R.layout.title, mCursor, new String[]{"title"}, new int[]{R.id.my_title}) );
Should work, ticky-boo.

Categories

Resources