Friends. I am new android developer and asking question .I have searchd a lot,but didn't get the desired result.So, Here's my problem.
1. I have a ListView that conations a number of items
ListView
_________
item1
item2
item3
_________
2. When I click onItem click Listener, I get all the values of item conation(ItemDetail.java)
3. Now I have two buttons in (ItemDetail.java) buttonNext and buttonPrevious
so my query when I click buttonNext and buttonPrevious my listview item detail from list should be changed dynamically increment and decrement of position.
Thanks in advance friends.
You have to code on onResume of that List Activity.
Just update your adapter with the newly sorted data as you want and refresh the listview with that new data.
In case of any query, let me know.
Hope you got my point.
Did you try this:
OnClick() change collection elements order of the list you used in adapter (list that is binded). SwapElements(list, index1, index2)
you need to use runOnUiThread() to bind data of UI components.
runOnUiThread(new Runnable() { public void run() { refreshListview() } });
refreshListview() method is the one you are using for your refresh routine.
Related
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
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 .
In the layout I'm using for my custom adapter that I use to display my listview, there is a TextView. I would like to modify the value of this textview programmatically, but without clicking on anything. The new value is obtained on the main activity and is updated every 1s from a bound service. Is there a way to access the setText method of a textview in a specific row in a listview? Should I use a listener in the custom adapter to update the value?
Thanks a lot.
Yes You can do it all you need to do is datasource that is attached to the listView
e.g. ArrayList of data from where you show the initial data to the list item.
You need to update it when you got data from the service and after datasource is updated you just need to call the notifiydatasetChanged method
e.g. mAdapter.notifyDataSetChanged();
it will refresh your list with the updated data. In your case Textview text.
You can put your conditional logic inside getView() of your adapter to modify the TextView. For example , if you want to change the TextView when the listview item position is 2 , inside your getView() have something like
if(position==2){
//modify your textview as per requirement
}
Here the code:
So pretend that you already created a ListView Adapter then
if you want to edit your list view just put this code and remember dont forget to put adapter.notifyDataSetChanged();
//The Count of your array position // your getter and setter
data.set(datacount , new URLKeylistData(*Your parameter that show in UI*)
adapter.notifyDataSetChanged(); // this is important dont miss
List_lv.setAdapter(new URLKeyAdapter(getBaseContext(), data)); //set again your adapter
I'm trying to develop an app in which i need to create a list which contain many items.
There is also some button represent categories , and i want to populate my list according to the category.
For example: if there are 10 items in the list out of which item 1,5,7,8 are belongs to 1st category and rest of them belongs to 2nd category,now if user press 1st category button then listview show items belonging to 1st category only.
How can I do it?
I think ExpandableListView will work for you. Refer this tutorial: http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/
If you wish to be able to filter (hide/show) items in a ListView you can use an adapter, for example by creating your own implementation of BaseAdapter. This adapter has methods for sorting, clearing and adding elements to the dataset used by your ListView, as stated in this list tutorial. Just remember to call notifyDataSetChanged() after you've altered the dataset to refresh the ListView.
Setup example:
ListView list = (ListView)findViewById(R.id.list);
adapter = new MyAdapter(...); // Probably send along data
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
Filter example:
int[] newData = { 1, 5, 7, 8 };
adapter.clear();
for(int i : newData)
adapter.add(i);
adapter.notifyDataSetChanged();
You need to hide item in list (remove from list) before you set adapter. When your list is ready set notifyDataSetChanged() to adapter
I have an ArrayAdapter linked to a ListView.
mListView.setAdapter(mArrayAdapter);
Whenever I reset the ArrayList data to the ArrayAdapter:
mArrayAdapter.clear();
mArrayAdapter.addAll(mArrayList);
mArrayAdapter.notifyDataSetChanged()
the ListView gets correctly updated
however, if just after the above three lines, I call my custom method mListView.hasScrollbar() to detect whether the listview has a scrollbar or not, I get a null lastVisibleItem:
public boolean hasScrollbar() {
View lastVisibleItem = (View) getChildAt(getChildCount() - 1);
if (lastVisibleItem.getBottom()>=getHeight()) {
return true;
}
return false;
}
does it mean that the listview is still refreshing?
My main question is:
how can I test if the listview has the scrollbar after resetting the adapter with new data?
thank you for any help!
Using getLastVisiblePosition / getFirstVisiblePosition is a valid method of detecting wether you have scrolling or not within the list view (aslong as you compare it to getCount() and do your math ofc).
The problem you have as you already guess is that you are attempting to check out of sync.
In order to sync your query when the adapter already filled your List Data and updated changes, you need to issue a post request to the list, which will stack that petition to the message queue of the adapter.
yourAdapter.notifyDataSetChanged();
yourAdapter.getListView().post(new Runnable() {
#Override public void run() {
//your code here
}
});
Make sure to call that after notifySetDataChanged() of course. Because you want the list to update before the check.
I think your question equals to "how to tell if list view contains items need to displayed on more than one screen"?
So my suggestion is to use listView.getFirstVisiblePosition() and getLastVisiblePosition() to tell it.