Reading data from database and binding them to custom ListView
Posted
by
N.K.
on Stack Overflow
See other posts from Stack Overflow
or by N.K.
Published on 2012-10-20T16:54:34Z
Indexed on
2012/10/20
17:00 UTC
Read the original article
Hit count: 238
I try to read data from a database i have made and to show some of the data in a row at a custom ListView. I can not understand what is my mistake.
This is my code:
public class EsodaMainActivity extends Activity
{
public static final String ROW_ID = "row_id"; //Intent extra key
private ListView esodaListView; // the ListActivitys ListView
private SimpleCursorAdapter esodaAdapter; // adapter for ListView
DatabaseConnector databaseConnector = new DatabaseConnector(EsodaMainActivity.this);
// called when the activity is first created
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_esoda_main);
esodaListView = (ListView)findViewById(R.id.esodaList);
esodaListView.setOnItemClickListener(viewEsodaListener);
databaseConnector.open();
//Cursor cursor= databaseConnector.query("esoda", new String[]
// {"name", "amount"}, null,null,null);
Cursor cursor=databaseConnector.getAllEsoda();
startManagingCursor(cursor);
// map each esoda to a TextView in the ListView layout
// The desired columns to be bound
String[] from = new String[] {"name","amount"}; // built an String array named "from"
//The XML defined views which the data will be bound to
int[] to = new int[] { R.id.esodaTextView, R.id.amountTextView}; // built an int array named "to"
// EsodaMainActivity.this = The context in which the ListView is running
// R.layout.esoda_list_item = Id of the layout that is used to display each item in ListView
// null =
// from = String array containing the column names to display
// to = Int array containing the column names to display
esodaAdapter = new SimpleCursorAdapter (this, R.layout.esoda_list_item, cursor, from, to);
esodaListView.setAdapter(esodaAdapter); // set esodaView's adapter
} // end of onCreate method
@Override
protected void onResume()
{
super.onResume(); // call super's onResume method
// create new GetEsodaTask and execute it
// GetEsodaTask is an AsyncTask object
new GetEsodaTask().execute((Object[]) null);
} // end of onResume method
// onStop method is executed when the Activity is no longer visible to the user
@Override
protected void onStop()
{
Cursor cursor= esodaAdapter.getCursor(); // gets current cursor from esodaAdapter
if (cursor != null)
cursor.deactivate(); // deactivate cursor
esodaAdapter.changeCursor(null); // adapter now has no cursor (removes the cursor from the CursorAdapter)
super.onStop();
} // end of onStop method
// this class performs db query outside the GUI
private class GetEsodaTask extends AsyncTask<Object, Object, Cursor>
{
// we create a new DatabaseConnector obj
// EsodaMainActivity.this = Context
DatabaseConnector databaseConnector = new DatabaseConnector(EsodaMainActivity.this);
// perform the db access
@Override
protected Cursor doInBackground(Object... params)
{
databaseConnector.open();
// get a cursor containing call esoda
return databaseConnector.getAllEsoda();
// the cursor returned by getAllContacts() is passed to method onPostExecute()
} // end of doInBackground method
// here we use the cursor returned from the doInBackground() method
@Override
protected void onPostExecute(Cursor result)
{
esodaAdapter.changeCursor(result); // set the adapter's Cursor
databaseConnector.close();
} // end of onPostExecute() method
} // end of GetEsodaTask class
// creates the Activity's menu from a menu resource XML file
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.esoda_menu, menu); // inflates(eµf?s?) esodamainactivity_menu.xml to the Options menu
return true;
} // end of onCreateOptionsMenu() method
//handles choice from options menu - is executed when the user touches a MenuItem
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// creates a new Intent to launch the AddEditEsoda Activity
// EsodaMainActivity.this = Context from which the Activity will be launched
// AddEditEsoda.class = target Activity
Intent addNewEsoda = new Intent(EsodaMainActivity.this, AddEditEsoda.class);
startActivity(addNewEsoda);
return super.onOptionsItemSelected(item);
} // end of method onPtionsItemSelected()
// event listener that responds to the user touching a esoda's name in the ListView
OnItemClickListener viewEsodaListener = new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
// create an intent to launch the ViewEsoda Activity
Intent viewEsoda = new Intent(EsodaMainActivity.this, ViewEsoda.class);
// pass the selected esoda's row ID as an extra with the Intent
viewEsoda.putExtra(ROW_ID, arg3);
startActivity(viewEsoda); // start viewEsoda.class Activity
} // end of onItemClick() method
}; // end of viewEsodaListener
} // end of EsodaMainActivity class
The statement: Cursor cursor=databaseConnector.getAllEsoda();
queries all data (columns)
From the data I want to show at my custom ListView 2 of them: "name" and "amount".
But I still get a debugger error.
Please help.
© Stack Overflow or respective owner