SQLite changing layout of list - android

Im working on the notepad tutorial from the android website. I have created a database with 2 entries: name and address. When the application is run only the Name is displayed. I want to display both Name and address. I have edited my filldata method but it doesnt work. See my code below. Any suggestions?
private void fillData() {
Cursor notesCursor = mDbHelper.fetchAllNotes();
startManagingCursor(notesCursor);
// Create an array to specify the fields we want to display in the list (TITLE + BODY)
String[] from = new String[]{NotesDbAdapter.KEY_TITLE,NotesDbAdapter.KEY_BODY};
// and an array of the fields we want to bind those fields to (in this case just text1)
int[] to = new int[]{R.id.text1};
// Now create a simple cursor adapter and set it to display
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.notes_row, notesCursor, from, to);
setListAdapter(notes);
}

In your R.layout.notes_row file you should have two TextViews one for the title and one for the address and then you will write:
String[] from = new String[]{NotesDbAdapter.KEY_TITLE,NotesDbAdapter.KEY_BODY};
// and an array of the fields we want to bind those fields to (in this case just text1)
int[] to = new int[]{R.id.text1, R.id.id_of_the_second_text};
// Now create a simple cursor adapter and set it to display
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.notes_row, notesCursor, from, to);
setListAdapter(notes);
Edit:
R.layout.notes_row could be something like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

Related

How to present cursor from two columns of the same table to SimpleCursorAdapter Android

I a developing an app whereby i retrieve data from an sqlite database and the data is displayed on a listview I am retrieving data from two columns of a specific table and it is added to a SimpleCursorAdapter which is then set to the listview i would like to display each record in the different columns to be displayed on its own row currently data from both columns is displayed in one row any help will be appreciated especially a link to a working example
i have deicded to add my code maybe this will help you help me even better
mDicCursor=managedQuery(DictionaryProvider.CONTENT_URI, null, null,
new String[] {query}, null);
if (mDicCursor == null ) {
mTextView.setText(getString(R.string.no_results, new Object[] {query}));
/* final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("WORD MISSING");*/
showDialog(DIALOG_WORDMISSING);
//new AlertDialog.Builder(this).setTitle("Argh").setMessage("Watch out!").setNeutralButton("Close", null).show();
} else {
int count = mDicCursor.getCount();
String countString = getResources().getQuantityString(R.plurals.search_results,
count, new Object[] {count, query});
mTextView.setText(countString);
/* int iWord = mDicCursor.getColumnIndexOrThrow(DictDatabase.REC_DESCRIPTION);
mTextView.setText(mDicCursor.getString(iWord));*/
// Specify the columns we want to display in the result
String[] from = new String[]{ DictDatabase.REC_WORD,
DictDatabase.REC_DESCRIPTION };
// and an array of the fields we want to bind those fields to (in this case just text1)
int[] to = new int[]{ R.id.text2,
R.id.text3};
SimpleCursorAdapter words =
new SimpleCursorAdapter(this, R.layout.dictwords, mDicCursor, from, to);
mListView.setAdapter(words);
In your cursor adapter, you can detail how the data is set up and presented on the screen. You need to ensure that every 2nd row comes from the second column. This can be done using modulus on the current row index
boolean isEvenRow = (currentRow % 2==0); // will give 0 for even rows, 1 for odd rows
And then you can use this boolean result to choose whether the data comes from Column A or Column B.
String data;
if (isEvenRow){
data = mCursor.getString(COLUMN_A);
}else{
data = mCursor.getString(COLUMN_B);
}
And then you also need to ensure that the count of total rows is going to be double the size of the cursor, so when you are returning the count, make sure you return the correct value:
return mCursor.getCount()*2;
This will allow you to scroll double the length of the cursor.
Create an another layout which is containi ng 2 textviews and place them next to each other. In your activity "R.layout.new_layout" and use this in your simplecursoradapter's constructor with your new 2 textviews id.m
Example :
SimpleCursorAdapter simple=new SimpleCursorAdapter(this, R.layout.new_layout,cursor, new String[]{column1,column2} , new int[] {textview1,textview2});

Fill each spinner row with two values from database

I have a spinner currently being filled with one column (name) from a database. I would like to change this to have each spinner row displaying the name, a space and an Id from the same table. This is the code I am using at the moment, I am not sure how to map multiple rows in the "from" array into the same textview in the "to" array.
Cursor c = db.fetchAllUsers();
startManagingCursor(c);
// create an array to specify which fields we want to display
String[] from = new String[]{"name"};
// create an array of the display item we want to bind our data to
int[] to = new int[]{android.R.id.text1};
// create simple cursor adapter
SimpleCursorAdapter adapter =
new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c, from, to );
adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
// get reference to our spinner
spinner.setAdapter(adapter);
Thanks!
Use SimpleCursorAdapter.setViewBinder with a custom ViewBinder that does what you wish. To elaborate, you can simply ignore columnIndex in your implementation of setViewValue and use the given Cursor to populate the given View however you wish.
See: http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html#setViewBinder(android.widget.SimpleCursorAdapter.ViewBinder)

