I'm wondering right now how to implement following functionality in best and most reusable way.
Whe then user clicks an item in the ListView, he goes to DetailsActivity, but when he comes back to ListActivity item that he clicked before is shown as first in the list.
I'm wondering should I implement some kind of sorting in listview or in adapter. Which way would be most reusable and generic eg. clicked item is excluded?
You can use the insert method from ArrayAdapter. When coming back from DetailsActivity, delete the clicked item from your adapter using the remove method, then insert the element at the first position. Of course, you also need to call notifyDataSetChanged when you're done.
To identify when you're coming back from DetailsActivity, you could launch it with startActivityForResult and modify the adapter in onActivityResult. More on that here.
The most simplest thing I could suggest for it is
Remove the item from the list and the add the item to the list on calling on activity result.
Set the data to the adapter (may be using a public method in adapter class)
Call notify data set changed.
Get the item position clicked from onItemClicklistener and delete the item at that position in arraylist or array and add it to 0th position. So while calling DetailsActivity instead of writing startActivity(Intenet) use startActivityForResult(Intent) and in OnActivityResult method do the above said operation and call notifydatasetchanged().
Related
I'm new to Android and can't quite figure out the right approach for the problem I'm trying to solve. I have an ExpandableListView with several items. Each item has an EditText, except the last item has a button. The contents of the EditTexts are to be loaded from the database. When the button is clicked or when the activity is navigated away from, I want to save the contents of each EditText to my database.
I'm not sure what to call from the activity's class, what to call from my adapter, and how exactly to access each item appropriately. Code is welcome but not necessary, I'm just looking for guidance on the general approach. Thanks.
I'd recommend putting a method in your adapter called saveAllValues. It iterates through the list of objects in the adapter and saves them to the database. Call this when your button is clicked and in your activity's onStop() method (which is called when the activity is no longer visible).
You should have your activity fetch the values for the item IDs in an AsyncTask in its onCreate method. Then pass the list of id/value pairs to the adapter in its constructor. It should maintain this list so it can go back through it and save the IDs and values to the DB.
Hope my answers in these links help:
Values of counter changes after scrolling ExpendableListView shows how to maintain the list inside the adapter and how to get list from the activity.
Remove the divider view in the expandablelistview last item shows how to make the last child different from the others.
In my android application, I have one spinner box and two edit text box for adding item.When I click on add button all the data will store in database and display in below Item list (listview display in my first image) by refreshing listview.
My problem is, when I click on add button at a time only one list is display and listview heading is also gone.
May be this problem is raised due to refreshing of listview.
I searched on google and found notifyDataSetChanged() method is used for refreshing list view but I don't know how notifyDataSetChanged() method is used in fragment?
Does anybody know how to use this method in fragment?
I am new to fragment. In my app all the java code is developed using fragment activity, and xml file is simple layout.(LinearLayout/RelativeLayout)
You can see my screen here:
1)First image show list heading.
2)Second image show List item.
You need to setAdapter again to your listview after adding new data. it will show new data int listview.
if not get, post your code where you setAdapter to listview.
You have to first add item in Arraylist/List which you passed in Adapter then after you can call below code..
adapter.notifyDataSetChanged();
listView.invalidateViews();
I have 3 activities containing listviews with custom adapters, an item selected at the first one drives the user to the second one, and so on...
At these listviews, some items are highlighted as "new itens" (each time adapter's getView is called, I check at a database if the current item should be highlighted). Once user has reached the 3rd listview, I mark these items as "checked", and want to propagate this change back to the other listviews...
That means, when user comes back, pulling the 2nd and 1st listviews up from the stack, I want the viewed items not to be highlighted anymore.
I've tried this answer on SO, without success. When executing notifyDataSetChanged() from onResume(), my listview simply didn't shows up. And I'd prefer not to use startActivityforResult()...
here's my code to refresh the listview at onResume():
#Override
public void onResume(){
super.onResume();
//I have basically the same code at onCreate()
adapter = new ListingsAdapter(getApplicationContext(), this);
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
here's another answer that sounds promising, but I wasn't sure how can I retrieve an specific view from the adapter... I want to check all visible items with adapter.getView(), but it asks for a convertView and a parent ViewGroup and I couldn't get that
Thanks in advance for any hint on this
Why don't you wanna use startActivityforResult()?
You don't need to create a new adapter every time in onResume(). You only need to change the underlying data (set the checked flag for the visited item) and then call adapter.notifyDataSetChanged(). This will trigger the adapters getView() method for all the visible list elements.
I have an intent that loads a new activity, I also have some extra intent data to get when the new activity is started. Depending on what button is clicked on the first activity, it should scroll to a certain part of the scroll view on the next activity
It would be easiest to have it scroll down enough so that a certain item is at the top and the first thing read (defined by its ID)
Does anyone know how to do this?
Thanks
Check out the smoothScrollToPosition method in ListView:
http://developer.android.com/reference/android/widget/ListView.html#smoothScrollToPosition(int)
This is what you can call to automatically show your list view to the desired position
ListView.setSelectionFromTop(index, 0);
to find out where to start you must compare the item that you want the list to show at startup from the list and the item that is received from your intent.
if you exactly know the position of a specific item in the listview you can call the method written above directly
I want to add data in list. When I click the button, it takes data from different classes and adds it as a row in the list and comes back.
And again, when I insert data in form and press the button, it adds it below the previous one on another row. So an array is created of rows, and now when I click on any position of list item row, I get its content. So how to implement it?
First thing you need to do is whenever you add new data in the list you should call notifyDataSetChanged(). For getting its content you need to write onItemClickListener() and in listener you can get the content through its position id.
Your question is not very clear. But from what I understand, you what to use a ListView or a ListActivity.
In either case, you will need an adapter which manages the data displayed in the list. I would suggest that your "different class" which provides the data should subclass BaseAdapter.
You can then bind the class providing the data to the view using setAdapter(ListAdapter)
When your data changes, all you need to do is call notifyDataSetChanged() on your data class which provides the data and the view will be updated.
To get the clicked item, you will need to create and set an OnItemClickListener on your ListView
See this list activity example for guidance.
deep copy the generated list and append it to the previous array