2015-04-01 164 views
0

好吧,所以我在這裏是新的,我知道這個問題已被問了很多次,但我似乎無法得到任何工作,我可以得到一些樣本,我發現工作罰款在一個列表視圖,但如果我嘗試修改或重新創建我只是似乎無法得到它的工作,所以即時通訊curreouse至於我做錯了我的代碼....對不起,格式不好。在列表視圖中添加和從SQLite數據庫中檢索數據Android

public class OrgEventCreationActivity extends Activity { 

private SQLiteAdapter mySQLiteAdapter; 
public EditText eventnames; 
public EditText evedes; 
public EditText numofv; 
public EditText possisions; 
public EditText skills; 
public EditText minage; 
public Button saveevent; 
public Button proceed; 
public Button stime; 
public Button etime; 
public Button date; 
public ListView list; 
public Button submit; 

/** Called when the activity is first created. */ 
@SuppressWarnings("deprecation") 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_org_event_creation); 

    eventnames = (EditText)findViewById(R.id.eventnameinput); 
    evedes = (EditText)findViewById (R.id.eventdescriptioninput); 
    numofv = (EditText)findViewById (R.id.numofv); 
    possisions = (EditText)findViewById (R.id.posinput); 
    skills = (EditText)findViewById (R.id.skillsinput); 
    minage = (EditText)findViewById (R.id.minage); 
    saveevent =(Button)findViewById (R.id.aeventbtn); 
    proceed = (Button)findViewById (R.id.submitbtn); 
    stime = (Button)findViewById (R.id.stimebtn); 
    etime = (Button)findViewById (R.id.etimebtn); 
    date = (Button)findViewById (R.id.datepicker); 
    list = (ListView)findViewById (R.id.orglist); 
    submit = (Button)findViewById (R.id.submitbtn); 
    submit.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Intent us = new Intent  (OrgEventCreationActivity.this,UserHomeActivity.class); 
      startActivity(us); 
     } 
    }); 

    saveevent.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      populatelist(); 
     } 
    }); 

    } 
    public void populatelist(){ 

    String evename = eventnames.getText().toString().trim(); 

    /* 
    * Create/Open a SQLite database 
    * and fill with dummy content 
    * and close it 
    */ 

    mySQLiteAdapter = new SQLiteAdapter(this); 
    mySQLiteAdapter.openToWrite(); 
    mySQLiteAdapter.deleteAll(); 

    mySQLiteAdapter.insert(evename); 


    mySQLiteAdapter.close(); 

    Intent log = new Intent(OrgEventCreationActivity.this, UserHomeActivity.class); 
    startActivity(log); 


} 


    mySQLiteAdapter = new SQLiteAdapter(this); 
    mySQLiteAdapter.openToRead(); 

    Cursor cursor = mySQLiteAdapter.queueAll(); 
    startManagingCursor(cursor); 

    String[] from = new String[]{SQLiteAdapter.MYDATABASE_TABLE}; 
    int[] to = new int[]{R.id.text}; 

    SimpleCursorAdapter cursorAdapter = 
     new SimpleCursorAdapter(this, R.layout.row, cursor, from, to); 

    listContent.setAdapter(cursorAdapter); 

    mySQLiteAdapter.close(); 


} 
} 
} 

活動,以顯示列表視圖

import android.app.Activity; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.widget.ListView; 
import android.widget.SimpleCursorAdapter; 

public class UserHomeActivity extends Activity { 

private SQLiteAdapter mySQLiteAdapter; 

/** Called when the activity is first created. */ 
@SuppressWarnings("deprecation") 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    ListView listContent = (ListView)findViewById(R.id.contentlist); 

