2

i have this code for custom list.

the my_list.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="wrap_content"
     android:orientation="vertical">

     <TextView android:id="@+id/text1"
         android:textSize="16px"
         android:textStyle="bold"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"/>

     <TextView android:id="@+id/text2"
         android:textSize="12px"
         android:textStyle="italic"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"/>
</LinearLayout>

and the activity:

public class ZcustListActivity extends ListActivity {


//db Work
SQLiteDatabase db;
String SQL;
ArrayList<String> db_results=null;


    private SimpleAdapter notes;

    ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    //db Work
    this.db = this.openOrCreateDatabase("DEMO", MODE_PRIVATE, null);
    this.db.execSQL("CREATE TABLE IF NOT EXISTS MEN(A VARCHAR,B VARCHAR,C VARCHAR);");
    this.db.execSQL("delete from MEN;");
    db.execSQL("INSERT INTO MEN(A,B,C) VALUES('AA1','BB1','CC1');");
    db.execSQL("INSERT INTO MEN(A,B,C) VALUES('DD2','EE2','FF2');");


        notes = new SimpleAdapter(
                this, 
                list,
                R.layout.my_list,
                new String[] { "line1","line2" },
                new int[] { R.id.text1, R.id.text2 }  );
       setListAdapter( notes );

      HashMap<String,String> item = new HashMap<String,String>();
      item.put( "line1","i am row 1" );
      item.put( "line2","i am row 2" );
      list.add( item );
       notes.notifyDataSetChanged();
    }
}

i add my database work - but how to combine this ?

i'am new on android and it dont work for me......

Gali
  • 14,511
  • 28
  • 80
  • 105

2 Answers2

2

I'm guessing you already have a DBManager class. You call in the onCreate a populateList method with a SimpleCursorAdapter:

 private DBManager dbM = new DBManager();
 private Cursor c;

 public void populateList(){
       dbM.open();
       c = this.db.query(MEN, new String[] {"line1", "line2"
            }, null, null, null, null, null);
   startManagingCursor(c);

   String[] from = new String[]{"line1","line2" };

       int[] to = new int[]{R.id.text1, R.id.text2};

SimpleCursorAdapter notes = 
        new SimpleCursorAdapter (this, R.layout.list_row, c, from, to);
    setListAdapter(notes);

}
 }

Also, you need to create a separate view for your list_row (which is used in the SimpleCursorAdapter constructor.) so define it (any layout you want will do, in the case in question it would be a LinearLayout with 2 textViews) with the data you need to put in each row of the table.

Th0rndike
  • 3,406
  • 3
  • 22
  • 42
  • Just initialize the cursor with the lines you need for the db and apply the code i wrote above. for example: Cursor c = db.exeSQL(SELECT * FROM MEN); – Th0rndike Mar 28 '12 at 09:58
  • i try this, and got error on: c = this.db.fetchAllNotes(); it say: The method fetchAllNotes() is undefined for the type SQLiteDatabase – Gali Mar 28 '12 at 10:57
  • thanks again for your Patience, but i got error on: c = this.db.execSQL("SELECT * FROM MEN;"); it say: Type mismatch: cannot convert from void to Cursor – Gali Mar 28 '12 at 13:55
  • Everything you need to make the list from your db is in that code. All you need to do is figure out how to get the data from the db to the cursor. Make an effort to understand how to interrogate your db and put the results in a cursor, i bet you will be able to make it on your own if you do. I mean, the code is ALMOST ready to use, just make slight modifications until you make it work, it's not like we are here to work for you. – Th0rndike Mar 28 '12 at 14:21
  • 1
    I really appreciate your help, i make my first steps on android (from .net programer) i really need to solve this and i dont know how. i search in the web and I found lots of material - But I got lost. please if you can help me with this I'll be grateful – Gali Mar 28 '12 at 14:36
  • http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html <- there's everything you need to make it work, use query or rawQuery instead of execSQL which is not useable with queries that return data – Th0rndike Mar 28 '12 at 14:39
  • I hate this site (android developers), Can anyone point out what I'm not doing well in my code ? – Gali Mar 28 '12 at 14:45
  • are you kidding me? i just told you: execSQL will not work for queries that return data. You need to use rawQuery() or query(). I modified the answer again. – Th0rndike Mar 28 '12 at 14:52
  • i try this: c = this.db.query("MEN", new String[] {"line1", "line2"}, null, null, null, null, null); and got error: android.database.sqlite.SQLiteException: no such column: line1: , while compiling: SELECT line1, line2 FROM MEN ====>>>>> i try this: c = db.rawQuery("select A,B,C from MEN", null); and got this error: java.lang.IllegalArgumentException: column '_id' does not exist – Gali Mar 28 '12 at 15:22
  • try c = this.db.query("MEN", new String[] {"A", "B"}, null, null, null, null, null); //this is to get columns A and B just write the columns from your db that you need inside the list in the new String[]{HERE!} – Th0rndike Mar 28 '12 at 15:33
  • i change to: c = this.db.query("MEN", new String[] {"A", "B"}, null, null, null, null, null); --> i have in my database column A,B and C --> but still got error: java.lang.IllegalArgumentException: column '_id' does not exist – Gali Mar 28 '12 at 15:40
-1

SimpleCursorAdapter is what you're looking for

http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html

dldnh
  • 8,923
  • 3
  • 40
  • 52