Help with spinner - Android

So this is actually help making an app not a ROM. I am trying to use a spinner in my app as a drop down menu.
Here is the xml code:
<Spinner
android:id="@+id/fromSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:spinnerMode="dropdown"
android:entries="@array/from_array" />
Here is the Java code:
Spinner fromSpinner = (Spinner) findViewById(R.id.fromSpinner);
Spinner toSpinner = (Spinner) findViewById(R.id.toSpinner);
ArrayAdapter<String> toAdapter = new ArrayAdapter<String>(this, R.id.toSpinner, R.array.to_array);
toSpinner.setAdapter(toAdapter);
ArrayAdapter<String> fromAdapter = new ArrayAdapter<String>(this, R.id.fromSpinner, R.array.from_array);
fromSpinner.setAdapter(fromAdapter);
String toCurrency = (String) toSpinner.getSelectedItem();
String fromCurrency = (String) fromSpinner.getSelectedItem();
I'm not sure what the adapters do but I think I need them. Anyway toCurrency and fromCurrency aren't getting the values they are supposed to get. Anyone know what I am doing wrong. I am a complete noob say it may be something really dumb.

Don't know if this is helpful. Just put the source for the whole app at https://github.com/Dmobbjr/CurrencyCalculator

Really? I know some developers here could figure this out in ~10 minutes.

I'm not able to look deeper but it would help for next time if you could post the value you are getting and the value that should be there.
I'm going to guess you are getting either the first element in the array or null?
You haven't selected a value anywhere and there is no evidence of a user interaction before you try and get the selected value so either there is no value selected or the first element in the array is selected (by default) and you are getting its value.
Have you looked through the documentation for Spinners in the API Guides section of the Android Developers website? I can tell the answer is no because if you had you would know why you need the Adapters.

Kavrocks said:
I'm not able to look deeper but it would help for next time if you could post the value you are getting and the value that should be there.
I'm going to guess you are getting either the first element in the array or null?
You haven't selected a value anywhere and there is no evidence of a user interaction before you try and get the selected value so either there is no value selected or the first element in the array is selected (by default) and you are getting its value.
Have you looked through the documentation for Spinners in the API Guides section of the Android Developers website? I can tell the answer is no because if you had you would know why you need the Adapters.
Click to expand...
Click to collapse
I will change it to see what the value is later but I know it is not getting the value of the first element.
The user interaction happens before any of the code above is executed. I assumed that when I create the spinners and adapters in java it just takes what was already selected. Do I have to initialize the spinners and adapters before user selection?
Also I did read the documentation which is why I even knew about adapters but I am still a noob so some of the docs can be pretty confusing.

dmobbjr said:
I will change it to see what the value is later but I know it is not getting the value of the first element.
The user interaction happens before any of the code above is executed. I assumed that when I create the spinners and adapters in java it just takes what was already selected. Do I have to initialize the spinners and adapters before user selection?
Click to expand...
Click to collapse
The spinner needs to be initialised with data before the user selects it otherwise it would be empty. That's what the ArrayAdapter in your OP is for (which is explained in the API Guides section on spinners), however the android:entries attribute in your xml file is also adding data to the spinner so you have redundant code.
The getSelectedItem() will return an Object containing the currently selected item but as you are changing the data in the spinner beforehand this could be altering the data in the selected item position.
I recommend you remove the android:entries attribute and move the code except for the getSelectedItem() lines to the onCreate() method of your Activity.
You really need to provide more info for me about what is happening for me to help. I can't view the source on github at the moment.

If I implement the spinners in onCreate(), how do I access them from my button's onClick() method. Wouldn't the spinners be local to the onCreate method and therefore not accessible outside of it?

dmobbjr said:
If I implement the spinners in onCreate(), how do I access them from my button's onClick() method. Wouldn't the spinners be local to the onCreate method and therefore not accessible outside of it?
Click to expand...
Click to collapse
You aren't implementing them in onCreate(), they are implemented in xml which is normally inflated in onCreate() when you call setContentView(). All you are doing is adding the data to them in onCreate(). After the layout is inflated everything in that layout file is global (can be accessed anywhere) to that class.
You can get a reference to them anywhere in your code, providing the layout that contains them has been inflated into memory, using findViewById() and passing in the id of the spinner you want to get a reference to.
You can declare a class variable to store the reference to these and then just get the reference once in onCreate() and access it anywhere else in your code, ie. in onClick(), instead of having persistant calls to findViewById(). This approach is more efficient and cleaner.

