I am having a fragment that send request to server and download json , then parse it into a listview , somehow i can't reach the view properly.below is some part of my apps , this is my first touch on listview with fragment , for some reason i only allow to use Fragment so i choose 'setAdapter' instead of 'setListAdapter' to be my adapter
myfragment.java
public class gallery extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View myFragmentView = inflater.inflate(R.layout.tab_frag2_layout,
container, false);
return myFragmentView;
}
public void onActivityCreated(Bundle savedInstanceState) {
.........
.........
.........
setAdapter(colorAdapter);
}
layout.tab_frag2_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff">
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/list_padding"
android:paddingRight="#dimen/list_padding"
android:tag="listf" />
</LinearLayout>
My expectation is using
MyListView = (ListView)myFragmentView.findViewById(R.id.list);
to get my view , but it doesn't work that way...
Now I just want to get my listview and apply
MyListView.setAdapter(colorAdapter) to it
any help would be appreciated, thanks
Option1: Since your fragment layout consists of just a simple ListView.
Consider using
public class gallery extends ListFragment
Then you can use this code to get your listview.
myFragmentView.getListView()
Option2: As Nickolaus mentioned, you need a custom id if you want to use findViewById.
android:id="#+id/listview1"
If you don't want to use a ListFragment, you need to add a custom id (android:id="#+id/custom_id") then you can use findViewById to find the ListView
Related
I'm trying to develop a sign-up menu for a social app, that I'm working on. I would like the sign-up menu to consist of a PageViewer, which holds five fragments. The last three fragments contains a ListView, where the user can 'check' information about them selves. The XML is here:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:minHeight="25px"
android:id="#+id/layoutSignupLists">
<TextView
android:text="Add something here"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:id="#+id/signupListDescription" />
<ListView
android:id="#+id/interestListView"
android:layout_below="#id/signupListDescription"
android:layout_height="match_parent"
android:layout_width="match_parent" />
</RelativeLayout>
This layout is inflated, when the last three fragments are created as is displayed correctly. I have subscribed a delegate to the itemSelected event in the ListView as seen below:
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
//Inflate view and find content
View view = inflater.Inflate(Resource.Layout.signupFragLayout4, container, false);
interestListView = view.FindViewById <ListView>(Resource.Id.interestListView);
var desc = view.FindViewById<TextView>(Resource.Id.signupListDescription);
//Adds the description
desc.Text = GetString(Resource.String.profile_menu_edit_interests);
//Populate ListView
interestListView.Adapter = new ArrayAdapter<string>(Activity,
Resource.Layout.CheckedListViewItem, MainActivity.InfoNames[(int)InfoType.Interest]);
interestListView.ChoiceMode = ChoiceMode.Multiple;
interestListView.ItemSelected += (object sender, AdapterView.ItemSelectedEventArgs e) =>
{
if(!Interests.Contains(e.Position))
Interests.Add(e.Position);
else
Interests.Remove(e.Position);
};
return view;
}
When putting a break-point in the delegate I find that it's never called and thus the ListView reset upon swiping right or left.
How can I make the fragment 'hold on' to the information so that it's displayed every time the fragment is shown?
Place the information in the MainActivity. You can make a simple reference to it by putting this variable in your fragment class:
MainActivity mainActivity;
And in your OnCreateView() place this to initialize it:
mainActivity = (MainActivity)Activity;
Now if you have any public variables in the mainActivity, you can reference them in the fragment like:
textView.Text = mainActivity.VariableName;
Additionally, for putting the information back in the fragment, override the 'OnResume' method like so:
public override void OnResume()
{
base.OnResume();
//Code to fill in all the information in your fragment again. I.E:
textView.Text = mainActivity.VariableName;
}
As ParseQueryAdapter is meant to be used with an Activity - what is the general design idea for using a ParseQueryAdapter for a ListFragment? I want to display a custom list view.
I've tried to use a ParseQueryAdapter with a ListFragment, but the images are not displayed properly. If I use the exact code with an Activity, the images load correctly.
How can you use a ParseQueryAdapterin conjunction with a ListFragment?
I've posted the general code, but the text from the query loads wonderfully in the list, the images however don't always load or when they do..take a LONG time to show up.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#android:id/list"
android:divider="#null"
android:dividerHeight="0px"
/>
</RelativeLayout>
public class PlacesTab extends ListFragment {
...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.places_tab, container, false);
mainAdapter = new ParseQueryAdapter<ParseObject>(getActivity(), “Places”);
mainAdapter.setTextKey("name");
mainAdapter.setImageKey(“placeImage");
listView = (ListView) rootView.findViewById(android.R.id.list);
listView.setAdapter(mainAdapter);
mainAdapter.loadObjects();
return rootView;
}
}//end class
Is it possible to have custom ListView inside Fragment class?
This is my fragment class wherein the fragment_find_people is just an empty XML:
public class FindPeopleFragment extends Fragment {
public FindPeopleFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_find_people, container, false);
return rootView;
}
}
Now, I've already have a customListView but it only works with the Activity class. How can I transfer that to Fragment?
my listview_main xml
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/listmain" >
<ListView
android:id="#+id/listview"
android:layout_marginTop="10dp"
android:background="#drawable/bg"
android:divider="#9c9c9c"
android:dividerHeight="5dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent" /></RelativeLayout>
Class context replace with getActivity thats only difference and replace view not activity:
View rootView = inflater.inflate(R.layout.fragment_find_people, container, false);
//now you must initialize your list view
Listview listview =(Listview)rootView .findViewById(R.id.your_listview);
//EDITED Code
String[] items = new String[] {"Item 1", "Item 2", "Item 3"};
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, items);
listview.setAdapter(adapter);
//To have custom list view use this : you must define CustomeAdapter class
//listview.setadapter(new CustomeAdapter(getActivity()));
//getActivty is used instead of Context
return view;
Inside the onCreateView method first define your layout.
RelativeLayout rl=(RelativeLayout)inflater.inflate(R.layout.status,container, false);
list=(ListView) rl.findViewById(R.id.list1);
itemList = new ArrayList<HashMap<String, String>>();
// fill the defined arraylist using and asynctask or any other method
adapter=new Customadapt(context.getActivity(), itemList);
list.setAdapter(adapter);
return rl;
you can create your custom list view programmatically also. for ex:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return new CustomListView(container.getContext());
}
Inside onCreateView(...) method, call your CustomListAdapter like this
adapter = new CustomListAdapter(getActivity());
customListView.setAdapter(adapter);
And your CustomListAdapter as follows , e.g
private class CustomListAdapter extends BaseAdapter {
LayoutInflater inflater;
public Context context;
public CustomListAdapter(Context context) {
this.context = context;
inflater = LayoutInflater.from(this.context);
}
.......
.......
}
First off, yes, it is possible. Here is how.
As with all other resources, there are two ways to declare them. Programatically and via XML layout files. I'll explain using XML as it is what I use most often and I find best.
In your fragment_find_people XML, declare your layout as normal. In that layout, I presume at least a LinearLayout, you need to add a ListView:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_find_people_linear_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="#id/your_list_view_id"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
Now you can just instantiate that ListView using:
Listview listView =(ListView)rootView.findViewById(R.id.your_list_view_id);
In order to use the "same" ListView, I would recommend simply duplicating the code of the ListView from one layout file, the one for your Activity to the Fragment. You could also separate the declaration of the ListView into a separate XML, which would be tidier if you'd be to reuse it a lot, but for a small project, just copy it.
I see a lot of questions being asked in the stackoverflow on the same topic, but i could not understand the deep problem of the exception.
Below is my XML layout. I have a listView.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" android:background="#FFFFFF">
<ListView
android:id="#+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#656565"
android:paddingLeft="#dimen/list_padding"
android:paddingRight="#dimen/list_padding"
android:dividerHeight="20.0sp"
android:divider="#656565"/>
</LinearLayout>
Below is my Fragment class.
public class FragmentClass extends SherlockListFragment implements AsyncListner{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.documents, container, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState){
}
#Override
public void onLoadComplete(List<DocumentResponse> data) {
DocumentsAdapter adapter = new DocumentsAdapter(getSherlockActivity(),
R.id.list, data);
setListAdapter(adapter);
}
}
Once the onLoadComplete call is finished, i am trying to create my ListView. But i get this error. "Your content must have a ListView whose id attribute is android.R.id.list"
change android:id="#+id/list" to android:id="#android:id/list"
With this you are using the requested android.R.id.list id.
Don't forget to update the code, too
DocumentsAdapter adapter = new DocumentsAdapter(getSherlockActivity(), android.R.id.list, data);
How can I add a simple static header to my listview inside a listFragment? I want to create the header from an xml def and add it through inflation.
My onCreateView:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View detailList = inflater.inflate(R.layout.detail_fragment, container, false);
View detailListHeader = inflater.inflate(R.layout.daily_sales_header, null, false);
container.addView(detailListHeader, 0);
return detailList;
}
This creates the header, but it is not above the listview, rather the listview appears underneath the header, ie the header is overlaying the listview.
Any hints on the correct implementation?
Putting hackbod's description into code for you since his answer created more questions before the answers came. Sometimes I just want the fish. I don't always need to know how the net is made...
To start with, create a layout that looks like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myListViewWithHeader"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="6dp"
android:textStyle="bold"
android:textSize="20sp" />
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false" />
<TextView
android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Then, in your onCreateView method you do this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.myListViewWithHeader, null);
return view;
}
The header can now be populated by doing this:
// get the header view
TextView headerView = (TextView) getView().findViewById(R.id.header);
headerView.setText("Header text goes here");
Notice that my header is a TextView, but it can be replaced with another view if you like. In that case you will need to do a getView().findViewById(R.id.xxxxx) for each view inside the header you want to work with
You should NEVER EVER be adding views directly to the container in onCreateView(). Please read the documentation: http://developer.android.com/reference/android/app/Fragment.html#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
Also see the various sample code in the Fragment documentation, as well as the API demos: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/index.html
There is nothing special about using a Fragment here. Just build a view hierarchy containing a ListView like you normally would in an Activity or elsewhere. You always need to return one View from onCreateView; this is the root of your hierarchy.
For example you could make the ListView and then use this to add a header to it: http://developer.android.com/reference/android/widget/ListView.html#addHeaderView(android.view.View)