How to display a message when listview is empty? - android

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.

Related

update fragment when taskadapter is empty

I have a fragment that has a listView which has an adapater, in the adapter class, there are buttons that remove objects from the adapter and then the adapter calls notifyDataSetChanged(); In the event that the adapter is empty I want the fragment to know and do something, is there a simple way to do this?
u can set a textview in your fragment xml file.Like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/tV_empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_centerVertical="true"
android:text="No item yet"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ListView
android:id="#+id/lv_notebook"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#android:color/transparent" />
</RelativeLayout>
When the adapter is empty,you can use textView.setVisible(VISIBLE).( you can search name).When the adapter has some items,you can use textView.setVisible(...)
Well,you can do like this in your fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
// set up listview
if(mDataList.size()==0){
mListView.setVisibility(View.GONE);
mTextView.setVisibility(View.VISIBLE);
}else{
YourAdapter mAdapter = new YourAdapter(mDataList...)
mListView.setAdapter(mAdapter);
mListView.setVisibility(View.VISIBLE);
mTextView.setVisibility(View.GONE);
}
return rootView;
}
public void deleteItem(int position){
Data item = mDataList.get(position);
mDataList.remove(item);
if(mDataList.size()==0){
mTextView.setVisibility(View.INVISIBLE);
}else{
mAdapter.notifyDataChanged();
}
}
public void addItem(Data item){
.....
}

How to add a button at the end of a ListView in a Fragment?

I have a fragment activity which contains a ListView, and i need to add a button at the bottom of the list.
This is a part of the code:
public class listview1 extends Fragment implements OnItemClickListener {
ListView list;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstances){
View rootView = inflater.inflate(R.layout.activity_main, container, false);
list = (ListView) rootView.findViewById(R.id.listView);
list.setAdapter(new CustomBaseAdapter(this.getActivity()));
list.setOnItemClickListener(this);
return rootView;
}
- CustomBaseAdapter extends a BaseAdapter which has a getView() method and all...
just go to your xml file and type this code inside the
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
I personally tested it and found it working
The xml file is in res -> layout ->yourfilename.xml

ListView addHeaderView glitching around after onResuming Activity

In my ListFragment I want a headerView to make the app look like its website counterpart.
onCreateView():
View headerView = inflater.inflate(R.layout.header, null, false);
onStart():
if(headerView != null) {
getListView().addHeaderView(headerView);
}
After pausing and resuming the activity, the headerView has a large top margin is glitching around: https://www.youtube.com/watch?v=Q_zhr8w5Yzg
header.xml
<?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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/news_bubble"
android:paddingTop="5dp"
android:paddingRight="10dp"
android:paddingBottom="20dp"
android:paddingLeft="10dp"
android:layout_marginTop="10dp"
android:text="#string/str_header"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white" />
</LinearLayout>
Edit: This question is solved. If anybody stumbles upon this error, this is my code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment, container, false);
listView = (ListView)rootView.findViewById(android.R.id.list);
headerView = inflater.inflate(R.layout.header, listView, false);
listView.addHeaderView(headerView);
[...]
return rootView;
}
You need to make the list view as the container for your header view. So inflate the listView first and then inflate the header view as follows
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_layout,container,false);
ListView listView = (ListView)view.findViewById(R.id.my_list);
View headerView = inflater.inflate(R.layout.header, listView, false);
listView.addHeaderView(headerView);
return view;
}

ParseQueryAdapter for ListFragment

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

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.

Categories

Resources