簡體   English   中英

在列表視圖中從SQLite數據庫Android添加和檢索數據

[英]Add and Retrieve data from SQLite Database Android in listview

好的,所以我是新來的,我知道這個問題已經被問過很多次了,但是我似乎什么都沒用,我可以得到一些我發現在Listview中工作正常的示例,但是如果我嘗試修改或重新創建我只是似乎無法使其工作如此即時,以至於我對我的代碼做錯了....對不起,不好的格式。

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

    }

}

}

我已經嘗試了好幾天了,如果您能幫助的話,我將真的很棒

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

  1. SQLiteAdapter中升級數據庫版本。
  2. 從設備或仿真器中刪除數據庫文件。 如果是真實設備,請清除設置中的數據

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM