2012-12-03 46 views
0

我想通過查詢我爲我的Android應用程序創建的SQLite數據庫來填充值列表。當用戶單擊按鈕(DROP)時,活動更改爲ListViewDelete,ListViewDelete應該具有基於ID的Select查詢中所有行的列表。我無能爲力,我該如何繼續。我嘗試了一些教程,但是我的應用程序失敗了。這裏是我的代碼:來自SQLitedatabase的Android ListView具有可點擊的項目刪除

--MySQLitehelper.java ----

public class MySQLitehelper { 

//public static final String TABLE_COMMENTS = "comments"; 
    public static final String COLUMN_ID = "GWid"; 
    public static final String COLUMN_DATE = "DateGWU"; 
    public static final String COLUMN_LOCATION = "location"; 
    public static final String COLUMN_TIME = "time"; 

    public static final String TABLE_NAME = "UPDTable"; 

    private static final String DATABASE_NAME = "UPDdb_version6"; 
    private static final int DATABASE_VERSION = 6; 

    private final Context context; 
    GetSet getset = new GetSet(); 
    public void GetIdForGwid(GetSet get) 
    { 
    getset=get; 
    } 

    // Database creation sql statement 
    private static final String DATABASE_CREATE = "CREATE TABLE " + TABLE_NAME + 
           " (" +COLUMN_ID+ " integer," + COLUMN_DATE + " VARCHAR," + 
           COLUMN_LOCATION+" VARCHAR," +COLUMN_TIME +" VARCHAR);"; 

// private static final String DATABASE_INSERT = "INSERT INTO " +TABLE_NAME + 
//            " Values (47688507,'DEC-07-2012','MARVIN 203','20:00');"; 

    private static final String DATABASE_INSERT = "INSERT INTO " +TABLE_NAME + " (" +COLUMN_ID+ " ," + COLUMN_DATE + "," + 
      COLUMN_LOCATION+" ," +COLUMN_TIME +")" + 
          " Values (47688507,'DEC-07-2012','MARVIN 203','20:00');"; 



    DatabaseHelper dbhelper; 
    SQLiteDatabase db; 




public MySQLitehelper(Context ctx) 
    { 
     this.context = ctx; 
     dbhelper = new DatabaseHelper(ctx); 
    } 



private static class DatabaseHelper extends SQLiteOpenHelper { 

    public DatabaseHelper(Context context) 
    { 
     super(context,DATABASE_NAME, null,DATABASE_VERSION); 
    } 

@Override 
public void onCreate(SQLiteDatabase db) { 
    // TODO Auto-generated method stub 
    db.execSQL(DATABASE_CREATE);   //execute create table 
    db.execSQL(DATABASE_INSERT);   //execute insert query 
    db.execSQL("INSERT INTO " +TABLE_NAME + " (" +COLUMN_ID+ " ," + COLUMN_DATE + "," +COLUMN_LOCATION+" ," +COLUMN_TIME +")" +" Values (47688507,'DEC-22-2012','OLD MAIN','23:00');"); 
    db.execSQL("INSERT INTO " +TABLE_NAME + " (" +COLUMN_ID+ " ," + COLUMN_DATE + "," +COLUMN_LOCATION+" ," +COLUMN_TIME +")" +" Values (1234567,'DEC-14-2012','FUNGER','12:00');"); 
    db.execSQL("INSERT INTO " +TABLE_NAME + " (" +COLUMN_ID+ " ," + COLUMN_DATE + "," +COLUMN_LOCATION+" ," +COLUMN_TIME +")" +" Values (7654321,'DEC-29-2012','GELMAN','22:00');"); 
    db.execSQL("INSERT INTO " +TABLE_NAME + " (" +COLUMN_ID+ " ," + COLUMN_DATE + "," +COLUMN_LOCATION+" ," +COLUMN_TIME +")" +" Values (47688507,'DEC-12-2012','IVORY','23:00');"); 
} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    // TODO Auto-generated method stub 
    Log.w(MySQLitehelper.class.getName(), 
      "Upgrading database from version " + oldVersion + " to " 
       + newVersion + ", which will destroy all old data"); 
     db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); 
     onCreate(db); 
    } 
} 


