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();
Related
I am dynamically adding items to my listview my code works fine but my problem is when the listview is updated it is going to the starting position (items are added but scroll view begins from initial position).I am using listview inside fragment.I want to avoid that scrolling to initial position.
CODE
ListAdapter adapter =
new SimpleAdapter(getContext(), productsList, R.layout.list_notify, new String[]{"id","title","des"},
new int[]{R.id.id, R.id.title,R.id.des});
lv.setAdapter(adapter);
lv.invalidateViews();
Reference : How to refresh Android listview?
ListView Refresh in Android
Refresh Listview in android
Android refresh listview in fragment
How to refresh Android listview?
ListView is officially legacy. Try to use RecyclerView then you will be able to tell that you don't update whole list with methods like notifyItemChanged(position)...
In your case you will call notifyItemRangeInserted(position, count)
https://developer.android.com/guide/topics/ui/layout/recyclerview
Try smoothScrollToPosition on your listview.
See this, pretty similar if I understand correct what you want.
So in order to get that scrolling to stop you basically have to block the listview from laying out its children so first off you have to create a custom listview something like
public class BlockingListView extends ListView {
private boolean mBlockLayoutChildren;
public BlockingListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setBlockLayoutChildren(boolean block) {
mBlockLayoutChildren = block;
}
#Override
protected void layoutChildren() {
if (!mBlockLayoutChildren) {
super.layoutChildren();
}
}
}
then you can use it like this for example
int firstVisPos = mListView.getFirstVisiblePosition();
View firstVisView = mListView.getChildAt(0);
int top = firstVisView != null ? firstVisView.getTop() : 0;
// Block children layout for now
mListView.setBlockLayoutChildren(true);
// Number of items added before the first visible item
int itemsAddedBeforeFirstVisible = ...;
// Change the cursor, or call notifyDataSetChanged() if not using a Cursor
mAdapter.swapCursor(...);
// Let ListView start laying out children again
mListView.setBlockLayoutChildren(false);
// Call setSelectionFromTop to change the ListView position
mListView.setSelectionFromTop(firstVisPos + itemsAddedBeforeFirstVisible, top);
the setBlockLayoutChildren being true is what will stop your listview from scrolling and of course you can set whatever else you would like it to do
you may also just want to look into recyclerview though may make your life easier
I used a gridview instead of my listview and it solved my problem
set 1 item per row can act as listview in my code
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/grid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
**android:numColumns="1"**
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:scrollbarStyle="outsideOverlay"
android:verticalScrollbarPosition="right"
android:scrollbars="vertical">
reference : Custom layout as an item for a grid view
I have a list of items: List<SomeObject> items that is mapped to an adapter to show on a ListView. When I set a new field for a specific item in that list and call adapter.notifyDataSetChanged() then list doesn't update. For example, I want to show a date TextView in a specific row in the ListView by setting
item.get(position).showDate(true);
adapter.notifyDataSetChanged();
The view doesn't show until I scroll away from that row and back to it (I'm assuming because of recycling).
Doing list.setAdapter(adapter); works but the entire view flashes and there shouldn't be a reason to re-set the adapter.
How can I get the list to update without scrolling?
You could try to change the List that is referenced in the adapter. Add something like this to your adapter:
public void updateData(List<SomeObject> dataItems) {
this.mDataItems = dataItems;
notifyDataSetChanged();
}
And then:
items.get(position).showDate(true);
adapter.updateData(items);
you can use adapter.getitem(postion) to get SomeObject and refresh it.
SomeObject sobj = (SomeObject)adapter.getItem(position);
sobj .showDate(true);
adapter.notifyDataSetChanged();
Hi friends i have a listview and the contents are fetched from a webservice call. In that webservice call, there are fields like
"OGType": "ORG" and "OGType": "GROUP"
If click a button, the listview must shows the item having "OGType": "ORG", and hide the item having "OGType": "GROUP". Hope you understand what i meant. Please anyone help me for that. Thanks in Advance.
Try to set new data (only with ORG) to adapter and then call
adapter.notifyDataSetChanged();
You can do it in your getView Method in your Adapter Class. That's the header
public View getView(int pos, View convertView, ViewGroup, parent)
There you can properly hide the element(s) you want, you know, using the method setVisibility()
For more help you can take a look here
You can create a custom adapter and pass data to it in the form of Array or ArrayList (ArrayList is better when dealing with Custom Adapters). Whenever you need to add or remove the data from ListView, just add or remove the item to or from you ArrayList and call notifyDataSetChanged() on your custom adapter and it will update the ListView automatically.
In your case, whenever you click a button, edit you ArrayList and call your custom adapter's method called notifyDataSetChanged() and that's it. You'll see every time you call this method ListView will refresh itself if you have made any changes to the data. Hope it helps.
NOTE - CUSTOM ADAPTER IS NOT COMPULSORY. ANY ADAPTER CAN BE USED e.g SimpleAdapter, ArrayAdapter etc.
You can use a visible list and filters lists. You should use "visible" for complete the BaseAdpter as always, then, you can change the pointer of visible to other list (all, filter...)
Don't worry by the memory, are pointers, you only have each element only once.
public class MyAdapter extends BaseAdapter {
private ArrayList<MyItem> visible;
private ArrayList<MyItem> all;
private ArrayList<MyItem> filter;
public MyAdapter(ArrayList<MyItem> items) {
all = items;
visible = all; //Set all as visible
filter = new ArrayList<Item>();
for (Item i : items)
if (i.getType().equals("ORG"))
filter.add(i);
}
//Complete adapter using "visible"
public void showOnlyOrg() {
visible = filter;
notifydatasetchanged();
}
}
The non hackish way will be to remove the items from your Collection which you use to generate the listview and then call notifyDataSetChanged();
In my Android app, I have a listview that initially displays a number of items. I then want to reload the listview with a new set of items. Typically, I do this to initially create the listview items:
adapterDirectories = new DirectoryAdapter(context, R.layout.shop_directory_row, directories);
lvDirectory.setAdapter(adapterDirectories);
My adapter looks like this:
private class DirectoryAdapter extends ArrayAdapter<ShopDirectoryInfo>
{
#SuppressWarnings("unchecked")
public DirectoryAdapter(Context context, int textViewResourceId, #SuppressWarnings("rawtypes") ArrayList items)
{
super(context, textViewResourceId, items);
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
When I want to update the items I do this:
directories = getDirectories();
ArrayAdapter<ShopDirectoryInfo> adapter = (ArrayAdapter<ShopDirectoryInfo>) lvDirectory.getAdapter();
adapter.notifyDataSetChanged();
What this ends up doing though is adding the new items to the existing ones, even though getDirectories returns a completely different set of data. I have no idea why the new items are prepended to the existing set instead of replacing the old set. The only way to get the new data to show up without the old data is to recreate the adapter instead of using notifyDataSetChanged. Is there a way to get the update without calling notifyDataSetChanged?
Please try this
adapterDirectories.notifyDataSetChanged();
lvDirectory.invalidate();
This works:
ArrayAdapter<ShopDirectoryInfo> adapter = (ArrayAdapter<ShopDirectoryInfo>) lvDirectory.getAdapter();
adapter.clear();
directories = getDirectories();
adapter.addAll(directories);
adapter.notifyDataSetChanged();
The key is to use addAll after clearing.
Before setting the adapter again, you should clear the previous items like this.
if(adapter!= null) {
adapter.clear();
adapter.notifyDataSetChanged();
}
I made a list view using this way :
String[] list = {"1","2","3","A","B","C"};
Widget_List_Adapter adapter = new Widget_List_Adapter(this, list);
//set the onItemClickListener as usual
getListView().setOnItemClickListener(this);
//set our custom list item listener for the extra functions
adapter.setListener(this);
setListAdapter(adapter);
Now how can I simply add some categories ?
you must maintain custom adapter for implementing categories into your listview. take a look at this tutorial