Why do we use single adapter per class in android? - android

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 :)

Related

Beginning Programming: How do I pass an ArrayAdapter to RecyclerView?

I am trying to understand what other alternative I have if I can't write this code:
arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,bluetoothDevices);
recyclerView.setAdapter(arrayAdapter);
I have started to learn Java and I am taking a freshman class that requires us to write a mobile app, but there is no instruction on how to do this. This class is not a programming class, it's a project based course so everyone has to learn everything on their own. I have literally been introduced to android a week ago.
My other question is how does android store text files?
ArrayAdapter isn't meant to be used with RecyclerView. If you want to keep your ArrayAdapter, you can change your RecyclerView to a ListView:
// Change to a ListView
// (Remember to change it in your layout as well)
ListView myListView = findViewById(R.id.my_list_view);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, myStringItems);
myListView.setAdapter(adapter);
This will work fine for simple data but, if you need to display more complex data or if you require more customization or performance, using RecyclerView is the recommended approach.

populate a recycler adapter from databases

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

Android Custom ListView/Adapter

I am wanting to change my listView from the normal ArrayAdapter (simple_list_item_1) to something more like this:
Name..... Score(center right).
Date (under name)
So There's 3 Views...
name
date
score
I've looked up how to make custom adapters and layouts but they are all very confusing. I just want a simple fix that I can add to an existing project.
Here's my code for my list right now:
//update listView
listAdapter = new ArrayAdapter<String>(GradesActivity.this, android.R.layout.simple_list_item_1, names);
mListView.setAdapter(listAdapter);
Help is appreciated!
I just had the SAME problem last week. My solution was a tutorial on the internet: http://androidtuts4u.blogspot.com.br/2013/02/android-list-view-using-custom-adapter.html
Just copying and pasting the code onto a temporary project (it will take less than 15 minutes) will help you understand how simple it is and also make it very clear so you can implement it into your original project. I hope it helps!
Peace!
ArrayAdapter can't auto binding three view
You can try SimpleAdapter or extend BaseAdapter implements it youself
This is Google's training
https://developer.android.com/training/material/lists-cards.html
https://developer.android.com/training/wearables/ui/lists.html
And here is a sample of SimpleAdapter http://www.java2s.com/Code/Android/UI/UsingSimpleAdaptertofilldatatoListView.htm
There are many fixes you need to do
1. Create a class that extends BaseAdapter.
Override all the methods create a constructor of that class that will initialize listitems
You are just passing names
listView listAdapter = new ArrayAdapter(GradesActivity.this, android.R.layout.simple_list_item_1, names);
Instead of just names create a class that contains three elements you need I.e name date and score and make there get and set methods
And the object of the class will be saved in a list.
4. In getview method you have to set these variables

Android ListView with Images inside of ListFragment

I have been trying to get a listview inside of SherlockListFragment for about a week (total of like 12 hours) and I can't figure it out. I'm out of options here. So, I have three tabs, which all need to have listviews, with images. I managed to get the listviews working, but I just cannot seem to find a way to add images. I am a HUGE beginner at this as well.
Here is one of the three tab classes that extend SherlockListFragment:
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String[] commands = this.getResources().getStringArray(R.array.currentmobs_selections);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
R.layout.main_list_item, commands);
setListAdapter(adapter);
}
Now I did make a custom array for the listview in attempt to get it working, but I didn't even know how to implement the custom array adapter into the code. If anyone knows any way to get a image/text listview inside of a ListFragment I'd be soooo happy. Thanks!!
You need to use a Custom adapter instead of an array adapter.
Good tutorial here: http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/
Use if listview holds only one string
ListAdapter listAdapter =
new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1,command);
setListAdapter(listAdapter);

I need help populating a listview from a remote source in android

I've searched far and wide for this answer and can't seem to find it.
I'm looking to populate a very simple 3 line listview, no more then 5-6 words per line at the most inside of my android app.
I'm currently using a base adapter and a string array to enable the actual text to show up on the screen.
I want to have the ability to update the information inside of my listview remotely using
some sort of means whether that's xml, SQLite, plain text, etc and then have that hosted file populate my listview.
Can anyone here help me to figure out how to do this? I'm still pretty new to android development so please go easy on me. Hopefully this question wont be too hard answer and also not too difficult to enable for a newbie like myself.
If the most you're going to ever have in there is just 3 lines of text, I think a SQLite DB may be a bit much for your situation. I'd look into using a Typed Array.
Here's a link to the Android Dev Guide on this subject:
http://developer.android.com/guide/topics/resources/more-resources.html#TypedArray
Here's a code sample:
public class YourListActivity extends ListActivity {
String[] mTestArray;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create an ArrayAdapter to contain your items
ArrayAdapter<String> adapter;
mTestArray = getResources().getStringArray(R.array.yourArray);
// Assign your array to an adapter with your layout file
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mTestArray);
// Assign the adapter to this ListActivity
setListAdapter(adapter);
}
}
EDIT
Just realized that your data will be on a remote server, so this approach may not work for you, but it can still give you an idea of how to take your data once received from your remote server and place it into a ListView.

Categories

Resources