Note that there are some explanatory texts on larger screens.

plurals
  1. POObstacle for retrieving data from SQLite database into a ListView(having TextViews) in Android:
    text
    copied!<p>I have faced some problems when I want to retrive data from db to some textviews and then put those textvies into a listview.Let me explain I have two columns like date and head in my database.And I want to retrieve those into their each textviews and put these textvies into a listview.I know this may be a silly question for others,but i am new in this field.i know something about SimpleCursorAdapter but dont know how to and where to use it.It may be solve through this.So please guide me.</p> <p>My Classes::I have created a DBAdapter class for database.MyArrayAdapter class. And a main class calles as Head.java.</p> <p><strong>DBAdapter.java</strong></p> <pre><code> public class DBAdapter { //EditText mHead; public static final String KEY_ROWID="_id"; public static final String KEY_DATE="date"; public static final String KEY_HEAD="head"; private static final String TAG="DBAdapter"; private static final String DATABASE_NAME="accounting"; private static final String DATABASE_TABLE="accounts"; private static final int DATABASE_VERSION=1; private static final String DATABASE_CREATE= "create table accounts (_id integer primary key autoincrement," + "date datetime not null, head text not null);"; private final Context context; private DataBaseHelper DBHelper; private SQLiteDatabase db; public DBAdapter(Context ctx){ this.context=ctx; DBHelper=new DataBaseHelper(context); } private static class DataBaseHelper extends SQLiteOpenHelper { DataBaseHelper(Context context){ super(context,DATABASE_NAME,null,DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub try { db.execSQL(DATABASE_CREATE); }catch(SQLException e){ e.printStackTrace(); } } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub Log.w(TAG, "Updrading database from version "+oldVersion+" to "+newVersion+"," + "which will destroy all old data"); db.execSQL("drop table if exists accounts"); onCreate(db); } } public DBAdapter open() throws SQLException{ db=DBHelper.getWritableDatabase(); return this; } public void close(){ DBHelper.close(); } **//--insert contact in the database--** public long insertContact(String date,String head) { ContentValues initialvalue=new ContentValues(); initialvalue.put(KEY_DATE, date); initialvalue.put(KEY_HEAD, head); return db.insert(DATABASE_TABLE, null, initialvalue); } //--delete a particular contact-- public boolean deleteContact(long rowId){ return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null)&gt;0; } //--Retrieving all the contacts-- public Cursor getAllContacts(){ return db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_DATE,KEY_HEAD}, null, null, null, null,null); } //--Retrieve a particular contact-- public Cursor getContact(long rowId)throws SQLException{ Cursor mCursor= db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,KEY_DATE,KEY_HEAD}, KEY_ROWID + "="+ rowId, null, null, null, null, null); if(mCursor != null){ mCursor.moveToFirst(); } return mCursor; } //--update a contact-- public Boolean updateContact(long rowId,String date,String head){ ContentValues args=new ContentValues(); args.put(KEY_DATE, date); args.put(KEY_HEAD, head); return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" +rowId,null)&gt;0; } } </code></pre> <hr> <p><strong>MyArrayAdapter.java</strong></p> <pre><code>public class MyArrayAdapter extends ArrayAdapter&lt;Object&gt;{ private final Context context; private final String[] values; //@SuppressWarnings("unchecked") public MyArrayAdapter(Context context,String[] values){ super(context,R.layout.list_things,values); this.context=context; this.values=values; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub //return super.getView(position, convertView, parent); LayoutInflater inflate=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View rowview=inflate.inflate(R.layout.list_things, parent, false); TextView mDate=(TextView)rowview.findViewById(R.id.tvdate); TextView mHead=(TextView)rowview.findViewById(R.id.tvhead); mDate.setText(values[position]); mHead.setText(values[position]); return rowview; } </code></pre> <p>}</p> <p><strong>At last the Head class where I want to do all things</strong></p> <p><strong>Head.java</strong></p> <pre><code>public class Head extends ListActivity { String date = ""; String head; TextView display; EditText headname; ListView list; LayoutInflater inflate; Layout layout; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.head); display=(TextView)findViewById(R.id.tvCurrentDate); display.setText(date); // Intent intent=new Intent(); DBAdapter db=new DBAdapter(this); SimpleCursorAdapter adapter=new SimpleCursorAdapter(Head.this, R.layout.list_things, null, null, null); //Cursor cursor=getContentResolver().query("/data/data/com.crypto.ranjit/DBAdapter", //new String []{db., selection, selectionArgs, sortOrder) Bundle b = getIntent().getExtras(); if (b != null) { date = b.getString("date"); } Button btnSave = (Button) findViewById(R.id.btnSave); btnSave.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub headname=(EditText)findViewById(R.id.etHeadname); date=display.getText().toString(); head=headname.getText().toString(); DBAdapter dba = new DBAdapter(Head.this); dba.open(); long id = dba.insertContact(date, head); // id=dba.insertContact(head, date); dba.close(); } }); //DBAdapter db=new DBAdapter(Head.this); //db.open(); } </code></pre> <p>}</p>
 

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