【问题标题】:Sqlite inserting data with ListViewSqlite 使用 ListView 插入数据
【发布时间】:2014-06-28 06:39:50
【问题描述】:

我想在 sqlitedb 中将编辑文本值附加到顶部的新文本(索引 0)并向下移动先前插入的数据(从索引 1 开始),同时插入行...在顶部插入新行并显示它们在 ListView 中。我的代码适用于在底部追加行。

帮帮我..

dbhelper.java

public class DBhelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "REGISTRATION_DB";
public static final String TABLE_NAME = "REGISTRATION_TABLE";
public static final int VERSION = 1;
public static final String KEY_ID = "_id";
public static final String NAME = "NAME";
public static final String DB = "create table " + TABLE_NAME + " ("
  + KEY_ID + " integer primary key autoincrement, " + NAME
 + " text not null );";


    public DBhelper(Context context, String name,
            CursorFactory factory, int version) {
        super(context, name, factory, version);
        // TODO Auto-generated constructor stub
    }

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


    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
         db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);

    }

}

数据操作.java

SQLiteDatabase database_ob;
 DBhelper openHelper_ob;
 Context context;


 public Dataoper(Context c) {
    // TODO Auto-generated constructor stub

 context=c;

 }

public Dataoper opnToRead() {
      openHelper_ob = new DBhelper(context,
        openHelper_ob.DATABASE_NAME, null, openHelper_ob.VERSION);
      database_ob = openHelper_ob.getReadableDatabase();
      return this;

     }

public Dataoper opnToWrite() {
      openHelper_ob = new DBhelper(context,
        openHelper_ob.DATABASE_NAME, null, openHelper_ob.VERSION);
      database_ob = openHelper_ob.getWritableDatabase();
      return this;

     }

 public void Close() {
      database_ob.close();
     }

public long insertData(String fname) {
      ContentValues contentValues = new ContentValues();
      contentValues.put(openHelper_ob.NAME, fname);
      opnToWrite();
      long val = database_ob.insert(openHelper_ob.TABLE_NAME, null,
        contentValues);
      Close();
      return val;

     }

 public Cursor readdata() {
      String[] cols = { openHelper_ob.KEY_ID, openHelper_ob.NAME };
      opnToWrite();
      @SuppressWarnings("static-access")
    Cursor c = database_ob.query(openHelper_ob.TABLE_NAME, cols, null,
        null, null, null, null);

      return c;

     }
 public Cursor queryAll(int nameId) {
      String[] cols = { openHelper_ob.KEY_ID, openHelper_ob.NAME};
      opnToWrite();
      Cursor c = database_ob.query(openHelper_ob.TABLE_NAME, cols,
        openHelper_ob.KEY_ID + "=" + nameId, null, null, null, null);

      return c;

     }

Mainactivity.java

public class MainActivity extends Activity {
ListView lv;
Dataoper adapter_ob;
DBhelper helper_ob;
SQLiteDatabase db_ob;
Button bt;
Cursor cursor;

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

        lv=(ListView)findViewById(R.id.list);
        bt=(Button)findViewById(R.id.bt);
        adapter_ob = new Dataoper(this);

          String[] from = { DBhelper.NAME };
          int[] to = { R.id.name };
          cursor = adapter_ob.readdata();
          SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this,
            R.layout.listitem_row, cursor, from, to);
          lv.setAdapter(cursorAdapter);

        bt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
Intent i= new Intent(MainActivity.this, Second.class);

                startActivity(i);
            }
        });
    }


}

【问题讨论】:

    标签: java android sqlite listview cursor


    【解决方案1】:

    您可以先显示通过查询最后插入的值

    从 REGISTRATION_TABLE orderby _id ASEC 中选择 NAME

    在执行这个查询时,你会得到游标值。从游标值中,你需要创建arraylist并将该arraylist传递给Arrayadapter。

    Arraylist<String> al=new ArrayList<String>();
    cursor cursor=db.rawquery("select NAME from REGISTRATION_TABLE orderby _id ASEC",null);
    if(cursor.getcount()!=0)
    { 
     cursor.movetofirst();
    
     do{
      al.add(cursor.getstring(0));
      }
    while(cursor.movetonext());
    }
    

    cursor.close();

    Arrayadapter adapter=new Arrayadapter(this,R.layout.simple_list_item_1,al);
    lv.setadapter(adapter);
    

    【讨论】:

    • 另一种方式 - 光标 c = database_ob.query(dbobj.TABLE_NAME, cols, null,null, null, null, dbobj.KEY_ID + " DESC ");
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多