When I try to call findViewById(toSpinner).getSelectedItem() The compiler says it can't resolve toSpinner as a variable. When I try to call R.id.toSpinner.getSelectedItem() it says it can't invoke getSelectedItem() on and int. I still cant access the spinners from within my onClick(). Sorry for the extreme noobishness but I just clearly do not know as much java as I should.

dmobbjr said:
When I try to call findViewById(toSpinner).getSelectedItem() The compiler says it can't resolve toSpinner as a variable. When I try to call R.id.toSpinner.getSelectedItem() it says it can't invoke getSelectedItem() on and int. I still cant access the spinners from within my onClick(). Sorry for the extreme noobishness but I just clearly do not know as much java as I should.
Click to expand...
Click to collapse
You are calling findViewById() returns a View object, you can't call getSelectedItem() on a View object. You first need to cast the View returned by findViewById to a spinner and then call getSelectedItem() on the spinner.
R.id.toSpinner is just an integer value pointing to a resource.
I already pointed out how you should go about calling getSelectedItem() on your spinner.
You can declare a class variable to store the reference to these and then just get the reference once in onCreate() and access it anywhere else in your code, ie. in onClick(), instead of having persistant calls to findViewById(). This approach is more efficient and cleaner.
Click to expand...
Click to collapse
I'll write the outline of the code you need for you.
Code:
public class TestActivity extends Activity {
private Spinner toSpinner;
public void onCreate() {
toSpinner = (Spinner) findViewById(R.id.toSpinner);
}
public void onClick() {
toSpinner.getSelectedItem();
}
}

Related

[REF][ADV] The B&N Home Screen Internals

The B&N Home application lets you add B&N-purchased books to the home screen and arrange/resize them arguably better than most generic android launchers. Unfortunately this doesn't work for sideloaded books. This is most definitely not because it can't be done, but likely as an incentive for you to purchase books from B&N and not sideload. The following describes the sqlite3 database entries necessary to manually add sideloaded books (and potentially other things) to this home screen.
Warning: Changes should not be attempted by someone not at least somewhat familiar with adb and sqlite3. This is not intended as a how-to for adding sideloaded books to the home screen (since it is tedious, and not something you'd likely do often manually), but more as a reference for someone to perhaps develop an android app for the NC to do so on-device. If you don't use the B&N Home at all and use the device more as a tablet than an e-reader, you can completely ignore this post.
Relevant databases:
/data/data/com.bn.nook.home/files/home.db - Contains the home screen 'workspace' table with entries for each item on the home screen.
/data/data/com.bn.nook.reader.activities/databases/lastreadingpoint.db - Contains the 'lastreadingpoint' table which records the last read position for all books (B&N and sideloaded).
/data/data/com.android.providers.media/databases/internal.db - Android 'media' provider database containing content entries for books (among other things). Heavily modified by B&N.
home.db, workspace table
Schema:
Code:
CREATE TABLE workspace (_id TEXT PRIMARY KEY, thumbnail_url TEXT, cover_image_url TEXT, ean TEXT, title TEXT, author TEXT, desktop_num INT NOT NULL, left INT NOT NULL, top INT NOT NULL, right INT NOT NULL, bottom INT NOT NULL, rotate_degree FLOAT NOT NULL, lending_state INT NOT NULL, is_lendable BOOLEAN NOT NULL, is_recommended BOOLEAN NOT NULL, is_new BOOLEAN NOT NULL, is_sample BOOLEAN NOT NULL, launcher_type TEXT, product_type INT NOT NULL, product_ean TEXT, is_hidden BOOLEAN NOT NULL DEFAULT FALSE, path_to_file TEXT);
Fields:
_id - Android content link to the book.
Examples: content://media/internal/products/1 (B&N book), content://media/internal/docs/1 (sideloaded book)
thumbnail_url - Thumbnail image location. Note that sideloaded use non-browser paths. File:// paths do not appear to work in this field.
Examples: http: //images.bn.com/images/85750000/85751898.JPG (B&N Book), /media/.docThumbs/Quick_Startepub.jpg (sideloaded book)
cover_image_url - Cover image location. Same format as thumbnail. Same image even, for sideloaded books.
Examples: http ://images.bn.com/images/85750000/85754280.JPG (B&N Book), /media/.docThumbs/Quick_Startepub.jpg (sideloaded book)
ean - B&N unique identifier, for B&N Books. File location in browser format (file:///media/...) for sideloaded books. Note this must match what's in lastreadingpoint.db for the last reading point to be correct when tapping the book to open it.
Examples: 9781402786402 (B&N Book), file:///system/media/books/Quick_Start.epub (sideloaded book)
title, author - Self explanitory. Plain text. Appear to be unused unless the images are not found, in which case the title is truncated and overlaid on a generic gray icon.
desktop_num - Which desktop the icon appears on. 0,1, or 2 with 1 being the default.
left, top, right, bottom - Pixel measurements for the corners of the icons. Determines the location and size of the icon on the home screen.
rotate_degree - Haven't played with this one yet.
lending_state - Current state of lendable book. Haven't investigated valid values other than the default 0.
is_lendable, is_recommended, is_new, is_sample - Flags that determine what 'banner' is displayed, and also what opens show on tap-and-hold.
launcher_type - Launcher to open the book with.
Valid values: blank (epub), epub, epib (children's books). Things still to try: html, pdf...
product_type - Type of book.
1 = B&N book, 0 = Sideloaded book.
product_ean - Appears to always match ean field.
is_hidden - Hide's icon? Haven't tried.
path_to_file - Filesystem path to book.
Examples: /media/B&N Downloads/Books/9781402786402_ePib.v2.epub (B&N Book), /system/media/books/QuickStart.epub (sideloaded book)
lastreadingpoint.db, lastreadingpoint table
Schema:
Code:
CREATE TABLE lastreadingpoint (_id integer primary key autoincrement, ean text , luid text , offsetrmsdk text , lastupdated long , bookdna int , sync_status int);
Relevant Field:
ean - B&N unique identifier, for B&N Books. File location in browser format (file:///media/...)for sideloaded books. Note this must match what's in home.db for the last reading point to be correct when tapping the book to open it.
internal.db, products/docs tables
Schema:
Code:
CREATE TABLE products (_id INTEGER PRIMARY KEY,ean TEXT NOT NULL UNIQUE,_data TEXT,_size INTEGER,product_type INTEGER NOT NULL,mime_type TEXT,_display_name TEXT,product_code TEXT NOT NULL,format_code TEXT NOT NULL,purchase_status TEXT NOT NULL,title TEXT NOT NULL,authors TEXT NOT NULL,mainAuthorFirstName TEXT,mainAuthorMiddleName TEXT DEFAULT NULL,mainAuthorLastName TEXT NOT NULL,publisher TEXT NOT NULL,date_published INTEGER NOT NULL,date_added INTEGER NOT NULL,date_modified INTEGER,date_last_accessed INTEGER DEFAULT 0,thumb_image TEXT,cover_image TEXT,rating INTEGER DEFAULT 0, user_rating INTEGER DEFAULT 0, subscription_ean TEXT DEFAULT NULL,isSubscription BOOLEAN DEFAULT 0,isSample BOOLEAN DEFAULT 0,locker_delivery_id INTEGER NOT NULL,locker_status TEXT NOT NULL, short_synopsis TEXT,storage_location INTEGER DEFAULT 1,isNew INTEGER DEFAULT 0,lendable BOOLEAN DEFAULT 0,lending_state TEXT,lendee TEXT,lender TEXT,lend_message TEXT,lend_id TEXT,lend_offer_expires INTEGER,lend_starts INTEGER,lend_ends INTEGER,category TEXT,luid TEXT NOT NULL,sync_status INTEGER DEFAULT 0,page_count INTEGER,rating_count INTEGER, local_thumb_image TEXT, local_cover_image TEXT, ebook_key TEXT, launcher_type TEXT, isDownloadable INTEGER DEFAULT 1, productEAN TEXT, isComingSoon INTEGER, DeliveryFrequency INTEGER, seriesTitle TEXT, soldBy TEXT, trialExpirationDate INTEGER );
Code:
CREATE TABLE docs (_id INTEGER PRIMARY KEY,ean TEXT,_data TEXT,_size INTEGER,product_type INTEGER,mime_type TEXT,_display_name TEXT,title TEXT,authors TEXT,mainAuthorFirstName TEXT,mainAuthorMiddleName TEXT DEFAULT NULL,mainAuthorLastName TEXT,publisher TEXT,date_added INTEGER,date_modified INTEGER,date_published INTEGER,date_last_accessed INTEGER DEFAULT 0,valid INTEGER,thumb_image TEXT,cover_image TEXT,rating INTEGER DEFAULT 0, user_rating INTEGER DEFAULT 0,storage_location INTEGER DEFAULT 0,category TEXT,page_count INTEGER, launcher_type TEXT, volume_id INTEGER DEFAULT 0);
Relevant Fields:
_id - The content id needed for the _id field in home.db workbook table.
thumb_image - Thumbnail image location. Note that sideloaded use non-browser paths. File:// paths do not appear to work in this field.
Examples: http ://images.bn.com/images/85750000/85751898.JPG (B&N Book), /media/.docThumbs/Quick_Startepub.jpg (sideloaded book)
cover_image - Cover image location. Same format as thumbnail. Same image even, for sideloaded books.
Steps to add sideloaded content via adb:
Pull all relevant tables from the device using adb.
Look up relevant database entries (content id, images, etc)
Use sqlite3 to add or modify entries in home.db, workspace table.
Example:
Code:
INSERT INTO workspace VALUES('content://media/internal/docs/3', '/media/.docThumbs/TestBook.jpeg', '/media/.docThumbs/TestBook.jpeg', 'file:///media/My Files/Books/TestBook.epub', 'Test Book', 'Test Author', 1, 20, 20, 184, 232, 0.0, 0, 0, 0, 0, 0, 'epub', 0, 'file:///media/My Files/Books/TestBook.epub', 0, '/media/My Files/Books/TestBook.epub');
Push home.db back to the NC.
Kill the Home process via shell or a process manager app.
Open Home again.
Notes:
12/29/10
Sideloaded books must be opened at least once to populate all the databases. This poses a potential problem for trying to do this at sideload time, ie through a calibre plugin.
The _id field of workspace does not appear to have to be correct for icons to function normally. My first tests had a generic 'test1' in that field until I figured out where to look for the correct value and I was able to open sideloaded books just fine with the resulting icon. This fact, along with the fact that image locations can be guessed correctly may mean we can do this at sideload time after all.
The User Guide and Quick Start are decent examples of sideloaded content on the B&N Home screen. The only oddities with them are blank ean fields, so last read position is not used when opening them.
I've seen some oddities where the icons don't render images until you touch one of them on the screen after the Home app launches. Haven't tracked down the cause yet, and it doesn't happen every time.
Setting a sideloaded book as lendable adds the 'Lend' option to the tap-and-hold, but it doesn't appear to do anything after you enter the contact. My guess is they verify this status either with their servers (likely), or with another on-device database (doubtful) .
Sideloaded books can be removed from the home screen as normal, by doing a tap-and-hold and choosing Remove.
very nice work
I can't help anyway with this but I'm glad someone's trying to fix this problem.
Wow, nice work! I'm hoping someone with more experience than me can write this into an app.
I actually really like the BN home screen, so maybe for I'll give this a try for my textbooks. I'll need them all semester, so it would probably be worth it.
Addendum: I wonder if it would be possible to make this process easier my first adding the books and the relevant thumbnails to the home screen via the process described here and then editing the database entry to remove the is_sample tag.
im gonna give this a try, but moreso, im gonna look at ADWLauncher, to see if i can add this to that launcher
Maybe I was too tired, but in my poking around (and your post) I didn't see anything about the "shelves" feature in the schema for these db. You have any ideas?
usernotfound said:
Maybe I was too tired, but in my poking around (and your post) I didn't see anything about the "shelves" feature in the schema for these db. You have any ideas?
Click to expand...
Click to collapse
Shelves are more a feature of the Library app than the Home app, so I haven't investigated them. There are tables in internal.db having to do with shelves, so I would look there first, as well as /data/data/com.bn.nook.library.
How about launching books
Any idea on how to launch a book with the B&N software? I would assume there is some kinda intents that could be used if documented to do that.
Would be nice for say a replacement home app to be able to launch books..
I've been able to (manually) create shortcuts in adw launcher to launch books using the B&N reader (as well as a few others like Aldiko, etc.). The intent (column in the adw db) that seemed to work for the B&N reader looked something like this:
Code:
file:///media/My Files/Books/Some-Book-Filename.epub#Intent;action=android.intent.action.VIEW;component=com.bn.nook.reader.activities/.ReaderActivity;end
One thing the BN Home does is initialize open-book-icon in the status bar ... to point to the 'currently reading' book. This icon doesn't work if an alternate launcher is set to default upon boot.
I'd love to get that currently-reading icon to work in alternate launchers, but I'm not sure where to begin. Any thoughts?
WhiskeySlx said:
I'd love to get that currently-reading icon to work in alternate launchers, but I'm not sure where to begin. Any thoughts?
Click to expand...
Click to collapse
This is unlikely to be trivial. As far as I can tell that icon is updated as long as the B&N home app is running in the background, even if you don't ever open it again. This likely means there is some communication between the B&N reader app and the home app to update the icon. Getting the reader app to somehow communicate with another app or launcher would almost certainly require decompiling and modifying it.
[mods, please move this to a new thread if this is too OT]
Rayven01 said:
Getting the reader app to somehow communicate with another app or launcher would almost certainly require decompiling and modifying it.
Click to expand...
Click to collapse
I wonder if the Nook reader app might expose an 'action' that matches that button's functionality. I'm thinking about how, using ADW, you can create custom shortcuts... and under "Activities", ADW lists activities for each app... for example:
- Gmail.ComposeActivity
- Market.UninstallActivity
- com.google.android.maps.PlacesActivity​
I don't know what method it uses to build that list, but... maybe it's a direction to pursue?

Reordering a List

Trying to reorder a list. I have a layout with up arrow image on left, textview in middle and down arrow on right. XML sets images to invisible.
On list item press, I turn the arrows to visible FOR THAT ITEM. Based on up or down, I swap the list item and call myAdapter.notifyDataSetChanged() in which I set both image arrows to invisible. Works just fine, except...
The client wants the arrows to "follow" the list item till a "Done" button is pressed.
I'm having trouble wrapping my head around how I get the arrows to be visible on list item pressed +/- 1...any ideas?
Tia,
Roots
P.S. The app has a database where I store these changes vs. the volatile nature of the list views
Rootstonian said:
Trying to reorder a list. I have a layout with up arrow image on left, textview in middle and down arrow on right. XML sets images to invisible.
On list item press, I turn the arrows to visible FOR THAT ITEM. Based on up or down, I swap the list item and call myAdapter.notifyDataSetChanged() in which I set both image arrows to invisible. Works just fine, except...
The client wants the arrows to "follow" the list item till a "Done" button is pressed.
I'm having trouble wrapping my head around how I get the arrows to be visible on list item pressed +/- 1...any ideas?
Tia,
Roots
P.S. The app has a database where I store these changes vs. the volatile nature of the list views
Click to expand...
Click to collapse
Hello,
I hope I have understood your problem correctly.
Keep an int named selectedIndex. When a list item is clicked, store the index of the clicked list item in selectedIndex. Then each time the up/down arrow is pressed you should swap the list item at selectedIndex with the item at selectedIndex - 1/ selectedIndex + 1 and do selectedIndex--/selectedIndex++ depending on which arrow was pressed. Then call
myAdapter.notifyDataSetChanged().
Then finally when the "Done" button is pressed, make your arrows invisible and store the changes in the database.
Please let me know if this was helpful to you
I do that and the ITEM is swapping ok, but the arrows aren't...See if I can explain it
<upArrow> This is LiistView Row1 <downArrow>
This is ListView Row 2
If I press the downArrow, I get:
This is ListView Row 2
This is LiistView Row1
....
<upArrow> This is ListView Row 14 <downArrow>
For some reason, the arrows are at like a "random" position. I need the arrows to follow the item...like:
<upArrow> This is ListView Row 2 <downArrow>
This is LiistView Row1
Have you made a ListLayout XML that defines each row of your list to have a layout that looks something like this?
Image Text Image
You could simply make the arrow images visible for the list row which is selected and invisible for those which aren't. That way you don't have to worry about changing the positions of the images. It should work but I guess there would be a better way to do it rather than making so many ImageViews.
Can you please please post your ListLayout XML if you have already defined it? Also please explain what you do to change the position of the arrow images when they are clicked.
Custom Listview; I don't have code to "change the position", that's the TOPIC of my thread! ROFL
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:id="@+id/layout">
<ImageView android:id="@+id/up" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:src="@drawable/up"
android:gravity="left" android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
androidnClick="onClick"
android:visibility="invisible"/>
<TextView android:id="@+id/toptext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20dip"
android:textStyle="bold"
android:layout_toRightOf="@+id/up" />
<TextView android:id="@+id/bottomtext" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="18dip" android:layout_below="@+id/toptext"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/up" />
<ImageView android:id="@+id/down" android:layout_width="wrap_content"
android:layout_height="30dip" android:src="@drawable/down"
android:gravity="right" android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
androidnClick="onClick"
android:visibility="invisible"/>
</RelativeLayout>
// Show arrows onClick
@Override
public void onItemClick(AdapterView<?> adapter, View v, int pos, long id) {
ImageView u = (ImageView) v.findViewById(R.id.up);
ImageView d = (ImageView) v.findViewById(R.id.down);
u.setVisibility(View.VISIBLE);
d.setVisibility(View.VISIBLE);
}
});
Click to expand...
Click to collapse
And what do you do when the arrow is clicked?
EDIT:
Can you please show the code for that too?
Not going to post actual code, but rest assured, it works. It's just a simple swap of list item, like:
// This is an "up" arrow click
temp = myList.get(pos); //where pos is row of item clicked
myList.set(pos - 1, myList.get(pos));
myList.set(pos, temp)
This works fine..tested it 100 times In my custom ArrayAdapter, if I set the up and down arrows to invisible, it works fine. Client wants the arrows to follow the item, in other words, swap the arrows too. This is the point I can't quite wrap my head around LOL
Have you tried using the ListView.getChildAt(iint) method to access the lists' rows' image views and then set them to their proper visibility in the method where you swap the items?
EDIT:
Sorry if i'm unable to help =/
Don't worry about it Like I've been saying, I just can't wrap my head around it. LOL
I'm working with setting the "tag" of each row to it's position in the list and then use "findViewWithTag", but still not having any luck.
I'm sure it's one of those "oh, duh moments" you get once you figure it out.
Thanks for trying...you did get me thinking about some new ideas and that's always a good thing!
Roots
Rootstonian said:
I do that and the ITEM is swapping ok, but the arrows aren't...See if I can explain it
<upArrow> This is LiistView Row1 <downArrow>
This is ListView Row 2
If I press the downArrow, I get:
This is ListView Row 2
This is LiistView Row1
....
<upArrow> This is ListView Row 14 <downArrow>
For some reason, the arrows are at like a "random" position. I need the arrows to follow the item...like:
<upArrow> This is ListView Row 2 <downArrow>
This is LiistView Row1
Click to expand...
Click to collapse
I assume you're using a list adapter right? Why not include the arrow status as a class member of whatever class it is that you are using as the base for the list adapter?
Yes Gene and I'm not sure what you mean. See if I can code a skeleton of what I have.
Code:
public class ReorderList extends ListActivity {
// standard on create stuff
// some code for app
// setup list adapter with my xml file
lv.setOnItemClickListener(new ListView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View v, int pos, long id) {
ImageView u = (ImageView) v.findViewById(R.id.up);
ImageView d = (ImageView) v.findViewById(R.id.down);
u.setVisibility(View.VISIBLE);
d.setVisibility(View.VISIBLE);
}
});
} // end ReorderList class
private class mAdapter extends ArrayAdapter<String> {
public mAdapter(Context context, int textViewResourceId,ArrayList<String>editList) {
super(context, textViewResourceId, editList);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// bunch of stuff in here, but at end, I set up and down arrows to invisible
// this works perfect, except the arrows are gone and you have to click
// on the item again to move it; client doesn't like that
// If I don't set to invisible, row 12 has the arrows
}
public void onClick(View v) {
// this just swaps items based on up or down arrow press
// works fine
}
OK, I see. You're just using "String" as the base to initialize the template. Instead, create a new class:
Code:
class MyListType{
String str;
Boolean bIsArrowEnabled;
}
Now initialize your template with MyListType:
Code:
private class mAdapter extends ArrayAdapter<MyListType> {
public mAdapter(Context context, int textViewResourceId,ArrayList<MyListType>editList) {
super(context, textViewResourceId, editList);
}
You'll have to work some other issues out using this approach, but no instead of just a string in your editList, you got a string and a boolean. The boolean is part of the class so no matter where editList ends up in the list, the boolean is part of it.
Still having problems with this. Here's a dumb question LOL...
When I hit one of my arrows, I swap the list items and call notifyDataSetChanged(), which calls the @Overrided public View getView(.....). At the end of this method, there is a return....where in the code is this "sitting" on the return? It's not back to onCreate or onResume...I toasted them and no message. But the onItemClick() listener is "active" and working in onCreate().
Gotta love Android programming
//********************** Nevemind, I got it...i think
It involved setting the tagId for each row in the list view. Based on which arrow is pressed, I set a boolean value to true. If up, set images visible to tagId - 1 or tagId + 1 for down
Questions or Problems Should Not Be Posted in the Development Forum
Please Post in the Correct Forums
Moving to Q&A

[Q] How To pull data from a database into a scrollable list?

Hi all,
I'm new to Android development but have been going through countless tutorials. One thing I haven't found a tutorial for is how to take a list of items from a database and put them into a list that a user can scroll through and then select one of them to open up a second screen.
E.g. if I have colors in my database, the screen would simply show all the colors in a list that I can scroll through to find the one I want. When I find the one I want, I can then select it to open up a second layout with the details.
Can anyone direct me to a tutorial or information that might go over this? Or just give me some hints as to what I should search for since my attempts didn't return anything relevant.
Thanks.
Ok, here we go:
First you need to create a layout with at least a ListView, and set it to the current activity using setContentView.
Read some data from your database (I dunno how do this, sorry), and add it to a String array or a List<String> (any of them should work).
Set an ArrayAdapter to the array (or List).
Obtain a reference to your layout ListView using findViewById. Set the ArrayAdapter to that reference.
Your list now is filled
To handle the click events, you'll need declare this method:
Code:
YourListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
//Here the code to handle clicks
//You can use a switch method, using position as variable, to know wich of the items was clicked
}
});
Example
Please use the Q&A Forum for questions &
Read the Forum Rules Ref Posting
Moving to Q&A

