ParseQueryAdapter for ListFragment - android

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

Related

How to display a message when listview is empty?

I am new to android programming. Here is my code. Kindly help.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
ListView view = (ListView) inflater.inflate(R.layout.fragment_alarms,container, false);
String[] titles = getResources().getStringArray(R.array.drawer_menu_list);
view.setAdapter(new ArrayAdapter<String>(getActivity(),
R.layout.drawer_list_item, titles));
view.setAdapter(mAdapter.setCurrentSeverityAlarm(0));
mListView = view;
mAdapter.refresh(mSpinnerAdapter);
return view;
}
xml file:
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list_alarms"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/alarm_list_vertical_margin"
android:paddingLeft="#dimen/alarm_list_horizontal_margin"
android:paddingRight="#dimen/alarm_list_horizontal_margin"
android:paddingTop="#dimen/alarm_list_vertical_margin"
android:divider="#android:color/transparent"
android:dividerHeight="#dimen/alarm_list_divider_height"
android:scrollbars="none">
</ListView>
ListView has a setEmptyView(View) method. The View you pass into this method will automatically be shown if your list is empty.
The easiest way to do this is to include the empty View in your layout. Your XML and Java will end up looking something like this:
XML
<ListView ... />
<TextView
android:id="#+id/empty_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Nothing to show" />
Java
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.fragment_alarms,container, false);
ListView view = (ListView) layout.findViewById(R.id.list_arlams);
View emptyView = layout.findViewById(R.id.empty_view);
// ...
view.setEmptyView(emptyView);
return layout;
}
You can of course also programmatically instantiate a View if you want to keep it out of your XML, or you could use a separate layout file for your empty View.

Fragment Class with ListView?

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.

ListView not appearing in fragment

So I have two Fragments inside an Activity using a PageAdapter.
One of the Fragments is a Listview and the other is a GridView of images.
The fragments are created successfully but the ListView is empty
#Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
View view = inflater.inflate(R.layout.eventlist ,container, false);
String[] x = new String[]{"AAA","BBB","CCC","AAA","BBB","CCC","AAA","BBB","CCC","AAA","BBB","CCC","AAA","BBB","CCC","AAA","BBB","CCC"};
ListView listView = (ListView) view.findViewById(R.id.listView);
ArrayAdapter<String> test = new ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.simple_list_item_1,x);
listView.setAdapter(test);
return view;
}
XML Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<ListView
android:id="#+id/listView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
You should not check null for listview. But rather check if the list is empty.
Change it to listView.getChildCount()==0.
But I think you dont need to check because it will always be empty because it's a new created view. And no items on the list. If you want to check correctly, put the condition check after you set the adapter to the list.
Check if this works.

How to find listview in 'fragment'

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

Add static header to listFragment in onCreateView

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)

Categories

Resources