Don't showing new record on ListView - android

i have 3 class. MainActivity,OneFragment,TwoFragment, I have listview which i fill using by Sqlite on OneFragment.And I add record from TwoFragment (with CreateData function using by my SqlHelper class) but new record dont showing in the listview.
How can i solve this?
If you want to see codes i can upload.

I highly recommend you to use RecyclerView instead of ListView (as Google says:
The RecyclerView widget is a more advanced and flexible version of ListView.
), but Both of them you need some refresh data may be like this on your OneFragment:
#Override
public void onResume() {
super.onResume();
items.clear();
items = dbHelper.getItems(); // reload the items from database
adapter.notifyDataSetChanged();
}
this question may help you with your problem Android ListView not refreshing after notifyDataSetChanged
if you do not understand send code

Related

Android RecyclerView.Adapter notifyItem drives me crazy

I searched for hours but still have no explanation (even not in official docu) what is the right way to use a RecyclerView.Adapters' notify-methods. Already read a lot of similar question on SO, but no could solve my simple problem. I try to put down a short, simplified example.
class MyAdapterClass extends RecyclerView.Adapter<MyAdapterClass.MyViewHolderClass> {
...
List myItemList = new ArrayList<MyItemType>();
...
}
RecyclerView myRecView = (RecyclerView) findViewById(R.id.myRecView);
myRecView.setAdapter(new MyAdapterClass(...)));
Even when inserting the first item, the view is not refreshed.
myAdapter.myItemList.add(newItem);
myAdapter.notifyItemInserted(myAdapter.myItemList.size() - 1);
If I then tightly touch (try to scroll) the 1-item-listview, the item appears. So it seams to be a refreshing issue. But why? I have an emtpy list, then I insert one item, then I notify the adapter about the insertion. Whats wrong with my expectation?
By the way, even if I additionally call:
myAdapter.notifyItemChanged(myAdapter.myItemList.size() - 1);
it gets not automatically refreshed.
EDIT: And of course, I dont want to use notifyDataSetChanged() :-) only insert, update, remove on an existing list.
Try to add this code inside your adapter class :
#Override
public int getItemCount() {
return myItemList.size();
}
i hope you define in adpter getItemCount() in list size. then used blow code for notify adapter.
myadapter.notifyItemRangeChanged(0, userDataArrayList.size());

Couchbase lite on Android, retrieve views

This question is about Couchbase lite (no Sync Gateway).
I'm new to Couchbase, I managed to use the demo app, but I don't understand it completely.
It contains this code which (as far as I understand, since I'm not native English speaker) retrieve views to populate a listview with the indexes:
// This code can be found in ListsActivity.java
// in the setupViewAndQuery() method
com.couchbase.lite.View listsView = mDatabase.getView("list/listsByName");
if (listsView.getMap() == null) {
listsView.setMap(new Mapper() {
#Override
public void map(Map<String, Object> document, Emitter emitter) {
String type = (String) document.get("type");
if ("task-list".equals(type)) {
emitter.emit(document.get("name"), null);
}
}
}, "1.0");
}
listsLiveQuery = listsView.createQuery().toLiveQuery();
Could anyone give me a hand with what each part is doing?
In which step is the listview populated
Can I change "list/listsByName" in the code (line 3)? What would happen?
Can I emit more than one element?
The code is a little bit convoluted. Let's answer the easy parts first.
Can I change "list/listsByName" in the code (line 3)?
Yes. That's just the name of the Couchbase View. You choose the View name. Unfortunately the terms used in Couchbase and Android overlap some. A Couchbase View is a kind of static index of your database.
Can I emit more than one element?
Yes. You can emit most anything you want. Take a look at the documentation here
Now, tracing how the Android ListView gets updated:
In ListsActivity.java notice in the onCreate method a ListAdapter instance gets added to the ListView. This ListAdapter is a private inner class that extends LiveQueryAdapter.
LiveQueryAdapter is in the utils subpackage. If you look at its constructor, you'll see it adds a change listener to the query passed in. When triggered, this change listener sets an enumerator equal to the rows passed back by the live query, then calls notifyDataSetChanged to tell the list to refresh itself. That, in turn, causes getView in ListAdapter to get called. That's where the data is pulled from the database and used to populate a list entry.

SwipeMenuListView new Row added is showing First time data

I've read many of the previous posts on this topic, but i'm not getting it right.
I have an adapter which has a private values of the items in the list.
When i update the values(add a new items), i watch the value in debugger and the "getView" func and see that the value is correct.
BUT the actual rowView i see is just the first item in the list.
I have no clue what may cause this.
This listview is on the same activity while i show a different layout and hide the listview to add a new item.
Can there be a connection while the listview visibility is "GONE"?
When i remove items from it it updates listview fine(that is done when listview is visible).
private void updateAdapter() {
this.values.clear();
this.values.addAll(staticlistIndifferentclass);
notifyDataSetChanged();
}
~~~~UPDATE~~~~
Ok,
So i discovered the cause of the problem, though i'm not sure why it is.
The code was fine the way it was with regular Listview but the bug is on:
com.baoyz.swipemenulistview.SwipeMenuListView
Try use ListView.invalidateViews
This should cause views rebuilding
Use new Adapter object like this :
listView.setAdapter(new yourCustomAdapter());
When you delete all dataSet items ,it is better to bind new Adapter object .

Update ListView when updated list has got from server

How dynamically update the content of ListView?
App gets list from server and shows it. Also app caches this list for showing it to the user without delay. Somewhere in the future app will get updated list (update is small: two items was added, one was deleted).
How to detect changes and update only these items in ListView? Is there any complete solution?
If you are using ArrayAdapter, you can use .add(), .addAll(), .remove() to change data of the Adapter. And then call .notifyDataSetChanged() to refresh the view in the GUI.
Here are codes to refresh ListView when its data source is changed.
List<Something> lists;
And your Adapter should like this:
public MyAdapter extends BaseAdapter{
private List<Something> mLists;
public MyAdapter(List<Something> lists){
mLists = lists;
}
...
}
And initialize Adapter:
MyAdapter adapter = new MyAdapter(lists);
listView.setAdapter(adapter);
And when lists changes and you want to refresh ListView, call like this:
adapter.notifyDataSetChanged();
or:
listview.notify();
Then you will see your ListView is changed.

Update ListView from adapter Instantly

I'm developing an application that shows stock market information. In my application I use listactivity and my own adapter is being used.
The listView used to show the stocks is working fine and is updated with correct data. The only problem is, although I use
adapter.notifyDataSetChanged();
the list isn't updated until scrolling and items subjected to change, are scrolled out from the screen. When they are scrolled in, they appear with new data.
I need to change the data as soon as I notify the adapter.
I have face same problem
but finally got solution
And its solution is
listView.invalidateViews();
I think for that you have to take the help of the Lazy ListView or Lazy Loader:
Check out the below link :
Lazy load of images in ListView
It will help you.
Are you calling notifyDataSetChanged in UI thread? If not: you can do the following:
runOnUiThread(new Runnable() {
public void run() {
adapter.notifyDataSetChanged();
}
});

Categories

Resources