Android Udacity tutorial - adding data to that listview - android

I'm doing the Android Udacity tutorial on learning how to make an android app.
The video tutorial tells me to use some sort of view with a fragment - but then the text acknowledges that this doesn't exist anymore as a starting option, and instead of editing the tutorial they just ask I read up about fragments.
So I did, and fragments are confusing? There seem to be two imports I can user for fragments - android.app and android.support.v4.app? Which am I meant to use (not for making the same code as the tutorial, but for making quality code today)?
And then, for this exercise - just a view with a list - would I even use a fragment or would I kick it off with an activity directly?
I guess I'm asking what is best practice for my MainActivity class to make a screen that has a (i know it won't scroll bc there are so few items!) scrollable list with the entries
bob
jane
marko
helena

You don't need to use fragments for a simple list like this.
Here's a simple solution for static content:
listView = (ListView) findViewById(R.id.listView);
String[] names = new String[] {"bob","jane","marko","helena"};
//Param 1 is the context, 2 is the layout, 3 is the textview that's changing to match the name
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, android.R.id.text1, names);
listView.setAdapter(adapter);

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.

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

Is there a simple list widget in android that lets me select from a list without having to create an adapter?

I have a list, I want the user to select one. It's a string. Maybe even have an Object I can associate with it.
Is there any way to do this without creating a subclass with an arrayadapter?
I see tons of examples and they all seem overengineered for what must be the most basic list handling problem in the world. Is there no default simple list string handler built in?
If there is, I can't find it.
Help?
Is there no default simple list string handler built in?
ListView is a simple list string handler that's built in.
Is there any way to do this without creating a subclass with an
arrayadapter?
You don't have to subclass ArrayAdapter to use it. Binding the data to ListView is one line of code:
final ListView list = ...;
list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data));
In your case, data would either be a List<String> or a String[].

LoaderThrottle magic tricks

I'm new in Android. I spend my free summertime on studying it to try to develop an application.
I need to display in a list (ListView), data from a database table, in order for the user to manipulate it.
I found simpleCursorAdapter but it is indicated as deprecated to avoid performing queries in UI Thread.
With my fisrt table, which will keep small, it would perhaps not be a problem, but with main table it would. So I studied LoaderManager recommanded replacement.
I found LoaderThrottle example in ApiDemos SDK Samples to try to understand LoaderManager mechanisms.
ApiDemos/src/com/example/android/apis/app/LoaderThrottle.java
I tried hard to understand this example but I still have some points I don't understand.
I didn't found android.R.layout.simple_list_item_1 and android.R.id.text1 on line 406 :
// Create an empty adapter we will use to display the loaded data.
mAdapter = new SimpleCursorAdapter(getActivity(),
android.R.layout.simple_list_item_1, null,
new String[] { MainTable.COLUMN_NAME_DATA },
new int[] { android.R.id.text1 }, 0);
setListAdapter(mAdapter);
I also didn't find android.R.id.content on line 377:
// Create the list fragment and add it as our sole content.
if (fm.findFragmentById(android.R.id.content) == null) {
ThrottledLoaderListFragment list = new ThrottledLoaderListFragment();
fm.beginTransaction().add(android.R.id.content, list).commit();
}
There is no simple_list_item_1.xml in ApiDemos layout directory.
There is a lot of text1 in different layout but didn't find which one it could possibly be if any.
Found 3 "content", one in content_browser.xml of type ContentBrowserActivity$Content...
So I don't understand where is the link with a ListView I was expecting.
So for me it's still "magic" since I don't see the hidden links between ListView and the adapter.
What I am supposed to have in my ListView?
Nothing? One TextView per data I get from my database?
If someone had an example of LoaderManager to link a database table with a ListView not melt with all other ApiDemos, I would enjoy it!
Thanks in advance for your hints and answers,
Florent
Thank you for your answers. I now understand better the mechanism.
There is no need to create a particular layout.
So I think the link with the rest of the application is in FragmentTabs activity, line 54:
bar.addTab(bar.newTab()
.setText("Throttle")
.setTabListener(new TabListener<LoaderThrottle.ThrottledLoaderListFragment>(
this, "throttle", LoaderThrottle.ThrottledLoaderListFragment.class)));
ApiDemos/src/com/example/android/apis/app/FragmentTabs.java
Best regards,
Florent

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