3

I have two listview, like listview_1 and listview_2. I wanna refresh the listview_2 while listview_1 is refreshed.

My code like this:

public void updateTwoListView() {
    listview_1.getAdapter().notifyDataSetChanged();
    listview_2.getAdapter().notifyDataSetChanged();
}

But it don't work, listview_1 can refresh but the listview_2 can't.

And at that moment what I found is that listview_1 was on focus.

And then I tried to set focus to other views before ran the method, both of them didn't refresh. It likes to refresh a listview only if the listview has focus.

What's more I found that when I called the method to refresh, listview_2 didn't, and then I set focus to listview_2, refreshed itself!

So, What all I want to ask is:

How to refresh two listview at one moment in Android?


What's more code:

//init two listview there
public void init() {
    listview_1 = (ListView)findViewById(R.id.listView1);
    listview_2 = (ListView)findViewById(R.id.listView2);

    adapter1 = new MyListViewAdapter(mContext);
    adapter2 = new MyListViewAdapter(mContext); //I have tried use different adapter, that also didn't work.

    listview_1.setAdapter(adapter1);
    listView_2.setAdapter(adapter2);
}

the real code of upside snippet is:

public void updateTwoListView(int currentPosition) {
    adapter1.updateCurPos(currentPosition);
    adapter2.updateCurPos(currentPosition);
}

and in MyListViewAdapter.java:

public void updateCurPos(int currentPosition) {
    mCurrentPos = currentPosition;
    notifyDataSetChanged();
}

And I will call method like listViewManager.updateTwoListView(1) outside to refresh.


Any reply is appreciated.

AZZ
  • 55
  • 9
  • The second variable is `listview_1` too. Did you mean `listview_2`? – Fabi755 Sep 10 '15 at 08:55
  • @Fabi775 yes, you are right, I forgot to change it. – AZZ Sep 10 '15 at 09:27
  • Yes, that's no problem. At the bottom of your question is a edit button. – Fabi755 Sep 10 '15 at 09:29
  • @Fabi755 Thank you for your kind reply. ^_^ – AZZ Sep 10 '15 at 09:59
  • Have you found a solution? I testing it with two list views and two ArrayAdapters.. it works without trouble. From which adapter you inherit? – Fabi755 Sep 30 '15 at 13:08
  • @Fabi755 3Q for following! It isn't solved. And I make demo to test two or more list views updating at one time ,too. Yes , like you say it works well. And I guess my problem is caused by the method `updateCurPos(int currentPosition)`, I have only changed the value of `mCurrentPos` but the data bind of the list view, and then used `notifyDataSetChanged() ` to refresh, it may not work, I guess. I will continue to test until the problem is solved, and then I will announce the result there. thank you. – AZZ Oct 08 '15 at 09:45
  • 1
    @Fabi755 Yes, it workd ! I changed the `data` (bind of `listview_2`) in `updateCurPos ()`,and then `notifyDataSetChanged()` workd ! What left that I couldn't explain why `listview_1` could refresh with the same code while `listview_2` couldn't. Whatever I'm so happy I finally solve the problem.^_^ – AZZ Oct 08 '15 at 10:15

2 Answers2

1

You have called listview_1 twice. Just change one of them to listview_2 as below:

public void updateTwoListView() {
    listview_1.getAdapter().notifyDataSetChanged();
    listview_2.getAdapter().notifyDataSetChanged();
}
programmer23
  • 533
  • 4
  • 15
  • yes, I do this in fact. I think it's not the main reason thy I can't refresh the second listview . – AZZ Sep 10 '15 at 09:23
  • does it solves your problem or not ? if not then post more code, – kuldeep Sep 10 '15 at 09:24
  • If it doesn't work, we need to know at least where you call this function. – programmer23 Sep 10 '15 at 09:48
  • @programmer23 The story is a bit long. I have edited my question adding more code right now. And I can assure the method I showed was run. – AZZ Sep 10 '15 at 09:57
0

It seems the problem of your code is that you call getAdapter().

Sets the data behind this ListView. The adapter passed to this method may be wrapped by a WrapperListAdapter, depending on the ListView features currently in use. For instance, adding headers and/or footers will cause the adapter to be wrapped.

Source: http://developer.android.com/reference/android/widget/ListView.html#setAdapter(android.widget.ListAdapter)

The solution is save your Adapter as member variable in your activity and call the notifyDataSetChanged()from there.

See more on this question's answer: https://stackoverflow.com/a/31893525/2742759

Community
  • 1
  • 1
Fabi755
  • 1,495
  • 1
  • 12
  • 29
  • The question looks like similar to mine, and I hope the answer will work for me ,too. but sadly not. In fact, I have made my listview and my list adapter members of my class. You can see what code I added. – AZZ Sep 10 '15 at 10:33
  • Oh sorry. Yes your're right, I had only see the first code snippet. – Fabi755 Sep 10 '15 at 10:35