What am I doing wrong!?!??
I am trying to get a set of data to a listview.
First I Open the database and then I trying to get the set,
I get a response but only 1 row from the database and I am getting at least 10 when I trying it in sqlite browser.....
Anyway I don't know if this make any sense, here is the code: (i am new at this, please don't laugh to much)
And btw I use the same methods/functions in another listview but then I don't have any WHERE in my query and that works fine....
So I want to get all the rows from the database and i only get the first one :)
Thanks mates!
db.openDataBase();
Cursor c = db.getCoursesFromCountyID(countyID);
BindsimpleCursorAdapter(c);
db.close();
the getCoursesFromCountyID =
int id = Integer.parseInt(county);
return db.query("Courses", new String[]{KEY_course_ID, KEY_course}, KEY_county_ID + " = " + id, null, null, null, null);
And the BindsimpleCursorAdapter looks like this =
startManagingCursor(c);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.simpleadapter_courses, c, new String[]{DataBaseHelper.KEY_course_ID, DataBaseHelper.KEY_course}, new int[]{R.id.courseID, R.id.course});
ListView lv = new ListView(this);
lv = (ListView)findViewById(R.id.listViewCourses);
lv.setAdapter(adapter);
I don't see any problems on your code, try adding 'KEY_county_ID' to you projection argument and removing the selection argument to check if all the rows are really there.
Related
I have two tables atm, users and notes. I am trying to retrieve data that belongs to the user. So all data to list must be owned by the original user and shown only to him. I have made my table in Databasehelper.
I have made a new class that controls the notes table. In listNotes() I want to loop through the cursor row and get all data owned by the user. Am I quering it correctly?
// Listing all notes
public Cursor listNotes() {
Cursor c = db.query(help.NOTE_TABLE, new String[]{help.COLUMN_TITLE,help.COLUMN_BODY, help.COLUMN_DATE}, null, null, null, null, null);
if (c != null) {
c.moveToFirst();
}
db.close();
return c;
}
I then want to display the cursor data collected in a listview
public void populateList(){
Cursor cursor = control.listNotes();
getActivity().startManagingCursor(cursor);
//Mapping the fields cursor to text views
String[] fields = new String[]{help.COLUMN_TITLE,help.COLUMN_BODY, help.COLUMN_DATE};
int [] text = new int[] {R.id.item_title,R.id.item_body, R.id.item_date};
adapter = new SimpleCursorAdapter(getActivity(),R.layout.list_layout,cursor, fields, text,0);
//Calling list object instance
listView = (ListView) getView().findViewById(android.R.id.list);
adapter.notifyDataSetChanged();
listView.setAdapter(adapter);
}
You aren't creating the NOTE_TABLE right.
You miss a space and a comma here
+ COLUMN_DATE + "DATETIME DEFAULT CURRENT_TIMESTAMP"
It has to be
+ COLUMN_DATE + " DATETIME DEFAULT CURRENT_TIMESTAMP,"
There are two issues here:
One is you have missed a comma (after the Timestamp as specified in an earlier answer).
The other error you have is when using a SimpleCursorAdapter, you need to ensure that the Projection string array includes something to index the rows uniquely and this must be an integer column named as "_id". SQLite already has a feature built in for this and provides a column named "_id" for this purpose (however you can have your own integer column which you can rename to _id). To solve this, change your projection string array to something like:
new String[] {"ROW_ID AS _id", help.COLUMN_TITLE,help.COLUMN_BODY, help.COLUMN_DATE}
I guess the NullPointerException stems from this (but without the stacktrace I don't know for sure).
I have an activity page which has textview fields(name , phoneNo) and some buttons. I query the database to get the details of a single customer(1 record). A cursor is returned. I want to show the values(name , phoneNo) returned in the textviews of this activity. How can I do that without using a listView. I don't need a list because only a single record is returned. It's like a profile page. But to use a cursor I think I need somekind of listView. How to do this ???? Thankz in advance
String[] fromColumns = {TakenJobDataBaseOpenHelper.TAKENJOBS_COLUMN_RequestID,TakenJobDataBaseOpenHelper.TAKENJOBS_COLUMN_Destination,TakenJobDataBaseOpenHelper.TAKENJOBS_COLUMN_CustomerName};
int[] toViews = {R.id.textView1, R.id.textView3,R.id.CustomerName};
TakenJobDataBaseOpenHelper jobDatabaseHelper = new TakenJobDataBaseOpenHelper(this);
Cursor cursor = jobDatabaseHelper.getSingleRecord(id);
SimpleCursorAdapter cursoradapter = new SimpleCursorAdapter(this,
R.layout.activity_job_details, cursor, fromColumns, toViews, 0);
you don't have to use a listview. simply use it like this:
if (cursor.moveToFirst()) {
String name = cursor.getString(cursor.getColumnIndex('NAME'));
String phoneNo = cursor.getString(cursor.getColumnIndex('phoneNo'));
((TextView)findViewById(R.id.textView1)).setText(name);
((TextView)findViewById(R.id.textView2)).setText(phoneNo);
} else {
// nothing found!
}
I have 2 activities(Novamensagem & Mensagenssalva) in Novamensagem.java I have a spinner with contacts values, a EditText and a Save button. I select a contact and write a text and hit save. The TEXT gets saved, and when i open Mensagenssalva.java all the TEXTs i write and save is there in a ListView. So i want to know how to the ListView show the name of the contact i selected in the Spinner and then show the message. eg:
Person's name
Message i have written.
//EDIT//
now the error is: Force to Close the app when i compile it. The code now:
ListView user = (ListView) findViewById(R.id.lvShowContatos);
//String = simple value ||| String[] = multiple values/columns
String[] campos = new String[] {"nome", "telefone"};
list = new ArrayList<String>();
c = db.query( "contatos", campos, null, null, null, null, null);
c.moveToFirst();
if(c.getCount() > 0) {
while(true) {
list.add(c.getString(c.getColumnIndex("nome")).toString());
list.add(c.getString(c.getColumnIndex("telefone")).toString());
if(!c.moveToNext()) break;
}
}
// the XML defined views which the data will be bound to
int[] to = new int[] { R.id.nome_entry, R.id.telefone_entry };
SimpleCursorAdapter myAdap = new SimpleCursorAdapter(this, R.layout.listview, c , campos, to, 0);
user.setAdapter(myAdap);
The LogCat errors:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mensagem/com.example.mensagem.Contato}: java.lang.IllegalArgumentException: column '_id' does not exist
So the thing is, its trying to pull a "_id" from my database, but i dont have a "_id" column/row in it.
Posted from comments
You will have more control over your app while writing less lines of code by using a SimpleCursorAdapter as we discussed.
In order to use any CursorAdapter, your table must have a _id INTEGER PRIMARY KEY column, which you don't have. While I still recommend altering your table to add this column, there is a quick fix. If you don't specify a primary key all SQLite tables create an integer primary key by default, you can reference it with rowid, oid or _rowid_. But Android requires that the integer primary key column is named _id... Simply create an alias with the keyword AS for the meantime:
String[] campos = new String[] {"rowid as _id", "nome", "telefone"};
You need to create customAdapter instead of ArrayAdapter. Check out this link http://devtut.wordpress.com/2011/06/09/custom-arrayadapter-for-a-listview-android/
I'm having a lot of problems trying to get my spinner to fill with data after my application loads. I'm just messing around and made a database for vehicles. So I'm trying to get a spinner to load the database data into it. This is the code I use in a method to attempt to do so, but my spinner is never loaded with my database data.
SQLiteDatabase db = database.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT vehicle_make, vehicle_model, vehicle_year FROM vehicles;", null);
vehicleProfileSpinner = (Spinner) findViewById(R.id.vehicleProfileSpinner);
startManagingCursor(cursor);
String[] columns = {"vehicle_year", "vehicle_make", "vehicle_model"};
int[] theView = {R.id.vehicleProfileSpinner};
SimpleCursorAdapter vpsAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_dropdown_item, cursor, columns, theView);
vpsAdapter.setDropDownViewResource(R.layout.main);
vehicleProfileSpinner.setAdapter(vpsAdapter);
In my case instead of using Simplecursoradapter, I have created array from cursor entries and created ArrayAdapter and set it to list. It worked perfect. Somehow Simplecursor adapter looks for ID field and causing issue,so I ignored it and used Arrayadapter. I will try to update this post with example I posted for other question.
One thing is all you need SimpleCusorAdapter.
I am trying to populate a spinner with data from a database. The code is in a subroutine. Everything executes but the spinner is empty. If I copy the code to the "onCreate(Bundle saveInstanceState) it works fine. I am thinking I have context issue but can not figure it out.
Here is the subroutine:
public void showMatchNames (){
Cursor c;
String sMatchName = "MatchTable";
c = db.query(sMatchName, new String[]{KEY_ROWID, KEY_TITLE}, null, null, null, null, null);
if(c.moveToFirst()){
SimpleCursorAdapter MatchAdapter = new SimpleCursorAdapter(this,android.R.layout.simple_spinner_item, c, new String[]{KEY_TITLE}, new int[]{android.R.id.text1});
MatchAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner spin2 = (Spinner)this.findViewById(R.id.spinner2);
spin2.setAdapter(MatchAdapter);
}
scrMatchView.setOnItemSelectedListener(new mySpinnerListener());
c.close(); // Done with Cursor so close it
}
There is data in the database, confirmed by exporting to SQliteMan and doing a query.
I can also pull the names from the database and populate the spinner with a simple ArrayAdapter from the "showMatchNames" subroutine.
I am pretty sure it is something simple but I have been staring at this for hours now and can't get it to work.
Any help would be very welcome.