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..
Related
I'm trying to make ListView in a widget where there are multiple clickable items in each row. For example, if each row has data containing a link, one button in the row to open the link and another to share the link.
Sample code:
Widget layout
<LinearLayout...>
<TextView../>
<ListView with id="list_view" />
</LinearLayout>
Each list row in the ListView has:
<LinearLayout with id="listRowParent">
<TextView../>
<Button with id="openBtn"/>
<Button with id="shareBtn"/>
</LinearLayout>
I'm able to set onClick of each row using
remoteViews.setPendingIntentTemplate(R.id.list_view,
*<PendingIntent>*); //in onUpdate
and remoteView.setOnClickFillInIntent(R.id.openBtn, *fillIntent*); // in adapter
which works fine, but I can't have setPendingIntentTemplate for a list row button by doing remoteViews.setPendingIntentTemplate(R.id.shareBtn,
);
since nothing happens on clicking the button if I do this.
Question:
Is what I'm attempting possible? If yes, help is greatly appreciated :)
My bad. I should be using
remoteViews.setPendingIntentTemplate(R.id.list_view,
*<PendingIntent>*);
for both the buttons, but I was trying to use the button's ID directly in the second. Got fixed.
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.
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();
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"/>
I have Created a ListView and set a background image.Everything goes fine except I am unable to click on the right side of the row of ListView.Can anybody help me this.
Below is the xml file...
<ListView
android:layout_width="wrap_content"
android:id="#+id/JSONListView"
android:layout_height="wrap_content"
android:background="#drawable/listbg"
android:focusable="false"
android:cacheColorHint="#00000000"
android:visibility="visible" >
</ListView>
And I am not using any View.
Use the attribute
android:layout_width="match_parent"
and set click listener for items.
I guess you have created your custom adapter. If so, just go to the getView() method of that custom adapter and on the holder instance of that portion of listitem add a onCLickListener.
Provide more info. XML layout, etc.
Im guessing there is a view there or it is not filling the entire width but difficult to say.
Android ListView Activity