// open the DB 
public MySQLitehelper open() throws SQLException 
{ 
    System.out.println("Inside open function"); 
    db = dbhelper.getWritableDatabase(); 
    return this; 
} 

public void close() 
{ 
    dbhelper.close(); 
} 



public void insertRecord(long gwid, String date, String location, String time) 
    { 
      ContentValues initialValues = new ContentValues(); 
      initialValues.put(COLUMN_ID, gwid); 
      initialValues.put(COLUMN_DATE, date); 
      initialValues.put(COLUMN_LOCATION, location); 
      initialValues.put(COLUMN_TIME, time); 
      db.insert(TABLE_NAME, null, initialValues); 
    } 

public Cursor getAllrows()  //function to get all rows in the DB. Testing initially. 
{ 

    Cursor cur = db.rawQuery("SELECT * FROM "+TABLE_NAME, null); 
    return cur; 
} 

public Cursor getRecord(long getid) throws SQLException 
{ 
     Cursor mCursor = 
     db.query(true, TABLE_NAME, new String[] {COLUMN_ID, 
     COLUMN_DATE, COLUMN_LOCATION, COLUMN_TIME}, 
     COLUMN_ID + "= "+getid, null, null, null, null, null); //HARDCODED. Please make it dynamic 
     if (mCursor != null) 
     { 
      mCursor.moveToFirst(); 
     } 
return mCursor; 
} 

} 

--SelectOptions.java--下面的下拉按鈕(btnDrop)用戶點擊,然後控制轉到活動ListViewDelete.java其進一步低於

public class SelectOptions extends Activity { 

protected static final String EXTRA_MESSAGE = "This is a the GWID"; 
Button btnView, btnDrop, btnLocation; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_select_options); 


    final Intent intent = getIntent(); 
    //String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE); 
    //long getid = intent.getLongExtra(MainActivity.EXTRA_MESSAGE, defaultValue) 
      final Bundle extras = getIntent().getExtras(); 

     if (extras != null) { 
      long getid = extras.getLong(MainActivity.EXTRA_MESSAGE,0); 

     } 

    btnView = (Button)findViewById(R.id.btnViewShift); 
    btnDrop = (Button)findViewById(R.id.btnDropShift); 
    btnLocation = (Button)findViewById(R.id.btnViewLocation); 


    final GetSet gwid = new GetSet(); 
/* ArrayList<String> results = new ArrayList<String>(); 
     SQLiteDatabase sampleDB = null; 
     final MySQLitehelper db = null; 

     final Dialog diag = null; 
     final TextView txt = null; */ 
    final MySQLitehelper helper = new MySQLitehelper(this); 



    btnView.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 

      helper.open(); 
      Cursor c = helper.getRecord(extras.getLong(MainActivity.EXTRA_MESSAGE)); 
     // List<String> list = new ArrayList<String>(); 
     // Cursor cursor = helper.query(constantValues.TABLE_NAME, new String[] { "emailid"},null, null, null, null, null); // here emailid is the field name in the table and contantValues.TABLE_NAME is the table name 
      if (c.moveToFirst()) { 
      do { 

       System.out.println("In Do while"); 
       DisplayRecord(c); 

      } while (c.moveToNext()); 
      } 
      helper.close(); 
      System.out.println("Out of Do");     

     } 
    }); 


    btnLocation.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      Intent intent2 = new Intent(SelectOptions.this, MapViewActivity.class); 
      //startActivity(intent2); 
      startActivity(intent2); 


     } 
    }); 

    btnDrop.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Intent intent3 = new Intent(SelectOptions.this, ListViewDelete.class); 
      //startActivity(intent2); 
      intent3.putExtra(EXTRA_MESSAGE, extras.getLong(MainActivity.EXTRA_MESSAGE)); 
      startActivity(intent3); 

     } 
    }); 
} 