jquery Mobile orientation problem

Hi everyone.
I'm building an application with Html5 and jquery Mobile and I have a problem with orientation change. I have a controlgroup with 3 buttons. I want to make this buttons to be aligned horizontally while in portrait (data-type="horizontal") and aligned vertically while in landscape (data-type="vertical"). Im using this code:
Code:
$(window).bind('orientationchange', function(event){
if(event.orientation == 'portrait') {
$('.operation').attr('data-type', 'horizontal');
} else if (event.orientation == 'landscape') {
$('.operation').attr('data-type', 'vertical');
}
});
It changes the value of the data-type attribute on the page, but the buttons are still aligned horizontally. I figured this with inspect element on Chrome and I tried it on my Android phone but the buttons don't change. Any help what should I do to rearrange the buttons?
This might work. Call:
Code:
$(".operation").trigger("create")
after you change the data-type. It basically fires an event that tells jquery mobile to re-read all the data-* attributes and re-style the element.
footboydog said:
This might work. Call:
Code:
$(".operation").trigger("create")
after you change the data-type. It basically fires an event that tells jquery mobile to re-read all the data-* attributes and re-style the element.
Click to expand...
Click to collapse
I already tried that too but it didn't help. It helps if I try to change the class or something else with css but it doesn't help in changing the value of data-type.
Changing an attribute won't cause a redraw, like changing a css value or adding/removing a class name.
Where in the code do you define how to draw the buttons, depending on the attribute value? That's what you need to trigger again (document.ready, for example).
Archer said:
Changing an attribute won't cause a redraw, like changing a css value or adding/removing a class name.
Where in the code do you define how to draw the buttons, depending on the attribute value? That's what you need to trigger again (document.ready, for example).
Click to expand...
Click to collapse
This code is in the document.ready. How to trigger that?
Questions or Problems Should Not Be Posted in the Development Forum
Please Post in the Correct Forums & Read the Forum Rules
Thanks ✟
Moving to Q&A

Better than equalsIgnoreCase?

I am trying to make this application in which user needs to type in keyword. I would like to make it so that if user inputs, for example "lessons", string, with name "lessons list" would still appear, because we are talking about lessons. Maybe you have any tips, or maybe there are any android api's functions that would look not for the ideal match of strings, but for relatively close match of strings.
Thanks.
str1.contains(str2) is all you need (str1 is your string to compare, and str2 is your keyword).
Note that uppercase and lowercase characters aren't the same, so you should match them: str1.toLowerCase().contains(str2.toLowerCase()).

Categories

Resources