    /* 
    * Create/Open a SQLite database 
    * and fill with dummy content 
    * and close it 
    * 
    * mySQLiteAdapter = new SQLiteAdapter(this); 
    *mySQLiteAdapter.openToWrite(); 
    * mySQLiteAdapter.deleteAll(); 

    *mySQLiteAdapter.insert("Charity Dinner"); 
    *mySQLiteAdapter.insert("Fundraiser"); 
    *mySQLiteAdapter.insert("Benifit Concert"); 
    *mySQLiteAdapter.insert("Silent Auction"); 
    * 

    *mySQLiteAdapter.close(); 
    */ 
    /* 
    * Open the same SQLite database 
    * and read all it's content. 
    */ 
    mySQLiteAdapter = new SQLiteAdapter(this); 
    mySQLiteAdapter.openToRead(); 

    Cursor cursor = mySQLiteAdapter.queueAll(); 
    startManagingCursor(cursor); 

    String[] from = new String[]{SQLiteAdapter.MYDATABASE_TABLE}; 
    int[] to = new int[]{R.id.text}; 

    SimpleCursorAdapter cursorAdapter = 
     new SimpleCursorAdapter(this, R.layout.row, cursor, from, to); 

    listContent.setAdapter(cursorAdapter); 

    mySQLiteAdapter.close(); 


} 
} 

,這是我的數據庫

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.database.sqlite.SQLiteDatabase.CursorFactory; 
import android.view.View.OnClickListener; 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.database.sqlite.SQLiteDatabase.CursorFactory; 

public class SQLiteAdapter { 

public static final String MYDATABASE_NAME = "MY_DATABASE"; 
public static final String MYDATABASE_TABLE = "MY_TABLE"; 
public static final int MYDATABASE_VERSION = 1; 
public static final String NAME_COLUMN = "EVENTS"; 
public static final String KEY_ID = "_id"; 

public static final String KEY_CONTENT = "events"; 

//create table MY_DATABASE (ID integer primary key, Content text not null); 
private static final String SCRIPT_CREATE_DATABASE = 
    "create table " + MYDATABASE_TABLE + " (" 
    + KEY_ID + " integer primary key aautoincrement,"+ "evnents text);"; 

private SQLiteHelper sqLiteHelper; 
private SQLiteDatabase sqLiteDatabase; 

private Context context; 

public SQLiteAdapter(Context c){ 
    context = c; 
} 



public SQLiteAdapter openToRead() throws android.database.SQLException { 
    sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION); 
    sqLiteDatabase = sqLiteHelper.getReadableDatabase(); 
    return this;  
} 

public SQLiteAdapter openToWrite() throws android.database.SQLException { 
    sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION); 
    sqLiteDatabase = sqLiteHelper.getWritableDatabase(); 
    return this;  
} 

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

public void insertEntry(String evename) 
{ 
    ContentValues newValues = new ContentValues(); 
    // Assign values for each row. 
    newValues.put("EVETS",evename); 


    // Insert the row into your table 
    sqLiteDatabase.insert("ev", null, newValues); 
    ///Toast.makeText(context, "Reminder Is Successfully Saved", Toast.LENGTH_LONG).show(); 
} 





public int deleteAll(){ 
    return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null); 
} 

public Cursor queueAll(){ 
    String[] columns = new String[]{KEY_ID, KEY_CONTENT}; 
    Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns, 
      NAME_COLUMN, null, null, null, null); 

    return cursor; 
} 

public class SQLiteHelper extends SQLiteOpenHelper { 

    public SQLiteHelper(Context context, String name, 
      CursorFactory factory, int version) { 
     super(context, name, factory, version); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     // TODO Auto-generated method stub 
     db.execSQL(SCRIPT_CREATE_DATABASE); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // TODO Auto-generated method stub 

    } 

} 

} 

我一直在試圖算出這個數天,現在如果你能幫助我會真正偉大的

+0

修改或重新創建的意義上?你能用一個例子來解釋嗎? – Keshav1234 2015-04-01 07:00:58

回答

0

數據庫只是以兩種方式重新創建。

  1. 升級db版本SQLiteAdapter
  2. 從設備或模擬器中刪除數據庫文件。如果它是真實的設備,在設置中清除數據
相關問題