Android Listview keeps old items after adapter changes - android

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

Related

Android Save dynamically added text grid view

I implemented this example into my project:
http://camposha.info/source/aandroid-data-passing-from-textbox-to-second-activity
So, and now I want to save the text, I've added through the edittext. Is this possible?
Or can I change the grid view with list view? I want to enter text and the text will be displayed on second activity. If it's in a grid view or list view . I don't know if it's possible to do that with list view. If so, please help me :)
I am beginner, so thanks for helping.
Gustav
I want to save the text, I've added through the edittext. Is this possible?
Yes, You can use SharedPreferences, SQLite Database and other ways. I suggest you read this link which completely explains how to preserve data in android. You can modify DataHolder class, so this class can save and load data from one of these storage, and use it to save texts in MainActivity and load them in SecondActivity.
And I did not understand the second part of your question! Do you want to show data in a ListView instead of GridView? If so, then you can replace that GridView with a ListView in ActivitySecond.xml like this:
<?xml version="1.0" encoding="utf-8"?>
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.tutorials.hp.txttoactivitygrid.SecondActivity">
<!-- Replace with this -->
<ListView
android:id="#+id/lv"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</RelativeLayout>
And then in your SecondActivity.java replace GridView gv; with ListView ls; and then change
gv= (GridView) findViewById(R.id.gv);
gv.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, DataHolder.spacecrafts));
with
lv= (ListView) findViewById(R.id.lv);
lv.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, DataHolder.spacecrafts));
The idea is to replace every code which uses GridView to use ListView instead .I think this modification is enough and should work. If it doesn't let me know so I can help you.
Also you can read about ListView and how to use it here. (You can find lots of tutorials when you search for it on the internet :D)

AutoCompleteTextView in a Listview - notifyDataSetChanged() resets it

First item in my ListView may be the search - AutoCompleteTextView . But sadly when ListView gets adapter.notifyDataSetChanged() - what was typed there is being erased and AutoCompleteTextView looses focus. The keyboard stays but I can no longer type it until I press this view again and type it .
But. this adapter.notifyDataSetChanged() happens alot - what should I do?
Have you tried to put AutoCompleteTextView before / outside ListView ?
This should update only the itens in the ListView but with the AutoCompleteTextView isolated.
I have almost the same implementation and worked for me.
<LinearLayout />
<AutoCompleteTextView /> =====> Filtering here (I guess).
<ListView /> =============> notifyDataSetChanged().
Hope it helps.
dNiel Liqskk is right ....
but if you want to go for a second option....
You can add autocompletetextview as header view..

Get the new items added after refreshing a Listview using Pull to refresh in Android

I have a ListView with Pull to Refresh. After refreshing the ListView, it moves to the top. But I want to show the item added newly to the ListView. How to implement it, Can anyone help me. Thanks in advance.
Use this attribute android:stackFromBottom="true", because it always add new item at bottom of list view. Like following code,
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="40dp"
android:divider="#null"
android:stackFromBottom="true" >
</ListView>
And add one more thing add in your code,
[listView].smoothScrollToPosition([adapter].getCount() - 1);
I think this topic has the answer you want in addition to Ando Masahashi's answer : https://stackoverflow.com/a/3505869/1506369
You can add reFill method to your adapter, for example:
public void reFill(List<MyData> newData){
this.myData.clear();
this.myData.addAll(newData);
}
So, after refreshing the ListView and getting new data, you call this method of adapter and set new data, and call notifyDataSetChanged() on adapter. If you doing this, ListView doesn't move to the top.

Android ListFragment, new items appear at bottom of list

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>

How to show Empty List into Android ListView

I am currently developing application kind of notepad or Todo list. But I need to know to how to show empty list into listview, I guess android default ListView can not handle empty list view. Basically I need to show listview with no data, which I will add later, but initially the UI for listView should be empty.
Thank You!
this may helps you when there are no data in list
listview.setVisibility(View.INVISIBLE);
nodat.setVisibility(View.VISIBLE);
Hre nodata is Contain Empty Textview with SetText Your String like No Data
if you added the empty list into the adapter and that adapter set to listview then it will show you empty listview. after you add item to the list then you need to call notifydatasetchanged() of the adapter.
ListView as display initially empty till you add an item to the list and notify via adapter.
Note you must call method notifydatasetchanged() of an adapter when you populate the list item for add/edit/delete.
According to Developer Guidelines describing ListView you can easilty setup a TextViev, any other widget will be fine as well. Where you will encurage user to create ListItem.
Here is a snippet.
<ListView android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
<TextView android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:text="No data"/>

Categories

Resources