I have an activity where I want it to display results from a database using ListView. The table has three columns: word, description, and category. On the activity page, I have a list of categories being read from an array. I want to set it up so that if you click on an item on the list (for example Cat1), the results returned from the cursor will be all words/descriptions with the category Cat1 in the DB. Currently I simply have a Toast with that category's name appear when clicked.
As it is right now, the activity does not run properly. I have read around on the web, and am not sure how to proceed. Here is the code I have so far. If anyone can help me, I would really appreciate it.
public class Categories extends ListActivity {
DataBaseHelper db = new DataBaseHelper(this);
private SQLiteDatabase data;
int position;
private ListView list;
private String[] categories = {
"C1", "C2", "C3", "C4",
"C5", "C6", "C7", "C8",
"C9", "C10", "C11", "C12"
};
#Override
public void onCreate(Bundle icicle){
super.onCreate(icicle);
setContentView(R.layout.cat_list);
list = (ListView)findViewById(R.id.cat_listing);
list.setAdapter(new ArrayAdapter<String>(this,
R.layout.categories, categories));
list.setTextFilterEnabled(true);
Cursor cursor = data.rawQuery("SELECT term, desc FROM words WHERE cat = '" + categories[position] + "'", null);
startManagingCursor(cursor);
String columns[] = new String[] { "term", "desc" };
int[] to = new int[] { R.id.cat_term, R.id.cat_desc };
SimpleCursorAdapter myAdapter = new SimpleCursorAdapter(this, R.layout.cat_result, cursor, columns, to);
this.setListAdapter(myAdapter);
list.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view,
int position, long id){
CharSequence text = categories[position];
Toast toast = Toast.makeText(getApplicationContext(),
text, Toast.LENGTH_SHORT);
toast.show();
}
});
}
}
I have a similar function in my app. You will need to create a XML file to define the layout for the list and call them in your java code.
Here is the example XML:
LIST:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
</ListView>
<TextView
android:id="#+id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/no_projects"
android:padding="10dp"
android:textSize="16sp"
android:textStyle="bold" >
</TextView>
</LinearLayout>
This is the custom layout XML. I call it list_rows:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="#+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TextView>
<TextView
android:id="#+id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TextView>
<TextView
android:id="#+id/text3"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
And this is the JAVA code:
String[] fields = new String[] { db.TABLE_PRJ_NAME, db.TABLE_PRJ_TYPE, db.TABLE_PRJ_DESC };
int[] views = new int[] { /*android.R.id.text1*/ R.id.text1, R.id.text2, R.id.text3 };
c = db.getAllProjects();
startManagingCursor(c);
// Set the ListView
SimpleCursorAdapter prjName = new SimpleCursorAdapter(
this,
R.layout.project_list_rows,
//android.R.layout.simple_list_item_1,
c, fields, views);
setListAdapter(prjName);
The onListItemClickListener
// This section of code is for handling the Click Event of the Projects List
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Cursor o = (Cursor) this.getListAdapter().getItem(position);
String projectName = o.getString(1);
Intent showProjectDetails = new Intent(this, ProjectDetails.class);
Bundle bundle = new Bundle();
bundle.putString("sendProjectName", projectName);
showProjectDetails.putExtras(bundle);
startActivity(showProjectDetails);
}
What the new part of the code is doing is just sending the selected item to another activity through an intent. Then in the new activity, I am querying the DB using the selected item name from the bundle and displaying the result.
Do ask if you need further explanation.
I am making a very similar program. I'm having trouble with it as its my first program for android/java. What help I have got is from web and my step father who use to teach java.
When helping me setup a database he explained to setup my tables so categories is paired with items under it. So you would have a table for your categories including the title you will display for that category as well as your primary key. Then you need a table for the next level down from category, say title. This would include a text for title and primary key. Next you need to make another table to pair them up. A title key, category key, and primary key.
From there you will need to tell java to pair them up based off that last table.
I'm a noob so sorry I couldn't give more/better info. But anything I find on this issue I'll post here and good luck.
Related
I am populating a list from an SQLite database which works fine but the first text view is always empty and I cannot figure out why. This is causing me issues because I use the populated text views to display information when clicked but as the first one is empty and can be clicked it is causes the program to fail. Here is my code:
public class ShowPhrases extends Activity implements OnInitListener {
SQLiteDatabase database;
NotesDbAdapter md;
CursorAdapter dataSource;
TextToSpeech tts;
private static final String fields[] = { "phrase", "folang",BaseColumns._ID };
NotesDbAdapter.DatabaseHelper helper = new NotesDbAdapter.DatabaseHelper(
this);
#Override
public void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.hs);
//Set volume buttons to music volume instead of ringer
setVolumeControlStream(AudioManager.STREAM_MUSIC);
tts = new TextToSpeech(this, this);
database = helper.getWritableDatabase();
Cursor data = database.query("notes", fields, null, null, null, null,null);
dataSource = new SimpleCursorAdapter(this, R.layout.highscores, data, fields, new int[] { R.id.first });
final ListView lv = (ListView) findViewById(R.id.list_view);
lv.setHeaderDividersEnabled(true);
lv.addHeaderView(getLayoutInflater().inflate(R.layout.highscores, null));
lv.setAdapter(dataSource);
The highscores xml layout:
android:orientation="vertical" >
<TextView
android:id="#+id/first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginTop="20dp"
android:focusable="false"
android:textColor="#357ae8"
android:textSize="20dp" />
</RelativeLayout>
The hs xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/gerback"
android:orientation="vertical" >
<ListView
android:id="#+id/list_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
Can anyone see why this is happening?
Try this:
Cursor data = database.query("notes", fields, null, null, null, null,null);
data.moveToNext();
dataSource = new SimpleCursorAdapter(this, R.layout.highscores, data, fields, new int[] { R.id.first }, 0);
Does it works?
Remember to add 0 as last parameter to use the non deprecated method.
When you say that the first Textview is empty, are you talking about the headerview or the first item in the list?
If you mean the headerview, and the first piece of xml (in your question) is the one being inflated into it (R.layout.highscores), then the problem is simply that you haven't assigned it any text to display. The headerview isn't populated by the adapter, and it scrolls with the list, so it will be blank in your case.
I am filling a listview from names of a database. That is all working fine. After clicking on an Listitem with the name i would like to set the onListItemClickto fill out textviews in another activity. That is where i am stuck.
Here the code of my listview activity which is working:
public class DataListView extends ListActivity {
private ArrayList<String> results = new ArrayList<String>();
private SQLiteDatabase newDB;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hotelslist);
openAndQueryDatabase();
displayResultList();
}
private void displayResultList() {
ListView hotelslist = (ListView) findViewById(android.R.id.list);
hotelslist.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, results));
getListView().setTextFilterEnabled(true);
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Toast.makeText(getBaseContext(), l.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();
}
private void openAndQueryDatabase() {
try {
DataBaseHelper newDB = new DataBaseHelper(this, "mydatabase.sqlite");
SQLiteDatabase sdb = newDB.getReadableDatabase();
Cursor c = sdb.rawQuery("SELECT name FROM hotel ORDER BY name ASC", null);
if (c != null ) {
if (c.moveToFirst()) {
int i = 0;
do {
i++;
String name = c.getString(c.getColumnIndex("name"));
results.add(name);
}while (c.moveToNext());
}
}
} catch (SQLiteException se ) {
Log.e(getClass().getSimpleName(), "Could not create or Open the database");
} finally {
if (newDB != null)
newDB.close();
}
}
}
Besides the name the table also contains columns "address" and "telephone". This data i would like to be filled in textviews "addressfill" and "telephonefill" of another activity with following xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/address"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Address" />
<TextView
android:id="#+id/addressfill"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/telephone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Telephone number" />
<TextView
android:id="#+id/telephonefill"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
I tried to read into many tutorials out there and many posts here but didnt get around to found the right answer.
What code would i have to use for the onListItemClick and in my activity that uses the xml I presented. Help is very appreciated!
If you want to set up listeners on list items, you will need to extend array adapter and put the listener code in the adapter:
public class SettingsListAdapter extends ArrayAdapter<String> {
#Override
public View getView(int position, View convertView, ViewGroup parent){
// put your listener in here
mybutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do work here
}
}
}
In other words, the simplelistadapter is not sufficient to do want you want, you will need to create your own.
edit
Here's a good example of creating your own custom ArrayAdapter: Custom Array Adapter Tutorial
In onListItemClick() create an Intent and put the name in the Intent:
Intent intent = new Intent(DataListView.this, SecondActivity.class);
intent.putExtra("name", l.getItemAtPosition(position).toString());
startActivity(intent);
You must change SecondActivity to the class name of the Activity that will receive the name.
In this second Activity's onCreate(), read the String:
String name = getIntent().getStringExtra("name");
if(name != null) {
// Do something with the name, like selecting the address
// and telephone number from your database to display
}
However since you are working with a database you really should use a SimpleCursorAdapter. Working with a Cursor is more useful and efficient.
how can i do it for the other columns data of the table like address and telephone?
You have two options:
In your second Activity use a query like:
"SELECT address, telephone FROM Hotel WHERE name = '" + name + "'"
But if multiple hotels have the same name, you will get more than one address...
Change your query in your first Activity to:
"SELECT name, address, telephone FROM Hotel ORDER BY name"
Now in onListItemClick() you can pass the name, address, and telephone number all at once, but you will need to store the address and telephone number somewhere... This is where a SimpleCursorAdapter is extremely useful.
I'm trying to get my contacts on a list view. Now I know that using simple_list_item_multiple_choice enables me to select multiple contacts, but it views names only without numbers.
On the other side, simple_list_item_2 can be used to show both name and number, but supports selection of only one contact.
Is there any template that combine them both? If not, how could I build my customized list with both features?
EDIT: This is the code I'm using
CursorLoader cl = new CursorLoader(this,ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PROJECTION, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" ASC");
Cursor c = cl.loadInBackground();
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_multiple_choice, // Use a template
// that displays a
// text view
c, // Give the cursor to the list adapter
new String[] { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME},
new int[] { android.R.id.text1},0);
setListAdapter(adapter);
Here, the second parameter of SimpleCursorAdapter is simple_list_item_multiple_choice but it supports only dealing with android.R.id.text1. So I can use items only, not subitems.
But in the following code
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_expandable_list_item_2, // Use a template
// that displays a
// text view
c, // Give the cursor to the list adapter
new String[] { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ,ContactsContract.CommonDataKinds.Phone.NUMBER},
new int[] { android.R.id.text1,android.R.id.text2},0);
I can give it both ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME and NUMBER to be written in android.R.id.text1 and android.R.id.text2, but can't use multiple choice feature.
As Dipu said, you should make your own customized layout.
To show name and contact, you need two text views, and one check box for checking.
You can start coding from this tutorial:
http://www.mysamplecode.com/2012/07/android-listview-checkbox-example.html
Add one more text view to country_info.xml will solve your problem.
ADDED
To use a custom list view layout, you have to implement your own adapter.
This tutorial (2. Custom ArrayAdapter example) will help you figure out how to do that.
http://www.mkyong.com/android/android-listview-example/
The answer provided by Heejin is excellent, but it's not important to implement a custom ArrayAdaptor. What only I needed to do is to write a custom layout and send it to the SimpleCursorAdaptor constructor.
The custom layout represent the layout of each item in the list view. I need each row to contain a CheckedTextView and another small TextView as a subitem. So I've made a layout called row_view.xml with the following content
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<CheckedTextView
android:id="#+id/checkedTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:text="CheckedTextView" />
<TextView
android:id="#+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
Then I've just used it in the constructor
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.row_view, // Use a template
// that displays a
// text view
c, // Give the cursor to the list adapter
new String[] { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER},
new int[] { R.id.checkedTextView, R.id.textView},0);
setListAdapter(adapter);
This is the full code
public class MultipleContacts extends ListActivity implements OnItemClickListener {
private static final String[] PROJECTION = new String[] {
ContactsContract.CommonDataKinds.Phone._ID
,ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
,ContactsContract.CommonDataKinds.Phone.NUMBER
};
SimpleCursorAdapter adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.multiple_contacts);
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
getListView().setOnItemClickListener(this);
// Get a cursor with all people
CursorLoader cl = new CursorLoader(this,ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
PROJECTION, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" ASC");
Cursor c = cl.loadInBackground();
adapter = new SimpleCursorAdapter(this,
R.layout.row_view, // Use a template
// that displays a
// text view
c, // Give the cursor to the list adapter
new String[] { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER},
new int[] { R.id.checkedTextView, R.id.textView},0);
setListAdapter(adapter);
}
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//what to do when an item is clicked
CheckedTextView checkedTextView = (CheckedTextView) v.findViewById(R.id.checkedTextView);
Toast.makeText(this, checkedTextView.getText(), Toast.LENGTH_SHORT).show();
}
}
Please note that I've two layouts, one for the list view itself(called multiple_contacts) and the provided layout here (called row_view) is the layout for each item in the list view. All what I need from multiple_contacts is to write setContentView(R.layout.multiple_contacts);
I am designing a simple app from tutorials that I have seen, and I am attempting to simply display two text views in each row of a listview, a name and a price. This worked and I could select the row and it would activate an intent. However I then changed my xml code so that the listview would be placed in a linearLayout in order for me to have a header at the top of the screen. Now when I click on any of the rows they are highlighted but nothing else happens. I have already tried to making the textviews set to clickable = false in the xml but still no luck. I am hoping I am just missing something simple in the onCreate method. `public class ViewMenuListing extends ListActivity {
public static final String ROW_ID = "row_id"; // Intent extra key
private ListView contactListView; // the ListActivity's ListView
private CursorAdapter contactAdapter; // adapter for ListView
private String tableName;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
contactListView = getListView(); // get the built-in ListView
contactListView.setOnItemClickListener(viewContactListener);
setContentView(R.layout.viewmenu);
//Get table name of menu clicked.
Bundle extras = getIntent().getExtras();
tableName = extras.getString("table");
// map each contact's name to a TextView in the ListView layout
String[] from = new String[] { "name", "price" };
int[] to = new int[] { R.id.itemTextView, R.id.priceTextView };
//int[] to = new int[] { R.id.itemTextView};
contactAdapter = new SimpleCursorAdapter(
ViewMenuListing.this, R.layout.menu_list_item, null, from, to);
setListAdapter(contactAdapter); // set contactView's adapter
}`
The only thing I changed in this code was that I used the setContentView(R.layout.viewmenu) now when I didnt before and the list would just be the content view.
Here is my viewMenu file:
`
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello" />
<ListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>`
and my menu_list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/itemTextView"
android:padding="8dp"
android:clickable = "false"
android:textSize="20sp" android:textColor="#android:color/white"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical">
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/priceTextView"
android:clickable = "false"
android:textColor="#android:color/white"/>
</LinearLayout>
Thank you for your help!
Looks like you are explicitly setting an onItemClickListener for your ListView. This really isn't necessary since you are extending ListActivity and ListActivity has a method you can override called onListItemClick(). I would override the onListItemClick() method instead of explicitly setting an onItemClickListener. http://developer.android.com/reference/android/app/ListActivity.html#onListItemClick(android.widget.ListView, android.view.View, int, long)
Please correct following things
contactListView = getListView(); and setListAdapter(contactAdapter);
The above syntax can be only used if your class is extending ListActivity .In your case you are extending Activity,So above Approach will not work.
Your menu_list_item.xml looks perfectly fine to me.
in viewmenu.xml file make following correction while specifying id for listview.
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
Please see to this corrected code.
public class SampleActivity extends ListActivity
{
public static final String ROW_ID = "row_id"; // Intent extra key
private ListView contactListView; // the ListActivity's ListView
private CursorAdapter contactAdapter; // adapter for ListView
private String tableName;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
contactListView = (ListView) findViewById(R.id.list); // get the
// built-in
// ListView
contactListView.setOnItemClickListener(this);
setContentView(R.layout.viewmenu);
Bundle extras = getIntent().getExtras();
tableName = extras.getString("table");
// map each contact's name to a TextView in the ListView layout
String[] from = new String[]
{ "name", "price" };
int[] to = new int[]
{ R.id.itemTextView, R.id.priceTextView };
contactAdapter = new SimpleCursorAdapter(SampleActivity.this,
R.layout.menu_list_item, null, from, to);
contactListView.setAdapter(contactAdapter);
}
#Override
public void onListItemClick(AdapterView<?> adapterView, View view,
int position, long arg3)
{
}
}
Now in onListItemClick method you can write whatever logic you want.
Hope this help.
I was looking for an alternative to a spinner, since the first item is always selected (which causes me issues), and I found some examples for using an AlertDialog with a list instead.
I am having two problems:
The list is displaying and is formatted ok, but there are no values in it. I know the query is returning, and the cursor/adapter has the data in it.
This may be a symptom of #1 - but when I select a blank row, the Cursor cursor2 = (Cursor) ((AdapterView) dialog).getItemAtPosition(which); statement causes a crash (it's a ClassCastException).
I had similar code previously which set the adapter to a spinner object, and the data was displaying fine.
I don't think the adapter is getting set correctly, and I have been unable to come up with a solution thus far.
Any thoughts?
Thanks!
btnDenomination.setOnClickListener(new View.OnClickListener()
{
public void onClick(View w)
{
Cursor cursor = coinDB.myDataBase.rawQuery("select _id, denomination_desc from denomination", null); // must select the _id field, but no need to use it
startManagingCursor(cursor); // required in order to use the cursor in
String[] from = new String[] {"denomination_desc" }; // This is the database column name I want to display in the spinner
int[] to = new int[] { R.id.tvDBViewRow }; // This is the TextView object in the spinner
cursor.moveToFirst();
SimpleCursorAdapter adapterDenomination = new SimpleCursorAdapter(CoinsScreen.this,
android.R.layout.simple_spinner_item, cursor, from, to );
adapterDenomination.setDropDownViewResource(R.layout.db_view_row);
new AlertDialog.Builder(CoinsScreen.this)
.setTitle("Select Denomination")
.setAdapter(adapterDenomination, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
Cursor cursor2 = (Cursor) ((AdapterView<?>) dialog).getItemAtPosition(which);
strDenomination_id = cursor2.getString(0); // Gets column 1 in a zero based index, the first column is the PKID. this could
// be avoided by using a select AS statement.
Log.d("Item Selected", strDenomination_id );
TextView txtDenomination = (TextView) findViewById(R.id.textDenomination);
txtDenomination.setText(cursor2.getString(1));
dialog.dismiss();
}
}).create().show();
}
});
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:text=""
android:id="#+id/tvDBViewRow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF0000" />
</LinearLayout>
Are you sure R.id.tvDBViewRow is the id of the TextView in the layout android.R.layout.simple_spinner_item ? From this, the TextView's id should be android.R.id.text1.
So new answer for the second issue :)
I think you should reuse the initial cursor instead of trying to get a new one... Can you try to do :
adapterDenomination.moveToPosition(which);
strDenomination_id = adapterDenomination.getString(0);
in the onClick() ?