0

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.

baTimá
  • 554
  • 1
  • 10
  • 28
  • 2
    I'm going to suggest this again because you are still doing this the hard way. You need to learn about CursorAdapters. Read this [SimpleCursorAdapter tutorial](http://thinkandroid.wordpress.com/2010/01/09/simplecursoradapters-and-listviews/). – Sam Sep 27 '12 at 18:28
  • thanks its really helping me. haha i know it still hard to me but they sometimes teach many things you not gonna use. – baTimá Sep 27 '12 at 18:39
  • ok, so i followed it but a line in my code says that is deprecated: `The constructor SimpleCursorAdapter(Context, int, Cursor, String[], int[]) is deprecated` – baTimá Sep 27 '12 at 18:46
  • I assume that you are not using fragments, so you can simply add `, 0` as the last parameter to use the supported [SimpleCursorAdapter()](http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html#SimpleCursorAdapter%28android.content.Context,%20int,%20android.database.Cursor,%20java.lang.String[],%20int[],%20int%29) – Sam Sep 27 '12 at 18:53
  • Ok, thanks check edit in question for new error :x – baTimá Sep 27 '12 at 18:56
  • If your app crashes you should always post your LogCat errors. – Sam Sep 27 '12 at 19:00
  • `java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mensagem/com.example.mensagem.Contato}: java.lang.IllegalArgumentException: column '_id' does not exist` but as you can see in the EDIT code, there is no '_id' mentioned, so i dont know what it is? – baTimá Sep 27 '12 at 19:02
  • btw i do guess is only this line of the LogCat but if you think is not .. ? – baTimá Sep 27 '12 at 19:16
  • 1
    [This question](http://stackoverflow.com/q/3359414/1267661) talks about this error in detail. You need to have the the [SQLite `ROWID`](http://www.sqlite.org/lang_createtable.html#rowid) column included in your query. If your table already uses `_id INTEGER PRIMARY KEY` then simply change `campos` in your query to `String[] campos = new String[] {"_id", "nome", "telefone"};` but use another String array for the SimpleCursorAdapter that is only `new String[] {"nome", "telefone"}` – Sam Sep 27 '12 at 22:39
  • Right, but i dont have a `ROWID` in my database. So how should i do mine? Then i added: `"_id"` to the `String[] campos = new String[] {"_id", "nome", "telefone"};` but still doens't work – baTimá Sep 28 '12 at 16:46
  • 1
    You should alter the table to add the column, but how to do that is out of the scope of this question. However the SQLite link I provided describes how _every_ SQLite table has a integer primary key if no other key was provided. Maybe this will work for you `String[] campos = new String[] {"rowid as _id", "nome", "telefone"};` – Sam Sep 28 '12 at 16:57
  • ok, so i tried the code you provided but, still force to close application, LogCat: `column 'rowid as _id' does not exist` – baTimá Sep 28 '12 at 16:58
  • ins't all of this problem in my Cursor? is it right? c.moveToFirst... ? – baTimá Sep 28 '12 at 17:03
  • it Worked :D i did what you said, `String[] campos = new String[] {"rowid as _id", "nome", "telefone"};` and `new String[] {"nome", "telefone"}` for the SimpleCursorAdapter., post it as answer so i can accept it. – baTimá Sep 28 '12 at 17:13

2 Answers2

1

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"};
Sam
  • 86,580
  • 20
  • 181
  • 179
0

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/

Martin Vandzura
  • 3,047
  • 1
  • 31
  • 63