setEmptyView not working in custom ListView - android

I have a custom ListView which is one of the tabs of a viewPager.
public class MyListView extends ListView
I instantiate an object of my class and setEmptyView using
ListView myList = new MyListView(context);
View emptyView = myLayoutInflater().inflate(R.layout.mylist_empty_view, null);
myList.setEmptyView(emptyView);
myList.setAdapter(myAdapter);
However the emptyView never shows up, does not have a parent (which ideally should be the listview) and I cannot see it in the hierarchyViewer. What do I need to do?

AdapterView only manages the visibility of emptyView. If the adapter is empty or null, the visibility of the ListView will be GONE while emptyView is VISIBLE, but to make it show up, you need to add it to the layout yourself.
Just try using this layout
FrameLayout myLayout = new FrameLayout(context);
myLayout.addView(myList);
myLayout.addView(emptyView);
instead of myList directly.

Related

ListView addView(View child, int index) not working

I'm trying to add a custom view to my listview (which is already populated) using this code:
listview.addView(customView,rowNumber);
but my app crashes..
I realized that if I use this code :
list.addFooterView(customView);
it works, but I can't choose where to place the view . What can I do?
The addFooterView works because it adds on the bottom. You can't add anything in the Listview just putting it using addView. Instead, do the following example:
After doing it:
ArrayList<Object> _views = new ArrayList<Object>();
_views.add(new View());
_views.add(new View2());
ArrayAdapter<Object> _adapter = new ArrayAdapter<Object>(getActivity(), android.R.layout.simple_list_item_1, _views)
mListView.setAdapter(_adapter);
You only need to add the new View into ArrayList and notify the adapter that the listview had changed such as:
_views.add(new View3());
_adapter.notifyDataSetChanged();

How to use the standard functions after modification ListView

I use this Pull-to-Refresh, it works, but after the installation can not call standard functions for listView, such as:
public static RefreshableListView lv = (RefreshableListView) findViewById(R.id.messageListView); //this work
lv.setOnItemLongClickListener(longClickListener); //don`t work
lv.setSelection(listViewCountPosition); //don`t work
Parcelable state = lv.onSaveInstanceState(); //don`t work
lv.onRestoreInstanceState(state); //don`t work
Compiler writes error.
RefreshableListView implements FrameLayout ( not listview). Listview is added as a child to the FrameLayout. You have to call getListView() for list view instance.
something like this..
public RefreshableListView lv = (RefreshableListView) findViewById(R.id.messageListView);
lv.getListView().setOnItemLongClickListener(longClickListener);

I want to dynamically change the color of the text view inside list view

I have this layout in which I have a listview. The content of list is defined in another layout.I have text views inside this layout and I want to dynamically change the color of the text view depending on some condition(like simple if else).Please let me know how to do this.
Here is my code-:
ListAdapter adapter = new SimpleAdapter(this, menuItems,
R.layout.recentlist,
new String[] { KEY_CAT, KEY_DATE, KEY_TID, KEY_AMO, KEY_DEB,KEY_CUR,KEY_BAL,KEY_FEES}, new int[] {
R.id.textView1,R.id.textView2, R.id.textView3, R.id.textView4,R.id.textView5,R.id.textView7,R.id.textView6,R.id.textView8});
setListAdapter(adapter);
This particular layout I have added from another xml file to put the contents of listview in this.So I cant setTextColor by taking textView because it takes the id of this class's layout and not this layout.Please help me regarding this.
AFAIK you cant use a SimpleAdapter to perform this operation as SimpleAdapter only copies data from data set to TextView in your ListView. In order to handle color changes at run-time you should define a CustomArrayAdapter.
Override getView method of SimpleAdapter by extending it to your own class. Inside getView method, you can use View.findViewById and then do whatever you want to do with your TextView
TextView tv1 = (TextView) vi.findViewById(R.id.your_textview_id);
// where vi is convertview
tv1.setTextColor(Color.BLUE);

Adapter class cast exception when removing a Footer view?

I have an exception I never thought I would see. A class cast exception of the adapter when removing a footer view from a ListView (sic).
java.lang.ClassCastException: com.test.MyAdapter
at android.widget.ListView.removeFooterView(ListView.java:381)
How can this happen? What does removing a footer have to do with class cast exception????
The list is a multi-list adapter perhaps that is why but still a class cast exception for removing a footer (sic).
Add your footer view to ListView before calling setAdapter() method.
Added:
public void addFooterView (View v)
Since: API Level 1
Add a fixed view to appear at the bottom of the list.
If addFooterView is called more than once, the views will appear in the order they were added. Views added using this call can take focus if they want.
NOTE: Call this before calling setAdapter. This is so ListView can wrap the supplied cursor with one that will also account for header and footer views.
Parameters
v The view to add.
Source
Also you can check this interesting post.
Hope this helps.
This is some code for the answer above, it worked in my case:
I had to set a footerView (it's a loadingView in a listView with pagination) to my listView before setting it's adapter and then remove it. First I initialized my loadingView from a layout file in OnCreate method:
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
loadingView = layoutInflater.inflate(R.layout.loading_view, null);
Then I used this workaround in the same method:
this.setIsLoading(true);
listView.setAdapter(adapter);
this.setIsLoading(false);
Where
private void setIsLoading(boolean isLoading)
{
this.isLoading = isLoading;
if (isLoading) {
listView.addFooterView(loadingView);
}
else {
listView.removeFooterView(loadingView);
}
}
The problem does not come from removeFooterView(), but from addFooterView().
If you read the documentation, it states that an wrapper will be added to your adapter:
If the ListView's adapter does not extend HeaderViewListAdapter, it will be wrapped with a supporting instance of WrapperListAdapter.
Thus you must use the getter for retrieving the wrapped adapter and cast it to your adapter. Like this:
((MyAdapter) ((HeaderViewListAdapter) listView.getAdapter()).getWrappedAdapter())
Hope this will help you with your issue.
Best regards,
Adding to the other answers, if you're adding/removing footers dynamically (such as if they reach the bottom of your list and then you're adding a footer view) the easiest thing is to Override setAdapter in your ListView and add a new View object as the footer there, this will ensure the adapter is wrapped in the HeaderViewListAdapter:
#Override
public void setAdapter(ListAdapter adapter) {
addFooterView(new View(getContext()));
super.setAdapter(adapter);
}

How to change elements of an Adapter from outside?

I have a gallery with a custom adapter. I'm showing an image along with some texts and buttons.
I want to change the texts that are associated with the images when the corresponding buttons are clicked.
If I put the button handler inside the adapter, it doesn't catch the call.
I found out that I should moved the handler to the activity.
But the problem is that I don't have access to the Adapter anymore. Any solutions?
public class MyAdapter extends BaseAdapter { ...
MyGallery myGallery = (MyGallery) findViewById(R.id.mygallery);
myGallery.setAdapter(new MyAdapter(this, ...));
You should set the OnClickListener in the getView function of the adapter. When you inflate the view you can make a call to findViewById on the view to get a reference to the Button. You can then set the OnClickListener on the Button object itself.
Move
MyGallery myGallery = (MyGallery) findViewById(R.id.mygallery);
myGallery.setAdapter(new MyAdapter(this, ...));
into your Activity
Oh yeah and make a member reference to the Adapter eg MyAdapter myAdapter = new MyAdapter(....

Categories

Resources