I'm trying to add data (objects) to an adapter, (for a recycler view) from many databases. Theres two recycler views (both use view pagers) A and B, the data in A can change and anything in A should be movable to B, what i did was create a class that makes objects and then made an array list that holds the objects so in A id have my array list
List<CardWriter> cardMakerList = new ArrayList<>();
and add it to my adapter
cardAdapter = new CardAdapter(cardMakerList);
then add items to it and update the adapter
CardWriter cardWriter = new
CardWriter(getResources().getDrawable(R.drawable.happy),"HAPPY"," happy");
cardMakerList.add(cardWriter);
cardAdapter.notifyDataSetChanged();
if i wanted to change the data i could
CardWriter cardWriter = new
CardWriter(getResources().getDrawable(R.drawable.sad),"SAD","sad ");
cardMakerList.add(cardWriter);
cardAdapter.notifyDataSetChanged();
and for adding items to B i could get the position of the item from the onClick method. The adapter ive used for B has almost identical variables and methods so i could use this
SpeakRecyclerGrid.cardMakerList.add(SpeakRecyclerGrid.cardMakerList.size(),
cardWriter);
and it works fine but ive now started using databases so what i had:
List<CardWriter> cardMakerList = new ArrayList<>();
becomes this:
List<addNewCard> cardMakerList = new ArrayList<>();
to change the data its a new database so it changes to this
List<simpleNewCard> cardMakerList = new ArrayList<>();
but trying to add this new data to my (A) adapter doesnt work as its expecting addNewCard (even though the methods and properties are identical) it gives me an error like
CardAdapter(java.util.List<greendao.addNewCard>) in CardAdapter cannot be
applied to (java.util.List<greendao.addSimpleCard>)
so i don't want to make lots of different adapters i just want one for recycler view adapter A and one for the recycler view adapter B.
I've posed this question lately on here and had someone reply that i could make them implement a common interface or abstract Card class and make a List of that, I'm a novice programmer and have spent the last few days studying this and trialling different things, but I'm using a project called GreenDao and i don't know how to implement this can anyone give me some pointers
Related
I have two List coming from webservice Like past and future.Sometimes both List have data and sometime only one list have data. And how set the differt RecyclerView for pastList and FuturList with same adapter.
As long as your model objects are the same, it sounds like you need to have two versions of the adapter, where you can assign each list of data to each different adapter.
So if your adapter was MyObjectRecyclerViewAdapter, you could make two instances - pastObjectsAdapter = MyObjectRecyclerViewAdapter();
futureObjectsAdapter = MyOjbectRecyclerViewAdapter();
Then assign one to each of your RecyclerViews and update the list on each with your lists from your webservice.
In RecyclerViewFragment Class, there is MovieData, and the Adapter Class stores MovieData.getMovieList( ).
My question is, when Event Handler be triggered,
Add / Delete data directly in RecyclerViewFragment Class
Define add( ) / delete( ) function in Adapter Class and call adapter.add( ) / adapter.delete( ) in RecyclerViewFragment Class
Which way is the better design? Who has the responsibility to take care of data change?
I would suggest to have RecyclerViewFragment containing the data. And create an adapter passing the reference to the data. For example:
mAdapter = new CustomAdapter(mMovieData.getMovieList());
Then in the RecyclerViewFragment just perform the needed actions such as add / remove such as:
mMovieData.getMovieList().add(index);
Think this is the most common way todo it. You do not want to replicate the getMovieList data twice in adapter and RecyclerViewFragment. This will get confusing.
If data change is happening after adapter has been initialized, then it should be handled inside adapter. Adapter also has a nifty function notifyDataSetChanged() which will update item positions inside the adapter when data has changed.
The recyclerview is not concerned with the data, this is all handled by the adapter you have created to present the data. Have a look at the functions here to see what options you have in regard to data changes.
Adapter is the responsible class for filling a list with data ,for example if you have a list that need to be filled with data lets say SCORES then the adapter is the one responsible for filling these scores in the list. on the other side, RecyclerView is reposnsilbe for making the adapter fill it with more speed and effecieny, the recyclerView uses views that are previously used to display data by the adapter and place them in cache for later reuse to display the same type object so that it doesn't have to initialize a new object each time the adapter want to fill item in the list which make it faster.
and here is a link that might help you as well :
Hope my answer have helped you.
I have just started learning android using Big Nerd Ranch Guide. In a project, we were to implement a listView and get the data from an array using an ArrayAdapter, as the crime objects were stored in some list.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().setTitle(R.string.crimes_title);
mCrimes = CrimeLab.get(getActivity()).getCrimes();
ArrayAdapter<Crime> adapter =
new ArrayAdapter<Crime>(getActivity(),
android.R.layout.simple_list_item_1,
mCrimes);
setListAdapter(adapter);
}
After that, we were to create custom list to display the title as well as the checkbox and date so we created a custom layout. Now, we used a different adapter for that purpose.
private class CrimeAdapter extends ArrayAdapter<Crime> {
public CrimeAdapter(ArrayList<Crime> crimes) {
super(getActivity(), 0, crimes);
}
My question is why cant we use the same adapter that we used earlier?I mean we can just give it new values or create a new object.I searched the internet and found that we can create multiple adapters with some restrictions. why we extend it to ArrayAdapter? cant we just do it like we did previously by creating the adapter?
Please explain in detail, as I have just started.
Thanks.
Let us talk about the list view adapter implementation in android first of all in your first example you used the default implementation of listview adapter in android which provide you with list view of text
but most of the time this is not enough for your user interface you want to add an image a checkbox like in your second example so android OS gives you the ability to make your own custom cell (each row in list) that's why you would find yourself in a need to extend array adapter
in your first example you used
android.R.layout.simple_list_item_1
which is a layout containing only textview
and you provide a data source mCrimes
so when you extend array adapter you do the same you provide your own layout which represent the cell and a data source which will populate your list
hope that clarify it for you and welcome aboard :)
I have a ListFragment with an ArrayAdapter to display a list of objects. I want to refresh the ListFragment's contents. The items themselves change their text a little and also change their order. I have over one hundred items and the only way I've found to do this is clear the list and re-add them, but this feels ineffcient.
For example:
ListFragment display unordered:
Object A
Object D
Object F
Object B
Object E
Object C
ListFragment display ordered:
Object A
Object B
Object C
Object D
Object E
Object F
I set up my ArrayAdapter like this:
mAdapter = new ArrayAdapter<Store>(mContext,
android.R.layout.simple_list_item_1,
OtherClass.list);
mAdapter.setNotifyOnChange(true);
setListAdapter(mAdapter);
"OtherClass" is:
public class OtherClass extends Object {
public ListArray<Object> list;
...
}
The code in question:
mAdapter.clear();
for (Object o : list) {
mAdapter.add(store);
}
I've already read a lot of answers about how it works and how to use mAdapter.notifyDataSetChanged() but none of them solve my problem or answer my question. Is the only way to refresh the list done by adding/removing something from the list?
Whatever you are attaching your adapter to, like say a listView, it can be unattached by setting it to null or to something else.
So instead of the code in question:
1). Sort the list.
2). Then call:
mAdapter = new ArrayAdapter<Store>(mContext,
android.R.layout.simple_list_item_1,
sortedList);
setListAdapter(mAdapter);
I hope this helps, as your question is still a little unclear. Generally if you have a list generated by an adapter, you dont want to be manually digging through the list as it is auto generated for you and doing so takes away from its ease of use.
OP here. My solution is to stick to my original method which is: If you want to modify the list slightly, but have the list refresh so it's updated and also keep the scroll position then the only way to do this is to clear() and refill the list.
Source: http://vikinghammer.com/2011/06/17/android-listview-maintain-your-scroll-position-when-you-refresh/
Hello folkes I have this little problem for which I cannot find a suitable answer looking around the web and on these forums. Please don't direct me to articles in which people have requested list view text color changes at run time, as I read lots of them and not found one to help me out.
I have a simple ListView that displays an array of String objects via the use of a ListAdapter.
I need to update some of ListView Strings at run time, based on their contents. Using a global reference to the list adapter used in the lists views creation I can get the contents of each list view String using following code below.
However, in addition to retrieval I'd like to be able to modify each string in turn, then put it back in the same index position and have the list view reflect the changes. How?
for (int x = 0; x <= listAdapter.getCount();x++)
{
Object o = this.listAdapter.getItem(x);
if (o.getClass().getSimpleName().equals("String"))
{
String s = (String) o;
s = modifyString(s);
//s is the string I want to modify then put back in the same place.
}//end if
}//end for
As far as I know you cannot change the items in an Adapter - unless you are using a custom Adapter (by extending a BaseAdapter etc...)
So, I think you will have to:
make sure you Adapter's constructor takes in the data structure that holds your strings
make sure your data structure is global
make the changes in that data structure whenever you need to
call myAdapter.notifyDataSetChanged();
This will tell adapter that there were changes done in the list and listview should be recreated.
And after your listview is renewed you can even take the user back to the index by:
list.setSelection(positionWhereTheUserClicked);
I hope this helps, let me know if you need more code references.
Here is some code
private ArrayList<String> results = new ArrayList<String>(); //global
private BaseAdapter searchAdapter = new BaseAdapter (results, this); //global
private void updateResults(final ArrayList<String> updatedList){
results = updatedList;
final ListView list = (ListView)findViewById(R.id.search_results);
list.setAdapter(searchAdapter);
list.setOnItemClickListener(new ListView.OnItemClickListener(){
// implementation of what happens when you click on an item //
});
searchAdapter.notifyDataSetChanged();
}
This code works just fine on my end, I hope it helps.
Just stumbled on this problem and found a solution.
I'm using a
m_ListAdapter = new SimpleAdapter(this, m_List, R.layout.option_list_row, columns, renderTo);
Each item in my listView is a manu option causing a dialog to show, once data is received through the dialog, all I have to do is just create a new SimpleAdapter with an updated ArrayList that includes the new data, then just setAdapter to the new adapter.
The ListView will update instantly.