Android ListFragment, new items appear at bottom of list - android

I'm new to Eclipse and Android app development. Been trying to solve this issue for two days now but with no luck. Surfed the net and documentation for... many hours. :S
I have a ListFragment and a Button enclosed in RelativeLayout. The button is located below the list and the list is intially empty at app startup which is what i want. I'm using an ArrayAdapter and my items (String objects for now) are inside an ArrayList.
When I click the button, the list is updated with the new item and everything works fine except that new items show up at the bottom of the visible list, just above the button. If i click once more on my button, a new item shows up at the bottom of the list, and the first item is moved above the second item. Why is that and how do i fix it? I want the first item to show up at the top of the list, second item below that item, third one below the second one and so on.
What i do when i add an item is simply adding an item to beginning of my list of objects and then call adapter.notifyDataSetChanged().
How do i specify that new items in my empty ListFragment list should appear at the very top of the list?
Been playing around with different TRANSCRIPT_MODE but that didn't help. The solution is probably trivial... but i just can't find the solution. Help!

If you want to add items to the top of your ListView, you need to insert the items at the top of your items-list.
List items = new ArrayList();
for(Object obj : objectList) { // objectlist is a list of new items
items.add(0, obj); // INSERT AT TOP
listAdapter.notifyDataSetChanged();
}

I'll answer my own question here.
Change of
android:layout_height="wrap_content"
to
android:layout_height="match_parent"
in the fragment XML tag fixed it. What I still don't understand is why wrap_content makes list items show up at bottom of the screen first. Maybe some bug with how wrap_content works with initially empty lists when used in a ListFragment?

To make it more clear.
items.add(obj);
This will populate my initial empty list when i click the button repeatedly.
items.add(0, obj);
This will do the same, only the internal order of the items are reversed.
In BOTH cases, the first visible item in the app when it is running, shows up above the Add Button (located at the bottom of screen). I want the first item to show up just below the ActionBar. The ListFragment is inside a relative layout and is defined like this
<!-- this file is included in both single-pane and two-pane versions of the layout-->
<!-- i use merge to avoid code duplication -->
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<Button
android:id="#+id/button_add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:onClick="onClick"
android:text="#string/button_add"/>
<fragment
android:id="#+id/item_list"
android:name="com.example.test.ItemListFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/button_add"
tools:context=".ItemListActivity"
tools:layout="#android:layout/list_content"/>
</merge>

Related

My listview seems to hide the bottom two entries once the entire page is full

I have a list view with a list view adapter in my kotlin project. It works perfectly fine for the first thirteen entries, once I go past that the next two entries don't show up however for every entry after those next two the entry I entered two entries before shows up and I can scroll to it (So if I put in a sixteenth entry then the fourteenth will show up). When deleting so that the whole page is no longer full the final two show up. When debugging I saw that they should be visible as the list view adapter shows the code to display them running so I think the problem is that when scrolling the listview I just cant scroll till the bottom 2. Due to this I suspect that the navigation bar is maybe covering the bottom two entries or that the list view for some reason is longer than or as long as the parent so the bottom of it is past the end of the page. However I am still not sure how to fix this problem even though I think I know what it is.
The xml code for the list is below:
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/header" />
Can you try to change your constraint for ListView like this
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#+id/header" />
here i have consider that there is not other view below listview to show
that's why i have set bottom constraint to parent

Spinner items are cut off at top if spinner is placed on a dialog

I placed a spinner on a dialog. When I want to select an item and the item list is shown below the spinner, everything is fine and I can scroll through the items. But if the item list is shown above the spinner, the item list is cut off and I cannot scroll:
It looks like the system does not identify, that the item list above the dialog is not visible and therefore does not show the scrollbars.
My spinner definition looks like this:
<Spinner
android:id="#+id/spnnrValue"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="2dp"
android:layout_marginLeft="-20dp"
android:layout_weight="1" />
This is how I define the dialog:
dialogColorPickerExtended = new Dialog(Base.getMainActivity(), R.style.full_screen_dialog);
dialogColorPickerExtended.setCancelable(true);
dialogColorPickerExtended.setContentView(R.layout.template_color_picker_extended);
if (dialogColorPickerExtended.getWindow() != null)
dialogColorPickerExtended.getWindow().getAttributes().width = (int) (GUIManager.getScreenWidth() * 0.9f);
Do you have any idea, how I can show the complete item list?
You might not be able to show it all at once, but adding a scrollbar to the spinner itself may be a solution. Visit this page, you might be able to solve this problem with an overall functionality improvement by adding a scrollbar to your spinner.
Spinner's scrollbar style

Why listview not showing up in xml?