Simply populate database table names in Android ListView

Very new to Android development...I've been scouring the internet for answers, but I'm still finding myself stuck and hoping someone can lend me a hand. I absolutely admit I am a newbie...but trying to learn.
I have an already populated sqlite database that I am including in a package. I've copied this into the asset folder. The database has 4 tables (and some fields beneath that).
I have a simple activity with a listview that I am trying to simply query the database for the table names and have them populate, however I get no results.
my DataBaseAdapter code is essentially the code from here:
http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
I modified my database name and path.
My class code to call this is here:
ON EDIT:::
public Cursor fetchAllNotes() {
return myDbHelper.rawQuery("SELECT name FROM sqlite_master WHERE type='table'",
new String[]{});
}
private void fillData() {
Cursor c = fetchAllNotes();
startManagingCursor(c);
String[] from = new String[] {"name"};
int[] to = new int[] {R.id.listview};
SimpleCursorAdapter notes =
new SimpleCursorAdapter (this, R.layout.list_view, c, from, to);
setListAdapter(notes);
}
}
The project runs, it just yields no results in the ListView (which is in an xml named lists.xml) the portion of code is here:
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:text="No data"
style="#style/ButtonText"/>
Any help would be great, I'm sure I am flailing hard on my sql calls...but I am stuck.
Your to and from arrays are incorrect. Your 'from' string array is currently blank. This is going to be the name of the column you are trying to display. So in your case it will be "name". Your 'to' array is going to be the id of the view you are mapping the value to...not the id of the list. You will need to create a new layout with a TextField in it to display. In the 'to' field you will then give the id of this new text field. Then in the SimpleCursorAdapter you can send in this new layout along with the 'to' and 'from' arrays.
Try this code:
private void getData() {
tb.open();
List<String> items = new ArrayList<String>();
Cursor c = tb.fetchallemployee();
c.moveToFirst();
ListAdapter adapter=new SimpleCursorAdapter(this,
R.layout.show_database, c,
new String[] {"date", "title"},
new int[] {R.id.toptext, R.id.bottomtext});
setListAdapter(adapter);
setListAdapter(entries);
tb.close();
}

How to Retrieve the data from the server

After the successful connection to the server(my local host),i want to retrieve the data that server sends to me. Can anyone please suggest me what should i do for this?
After receiving that data, i have to store it into an Array and then have to fetch it one by one into the dropdown list.
Thanks
Sheetal i have not worked with any application with server,but i retrieved data locally and then placed it in an array and showed in the spinner, this might help u
Cursor c = db.fetchAllReminders();
startManagingCursor(c);
// Create an array to specify the fields we want (only the TITLE)
String[] from = new String[]{DataManager.NAME};
// and an array of the fields we want to bind in the view
int[] to=new int[] { android.R.id.text1 };
// to[0]=android.R.id.text1;
final Spinner spinner = (Spinner) this.findViewById(R.id.spins);
// Now create a simple cursor adapter and set it to display
final SimpleCursorAdapter reminders =new SimpleCursorAdapter(this,
android.R.layout.simple_spinner_item, c, from, to);
spinner.setAdapter(reminders);

Populating a spinner with a db query

I recently started programming for android and java development in general. Currently I train by writing a timetable app.
I want to get a list with all subjects from the db (the table also contains teacher and rooms) and put them into a spinner.
Thats the code I wrote
Cursor c = dba.fetchAllSubjects();
if (c.getCount() != 0) {
Spinner subjectSpinner = (Spinner) findViewById(R.id.newlesson_subject);
startManagingCursor(c);
String[] from = new String[]{DbAdapter.KEY_SUBJECT};
int[] to = new int[] {android.R.layout.simple_spinner_item};
SimpleCursorAdapter subjectAdapter =
new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c, from, to);
subjectSpinner.setAdapter(subjectAdapter);
}
The problem is that when clicking on the spinner it shows a list with as much items as the db has entries, but the list doesn't show the names (you can say it's empty). So I probably messed something up with the simple cursor adapter, but I don't know what.
Thanks for your help
You're error lies in specifying the wrong Resource ID in your to integer array.
int[] to = new int[] {android.R.layout.simple_spinner_item};
You need to specify the ID of a TextView (or similarly typed view) and not the layout itself. Replace the line above with:
int[] to = new int[] { android.R.id.text1 };

Categories

Resources