Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to show data from cursor into a listview
    text
    copied!<p>I'm new to android and I'm stuck in a problem.<br> This is my main class and simply inserting the data and showing the data in Logcat. I want to show the data in ListView in a new activity. Now I am not getting how to get data from cursor and show it on ListView . I have made a <code>view.xml</code> &amp; <code>view.java</code> to show data but I don't know how to show data on that activity. Should I make an adapter class, cursoradapter or flater, etc. (I want to know the next steps).</p> <p>Thanks in advance :)</p> <p><strong>Main Class</strong> </p> <pre><code> public class main extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); addListenerOnButton(); database_delete(); database_add(); } public void database_add() { try{ Database_handler db = new Database_handler(this); Log.d("Insert: ", "Inserting .."); db.addData(new get_set(131,"A-Lucy")) ; db.addData(new get_set(132,"A-kk")) ; db.addData(new get_set(133,"A_Tweety")) ; db.addData(new get_set(134,"A-Naruto")) ; Log.d("Reading: ", "Reading all contacts.."); List&lt;get_set&gt; getSetList = db.getalldata(); for(get_set set : getSetList){ String log = "CODEE: " + set.getCode() + ", Description: " + set.getDescr(); Log.d("CODE: ", log); } </code></pre> <p>here is some code of my database handler class having functionality of inserting,retrieving data.</p> <p><strong>Database handler class</strong></p> <pre><code>public class Database_handler extends SQLiteOpenHelper { private static final int DATABASE_Version = 1; private static final String DATABASE_Name = "android_db"; private static final String TABLE_Name = "all_data"; private static final String Column_Name_code = "code"; private static final String Column_name_desc = "description"; private SQLiteDatabase db; Database_handler(Context cont){ super(cont,DATABASE_Name,null,DATABASE_Version); } @Override public void onCreate(SQLiteDatabase db) { String CREATE_TABLE_ANDRO = " CREATE TABLE " + TABLE_Name + "(" + Column_Name_code + " INTEGER PRIMARY KEY , " + Column_name_desc + " TEXT )"; db.execSQL(CREATE_TABLE_ANDRO); } @Override public void onUpgrade(SQLiteDatabase db, int Older_version, int new_version) { db.execSQL("DROP TABLE IF EXISTS " + TABLE_Name ); onCreate(db); } // Inserting record public void addData (get_set getSet){ SQLiteDatabase db = this.getReadableDatabase(); ContentValues cv = new ContentValues(); cv.put (Column_Name_code, getSet.getCode()); cv.put(Column_name_desc, getSet.getDescr()); db.insert(TABLE_Name,null,cv); db.close(); } Retrieving data public List&lt;get_set&gt; getalldata(){ List&lt;get_set&gt; data_list = new ArrayList&lt;get_set&gt;(); String select_query = "SELECT * FROM " + TABLE_Name; SQLiteDatabase db = this.getReadableDatabase(); Cursor cur = db.rawQuery(select_query,null); if (cur.moveToFirst()){ do { get_set getsetObj = new get_set(); getsetObj.setCode(Integer.parseInt(cur.getString(0))); getsetObj.setDescr(cur.getString(1)); data_list.add(getsetObj); } while (cur.moveToNext()); } return data_list; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload