update fragment when taskadapter is empty - android

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){
.....
}

Related

View inside of RecyclerView is returning null

I am trying to set an onClickListener to my CheckBox but my checkBox instance is returning null on the onClickListener giving me a nullPointerException. I have looked at other questions similar to mine and haven't found anything that worked. I'm not too familiar with RecyclerView so I would not be surprised if it has something to do with how I'm initializing the view.
Here is the code in question-
Months0Through6.java
public class Months0Through6 extends android.support.v4.app.Fragment {
public Months0Through6() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
CheckBox checkBox;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_blank, container, false);
//This is returning null
checkBox = (CheckBox)rootView.findViewById(R.id.checkBox_ID);
RecyclerView rv = (RecyclerView) rootView.findViewById(R.id.rv_recycler_view);
rv.setHasFixedSize(true);
MilestonesAdapter adapter = new MilestonesAdapter(new String[]{"Month 0 stuff", "Example Two", "Example Three", "Example Four", "Example Five" , "Example Six" , "Example Seven"});
rv.setAdapter(adapter);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
rv.setLayoutManager(llm);
checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(),
"Your Message", Toast.LENGTH_LONG).show();
}
});
return rootView;
}
card_item.xml
<?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="68dp">
<android.support.v7.widget.CardView
android:id="#+id/card_view"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_margin="10dp"
android:layout_height="62dp"
card_view:cardCornerRadius="4dp"
card_view:elevation="14dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:id="#+id/checkBox_ID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/tv_text"
android:layout_toRightOf ="#+id/checkBox_ID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
</TextView>
<TextView
android:id="#+id/tv_blah"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Another Recyclerview Fragment"
android:layout_below="#+id/tv_text"
android:layout_toRightOf="#+id/checkBox_ID"
android:layout_toEndOf="#+id/checkBox_ID">
</TextView>
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
Your checkbox is inside card_item layout. But you are inflating fragment_blank and trying to map checkbox to that layout.
You could map checkbox inside view holder and set onclick listener inside onBindViewHolder of adapter class
You should findViewById in your MilestonesAdapter, MilestonesAdapter is responsible for creating items for the RecyclerView.
See the guide from Android Developers site.
Check the stuff about onBindViewHolder.

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.

Why does onScrollListener think ListView is not created?

So the error that is thrown is
java.lang.IllegalStateException: Content view not yet created
This is thrown when I uncomment the getListView().setOnScrollListener(this); line.
public class MyCategoryFragment extends ListFragment implements OnScrollListener {
private ArrayList<Article> m_articles = new ArrayList<Article>();
public ArticleAdapter m_artadapter;
public MyCategoryFragment() {
super();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_category, container, false);
m_articles = manager.loadCategory(getActionBar().getTitle().toString(), 10);
m_artadapter = new ArticleAdapter(this.getActivity(), R.layout.article_button, m_articles);
this.setListAdapter(m_artadapter);
//getListView().setOnScrollListener(this);
return rootView;
}
Here is the layout just so you can see it is the correct listview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<TextView
android:id="#android:id/empty"
android:textColor="#FFFFFF"
android:textSize="30sp"
android:gravity="center_vertical|center_horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="It's not you it's us..."/>
</LinearLayout>
I have researched this extensively and I believe in must be something so simple but it is taking more than a day to fix this. Thanks
as the onCreateView doc stays:
creates and returns the view hierarchy associated with the fragment
so since the method does not return, you will are not able to access the ListView through getListView(). You can obtain a valid reference in the OnActivityCreate callback.

Android, findViewById returns null

I'm writing some code for Android, the matter is that, when I call findViewById it returns me null, and I cannot understand why! I've been wasting my brain since yesterday, but I cannot figure out the solution. The goal is to set a layout as a header for a listView. Here is my code, respectively my header and my page:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/header"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp">
<TextView
android:text="#string/bydistance"
android:layout_gravity="left"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textSize="13dp"
android:gravity="center_horizontal"/>
<TextView
android:text="#string/byactivity"
android:layout_gravity="right"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textSize="13dp"
android:gravity="center_horizontal"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000"
>
<ListView
android:id="#+id/parkList"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:divider="#android:color/transparent"
android:dividerHeight="5.0dp">
</ListView>
</LinearLayout>
And here my code where I call the addheaderView:
public class NearMeFragment extends Fragment implements View.OnClickListener{
private FragmentManager fragmentManager;
#Override
public void onCreate(Bundle savedBundle){
super.onCreate(savedBundle);
}
#Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedBundle) {
View v = inflater.inflate(R.layout.park_list, container, false);
//TODO remove
make_test(v, container);
return v;
}
public void openTest(View view){
new LiveComment().show(getChildFragmentManager(), "dialog");
}
public void onClick(View view){
if (fragmentManager == null)
fragmentManager = getChildFragmentManager();
//if (view.getId() == R.id.test){
fragmentManager.beginTransaction().add(new LiveComment(), "livecomment").commit();
}
private void make_test(View v, ViewGroup container) {
ListView listView = (ListView) v.findViewById(R.id.parkList);
Park[] list = new Park[3];
list[0] = new Park("parco a", "acquasparta", false, false, 1, 2, 3);
list[1]=new Park("parco b", "perugia", false, false, 1, 2, 3);
list[2]=new Park("parco b", "perugia", false, false, 1, 2, 3);
ParkListAdapter adapter = new ParkListAdapter(v.getContext(), R.layout.small_park_container,list);
LinearLayout header = (LinearLayout) container.findViewById(R.id.header);
listView.addHeaderView(header);
listView.setAdapter(adapter);
}
}
The error is given at
listView.addHeaderView(header)
R.id.header is inside v not inside container.
Change
LinearLayout header = (LinearLayout) container.findViewById(R.id.header);
with
LinearLayout header = (LinearLayout) v.findViewById(R.id.header);
about the ViewGroup container to doc says:
If non-null, this is the parent view that the fragment's UI should be
attached to. The fragment should not add the view itself, but this can
be used to generate the LayoutParams of the view.
Edit: Since your header view is in another Layout you have to inflate it too:
#Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedBundle) {
View v = inflater.inflate(R.layout.park_list, container, false);
View header = inflater.inflate(R.layout.headerlayout, null);
//TODO remove
make_test(v, header);
return v;
}
and
private void make_test(View v, View header) {
// your code
// you have to remove LinearLayout header = (LinearLayout) container.findViewById(R.id.header);
listView.addHeaderView(header);
listView.setAdapter(adapter);
}

XML layout has a ListView - But still getting ListView id not found exception

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

Categories

Resources