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
Related
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);
I am new to Android and am trying to get my header round the SimpleCursorAdapter and CursorLoader classes. From my understanding, all of the examples that I have seen use these two classes to load data into a ListView in a background thread (to not block the UI).
I am OK with this and have used this general approach with my own list of items, however I now want to be able to click on an item in the list and get the full information for the item. Is it usual practice to use SimpleCursorAdapter and CursorLoader to retrieve the details for a single item? or are they just meant for lists?.
Thanks.
They are not meant for lists only. You can - and should - use them in detail views (activities) as well.
I've sketched a possible way to do so on my blog:
http://www.grokkingandroid.com/using-loaders-in-android/
Think of Adapters as a layer of abstraction between your data (Cursor) and whatever you attach that Adapter to (ListView for example). This way, you have a common interface between your data (Cursor, ArrayList, whatever) and the View you display that data on (ListView, TableView, etc.), this is helpful because if you later find that you want to access your data through an ArrayList rather than a Cursor, then you simply swap out the adapter with a different one and you're ready.
Now considering your question, Adapters give an abstract access to information, therefore you can "ask" it for what information is stored and where. You could attach an OnItemClickListener to your ListView and then access your data from there.
I'm very surprised there is such a small amount of info on drag and drop sorting with a cursor adapter and list adapter.
The closest post I have found on stackoverflow is this one:
https://stackoverflow.com/a/5047618/317889
But, it's not clear to me how to implement what CommonsWare suggests - clarification would be very helpful.
So far I am binding the cursor data to a list adapter and setting this as follows:
mMyCursorAdapter = new MyCursorAdapter(getActivity(), null);
setListAdapter(mMyCursorAdapter);
getLoaderManager().initLoader(0, null, this);
The list is generated but I now wish to add drag and drop functionality to the list items.
I would like to know the best way to go about this from an architectural point of view and any pointers as to how to go about the development of the core functionality would also be useful.
This blog post by Jason McReynolds (including a sample project) helped me a whole lot.
It explains how to use Carl A. Bauer's Library Drag-Sort-ListView with a CursorAdapter and SqLite. It shows how to save the the ListView's newly ordered state in the database as well.
This can definitely be achieved and lucky for you most of the work has already been taken care of, but you will need to modify a class slightly to meet your specifications.
The default Android music app has all of the classes you'll need.
First, you'll need to grab their custom ListView that allows for dragging and dropping.
That can be found here - TouchInterceptor.java.
You'll also need to grab their custom Cursor that's used to actually move the items in your ListView. It's an inner class called NowPlayingCursor.
That can be found here - TrackBrowserActivity.java
NowPlayingCursor extends AbstractCursor and its used to return the queue. The method makeNowPlayingCursor() is specifcally where you'll write most of your own code. Instead of returning the queue, you'll need to return the items you interested in moving, whatever they may be.
In order to use the TouchInterceptor, you'll need to implement TouchInterceptor.DropListener.
private TouchInterceptor.DropListener mDropListener =
new TouchInterceptor.DropListener() {
public void drop(int from, int to) {
final NowPlayingCursor mNowPlayingCursor = (NowPlayingCursor) YOUR_CURSOR;
mNowPlayingCursor.moveItem(from, to);
// Call `notifyDataSetChanged` here.
}
};
You should also look their moveQueueItem method used to move an item from one index to another. This method is used in the NowPlayingCursor when onMove and moveItem are called.
That can be found here - MediaPlaybackService.java
So, there's some work to be done on your part, but this definitely possible.
Here is a library that hopefully will solve your problem, it enables drag and drop reordering of list items.
Has an excellent demo that includes use cases for Fragments and Cursors
https://github.com/bauerca/drag-sort-listview
Key features:
Clean drag and drop (no visual glitches; I hope!).
Intuitive and smooth scrolling while dragging.
Support for heterogeneous item heights.
Public startDrag() and stopDrag() methods.
Public interface for customizing the floating View.
I am banging my head for the last couple days in order to get this done but Im unable to. Someone please help me out!
Let me not tell u the whole thing and will try to explain it simply n clearly.
Im having 1 ArrayList. I am trying to replicate that into another one and trying to delete an item at a particular index. But this not only deletes the item in the replicated ArrayList but also the original ArrayList.
For ex:
var DuplicateList:ArrayList = new ArrayList();
DuplicateList = OriginalList;
DuplicateList.removeItemAt(2);
The above not only deletes the "Item 3" at Index-2 in the DuplicateList but also in the OriginalList.
I just need some workaround with this approach as this is the only way by which whatever I typed inside the controls present in an ItemRenderer of a FLEX List control that uses the OriginalList as a dataProvider is RETAINED, when I change the dataProvider of the List Control from OriginalList to DuplicateList. The following approach does not retain all the data.
var DuplicateList:ArrayList = new ArrayList();
DuplicateList.addAll(OriginalList);
DuplicateList.removeItemAt(2);
ListCntrl.dataProvider = DuplicateList;
Thanks for your help in advance...
A very, very important thing to understand:
ActionScript3 uses references to objects. Because of that, the two variables in this line of code refer to the exact same instance of an ArrayList:
DuplicateList = OriginalList;
So, when you remove an item from one reference, it is gone from the next. If you want two separate instances of ArrayList, then you need to clone it like you are suggesting later in your code.
So far, so good... but why is your ListCntrl retaining the data from the OriginalList? That doesn't make any sense at all. If you remove an item from DuplicateList and then use it as the data provider, then that item shouldn't be there. I think there is more to this story...
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.