在listview中添加和检索来自SQLite Database Android的数据

时间:2015-04-01 06:48:05

标签: android database sqlite 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();


}
}
}

显示listview的活动

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 个答案:

答案 0 :(得分:0)

数据库只是以两种方式重新创建。

  1. SQLiteAdapter 中升级db版本。
  2. 从设备或模拟器中删除db文件。如果是真实设备,则设置中的清除数据