public void DisplayRecord(Cursor c) 
{ 
    System.out.println("In side toast display function"); 
    Toast.makeText(this, "id: "+c.getString(0)+"\n"+ 
      "Date: "+c.getString(1)+"\n"+ 
      "Location: "+c.getString(2)+"\n"+ 
      "Time: "+c.getString(3), Toast.LENGTH_LONG).show(); 
} 


} 

--ListViewDelete.java

public class ListViewDelete extends ListActivity { 


String []list = {"A","B","C","D"}; //#I just tried to populate a sample list# 




@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    //setContentView(R.layout.activity_list_view_delete); 

    final Intent intent = getIntent(); 
    final Bundle extras = getIntent().getExtras(); //gets the GWID 

    final MySQLitehelper dbhelper = new MySQLitehelper(this); 

    setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,list)); 
    Cursor c = dbhelper.getRecord(extras.getLong(MainActivity.EXTRA_MESSAGE)); 
     if (c.moveToFirst()) 
     { 
      do { 

       System.out.println("In Do while"); 
       // DisplayRecord(c); 
       //list[] 

       } while (c.moveToNext()); 
     } 
    dbhelper.close(); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.activity_list_view_delete, menu); 
    return true; 
} 

} 

上述類需要進行重組,我似乎沒有走上正軌。任何幫助,將不勝感激。謝謝

回答

2

處理像這些imo這樣的事情的最好方法是使用由cursorAdapter支持的列表視圖,而不是由另一種適配器支持的列表視圖。這裏是ListViewDelete類的重構,以包含simpleCursorAdapter。缺點是你必須知道適配器將要寫入的textViews的id。好處是你可以用適配器直接引用你的數據庫行來實現它。

import android.support.v4.widget.SimpleCursorAdapter; // < --- make sure it's this one 

... 

public class ListViewDelete extends ListActivity { 

    private MySQLitehelper dbhelper; 
    private SimpleCursorAdapter adapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_list_view_delete); 

     final Intent intent = getIntent(); 
     final Bundle extras = getIntent().getExtras(); //gets the GWID 

     dbhelper = new MySQLitehelper(this); 
     Cursor c = dbhelper.getRecord(extras.getLong(MainActivity.EXTRA_MESSAGE)); 

     // SimpleCursorAdapter adapter = new SimpleCursorAdapter(context, layout, c, from, to, flags) 
     // ^I wanted you to see the parameters 

     adapter = new SimpleCursorAdapter(
       this, 
       R.layout.delete_activity, 
       //^custom xml holding a layout to be used for each row of your listview 
       c, 
       new String[]{MySQLitehelper.COLUMN_DATE, MySQLitehelper.COLUMN_LOCATION, MySQLitehelper.COLUMN_TIME}, 
       //^an array of the database columns that you want 
       new int[]{R.id.text1, R.id.text2, R.id.text3}, 
       //^the textView ids (in your custom layout) that you wanted the columns to be mapped to 1 to 1 
       0); 
     //^just put 0 for that 


     setListAdapter(adapter); 
    } 

    private void refreshListview() { 
     Cursor c = dbhelper.getRecord(extras.getLong(MainActivity.EXTRA_MESSAGE)); 
     adapter.changeCursor(c); 
    } 

    @Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 
     super.onListItemClick(l, v, position, id); 
     // where the delete action is gonna take place 
     dbhelper.deleteRow(id); 
     // when they click a row, the database will delete by id 
     // which the onListItemClick method conveniently provides with a cursorAdapter 
     refreshListview(); 
     // then we refresh the listview to see the changes. 
     // you might want to make an "delete? are you sure?" dialog or something for your own implementations. 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_list_view_delete, menu); 
     return true; 
    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     dbhelper.close(); 
    } 
} 

而在MySQLitehelper數據庫類刪除方法被簡稱:

public boolean deleteRow(long rowId) { 
    return db.delete(TABLE_NAME, COLUMN_ID + "=" + rowId, null) > 0; 
} 
+0

感謝的人。但我以不同的方式使用它。我將SelectOptions類意圖中檢索到的值的列表傳遞給了ListViewDelete意圖,我可以成功執行此操作。但你的回答也有幫助。 – noobcoder