Everything is done i.e adapter is created, the layout is created and data is passed but when I add ListView to the main activity, then nothing appears. I think the problem is somewhere in xml. Please help
Activity2.xml
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.student.shopifysalespediasample.Activity2"
android:orientation="vertical"
android:id="#+id/list"
android:drawSelectorOnTop="true">
<!-- android:drawSelectorOnTop="true" shows a visual effect while clicking on listItem views-->
It shows blank screen instead of showing a sample list item in xml
tools:listitem | tools:listheader | tools:listfooter
These attributes are intended for <AdapterView> (and its subclasses like <ListView> and <RecyclerView>) and used to specify the layout that should be drawn inside that adapter as a list item, header or footer. For example, fragment_contacts_xml layout of our Contacts+ app declares <RecyclerView> and this is how it looks like before and after adding tools:listitem=”#layout/contact_item”
tools:itemCount
This attribute is intended solely for <RecyclerView> and used to specify the number of list items the layout editor should render in the layout preview.
By default Android Studio shows 10 list items for <RecyclerView>.
Therefore usually after adding tools:listitem attribute the <RecyclerView> covers the entire layout screen and you can no longer see other view elements below it. In such cases tools:itemCount attribute will help you to see the elements below the <RecyclerView>.
More you can find in hidden gems article.
You need to add ArrayAdapter or CustomAdapter along with list item layout to populate list view. If after adding adapter problem persists then please share complete code. Use this link for reference. https://www.vogella.com/tutorials/AndroidListView/article.html

Android Listview keeps old items after adapter changes

I try to change the content of a Listview in Android. After that I want the list to be redrawn. Unfortunately the list elements change, but the old items are displayed in the background. If there are less items in the new list it seems like the old items are still part of the list, as they are still displayed, even though they are not clickable. If I jump out of the app via the Android Taskmanager and afterwards open the app again, the list gets displayed correctly!
Therefore I think it must be a problem with refresh.
How do I try to achieve it:
As the complete content changes I create a new adapter that I pass to the ListView. In my code "favorites" is the new Array that I pass to the adapter. I also try to clear the list and invalidate it. all of this happens in the UI thread (onPostExecute of AsyncTask)
MyAdapter newAdapter = new MyAdapter(
context, favorites);
MyAdapter current_adapter = (MyAdapter) myList.getAdapter();
if((current_adapter!=null)){
current_adapter.clear();}
myList.setAdapter(null); //inserted this because of answers, but with no effect.
myList.setAdapter(newAdapter);
myList.invalidateViews();
The clear method of the adapter:
public void clear(){
items.clear();
notifyDataSetChanged();
}
As you can see I tried all of the solutions to similar problems her on stackoverflow. Unfortunately nothing worked...
Thanks in advance for any ideas how to solve this.
Edit:
I have also tried to set the adapter null before passing the new one, also with no effect.
.setAdapter(null);
Also if I open the keyboard by clicking in a editText, the screen refreshs and the list is displayed correctly.
Have you tried myList.setAdapter(null); ?
I solved the problem by filling the remaining space under the list with a layout with more layout_weight than the ListView. This way the "extra" lines are hidden.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/transparent"
android:orientation="vertical" >
<ListView
android:id="#+id/main_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:id="#+id/aux_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#color/transparent"
android:orientation="vertical" />
</LinearLayout>
I tried that and I discovered HardwareAcceleration has to be set to TRUE (what is also default value) in application tag in your manifest file. Otherwise listView will keep old non-clickable items in its background during typing.
android:hardwareAccelerated="true"
First clear the data. I think it's favorites or items or whatever. and notify the adapter in this way.
items.clear();
((MyAdapter) myList.getAdapter()).notifyDataSetChanged();
MyAdapter newAdapter = new MyAdapter(context, favorites);
myList.setAdapter(newAdapter);
myList.invalidate();

ListView populates from the bottom instead of from the top

I have a listView that expands upwards instead of downwards.
I have another listView on another page that works just fine and populates itself from the top -> bot.
Why does my listView start from the bottom instead of the top?
My XML
`
<ListView
android:id="#+id/list_view_showRegister"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/showRegister"
android:layout_alignParentLeft="true"
android:layout_marginTop="50dp"
android:clickable="false" >
</ListView>`
Your ListView is still actually populating from the top downwards. Just not from the top of the page.
You have set the layout_height="wrap_content" and the layout_above="#+id/showRegister". With the height being wrap_content, the view will only be as large as it's contents. Then, due to the fact you have used layout_above, it will be directly above another view.
The listview is not filling from the bottom, it is filling from the top (of the listview) - but the listview itself is only starting from halfway up the screen. Anything above the listview is not actually part of the listview, but empty space instead.
The solution is to either set the layout_height="match_parent", or remove the layout_above tag.
I know this is an old question but I hope this helps anyone else who may have found this issue via google.
Have a look a these links. Is it possible to make a ListView populate from the bottom?. populating from bottom.
Add new items to top of list view on Android?. Add new item at the top of list.
See for android:stackFromBottom attribute.

Categories

Resources