0 Replies - 275 Views - Last Post: 01 July 2013 - 03:38 AM

#1 reiddwyer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 01-July 13

Clicking a listview, then opening an activity to query a database

Posted 01 July 2013 - 03:38 AM

What i need to do in this android app that I am creating is to click one of the list view items that I am displaying from my primary key id column. I need the app to grab the number or text from the list view item which has been clicked then open a new list view or activity to query the database "WHERE FIELDS EQUAL clicklistview" something along those lines im guessing. Then I want the results of that query displayed. And also the ability to delete the records. The code below shows the activity which displays the listview
public class View_Saved_Dates extends Activity

	ListView lv;

    protected void onCreate(Bundle savedInstanceState) {
        lv = (ListView)findViewById(R.id.cow_id_list_view);
        ActionBar actionBar = getActionBar();
        actionBar.setTitle("Saved Cattle"); 
        Database info = new Database(this);
        String [] cowid = info.getCowid();
        ArrayAdapter<String> arrayAdapter =  new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, cowid);

This code below is the code of my database where data is entered and I am guessing a new method would be created here to query the databse using the listview selected.
public class Database {
	public static final String KEY_COWID = "cow_id";
	public static final String KEY_INSEMINATION_DATE = "insemination_date";
	public static final String KEY_CALVING_DATE = "calving_date";
	private static final String DATABASE_NAME = "db";
	static final String DATABASE_TABLE = "calving_dates_table";
	private static final int DATABASE_VERSION = 2;
	private Dbhelper ourHelper;
	private final Context ourContext;
	private SQLiteDatabase ourDatabase;

	private static class Dbhelper extends SQLiteOpenHelper{

		public Dbhelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
			// TODO Auto-generated constructor stub

		public void onCreate(SQLiteDatabase db) {
			// TODO Auto-generated method stub
			db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
					KEY_CALVING_DATE + " TEXT);"				

		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub
	public Database(Context c){
		ourContext = c;
	public Database open() throws SQLException{
		ourHelper = new Dbhelper(ourContext);
		ourDatabase = ourHelper.getWritableDatabase();
		return this;
	public void close(){

	public long createEntry(String cowid, String dpdate, String calvedate) {
		// TODO Auto-generated method stub
		ContentValues cv = new ContentValues();
		cv.put(KEY_COWID, cowid);
		cv.put(KEY_INSEMINATION_DATE, dpdate);
		cv.put(KEY_CALVING_DATE, calvedate);
		return ourDatabase.insert(DATABASE_TABLE, null, cv);

	public String[] getCowid() {
		// TODO Auto-generated method stub
		ArrayList<String> strings = new ArrayList<String>();
		String[] cowid = new String[]{KEY_COWID};
		Cursor c = ourDatabase.query(DATABASE_TABLE, cowid, null, null, null, null, null);
		int iCow = c.getColumnIndex(KEY_COWID);
		while(c.moveToNext()){ strings.add(c.getString(iCow));
		String[] mString = (String[]) strings.toArray(new String[strings.size()]);
		return mString;
	public String getInseminationdate() {
		// TODO Auto-generated method stub
		String[] cowid = new String[]{KEY_INSEMINATION_DATE};
		Cursor c = ourDatabase.query(DATABASE_TABLE, cowid, null, null, null, null, null);
		String result = "";
		int iInseminationdate = c.getColumnIndex(KEY_INSEMINATION_DATE);
		while(c.moveToNext()){ result = result + c.getString(iInseminationdate) +"\n";
		return result;
	public String getCalvingdate() {
		// TODO Auto-generated method stub
		String[] cowid = new String[]{KEY_CALVING_DATE};
		Cursor c = ourDatabase.query(DATABASE_TABLE, cowid, null, null, null, null, null);
		String result = "";
		int iCalvingdate = c.getColumnIndex(KEY_CALVING_DATE);
		while(c.moveToNext()){ result = result + c.getString(iCalvingdate) +"\n";
		return result;

Is This A Good Question/Topic? 0
  • +

Page 1 of 1