每秒SQLite更新listview值

时间:2015-09-18 19:20:11

标签: android sqlite listview

我正在创建一个示例应用程序,我希望每秒更改一次TextView值。它应该改变listview中每个项目的int。我只是在代码中设置它应该如何改变。我该怎么做呢?我在互联网上找到的任何东西都没有帮助我。 数据库代码

private static final String DB_NAME = "mydb";
private static final int DB_VERSION = 5;
private static final String DB_TABLE = "mytab";

public static final String COLUMN_ID = "_id";
public static final String COLUMN_IMG = "img";
public static final String COLUMN_TXT = "txt";
public static final String COLUMN_NMB = "nmb";

private static final String DB_CREATE =
        "create table " + DB_TABLE + "(" +
                COLUMN_ID + " integer primary key autoincrement, " +
                COLUMN_IMG + " integer, " +
                COLUMN_TXT + " text, " +
                COLUMN_NMB + " number" +
                ");";

private final Context mCtx;


private DBHelper mDBHelper;
private SQLiteDatabase mDB1;

public DB2(Context ctx) {
    mCtx = ctx;
}

public void open() {
    mDBHelper = new DBHelper(mCtx, DB_NAME, null, DB_VERSION);
    mDB1 = mDBHelper.getWritableDatabase();
}

public void close() {
    if (mDBHelper!=null) mDBHelper.close();
}



public Cursor getAllData() {
    String[] columns = {"_id","txt","img","nmb"};
    return mDB1.query(DB_TABLE, columns, null, null, null, null, null);
}

public void addRec(String txt, int img, int nmb) {
    ContentValues cv = new ContentValues();
    cv.put(COLUMN_TXT, txt);
    cv.put(COLUMN_IMG, img);
    cv.put(COLUMN_NMB, nmb);
    mDB1.insert(DB_TABLE, null, cv);
}

public void delRec(long id) {
    mDB1.delete(DB_TABLE, COLUMN_ID + " = " + id, null);
}

private class DBHelper extends SQLiteOpenHelper {

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

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DB_CREATE);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }
}

活动代码

private static final int CM_DELETE_ID = 1;
int nmb1;
ListView lvData;
DB2 db;
SimpleCursorAdapter scAdapter;
Cursor cursor;
EditText et,nmb;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test4);
    et =(EditText)findViewById(R.id.editText2);
    nmb = (EditText)findViewById(R.id.nmb);
    db = new DB2(this);
    db.open();

    cursor = db.getAllData();
    startManagingCursor(cursor);

    String[] from = new String[] { DB2.COLUMN_IMG, DB2.COLUMN_TXT, DB2.COLUMN_NMB };
    int[] to = new int[] { R.id.ivImg, R.id.tvText, R.id.tvNmb };

    scAdapter = new SimpleCursorAdapter(this, R.layout.item, cursor, from, to);
    lvData = (ListView) findViewById(R.id.lvData);
    lvData.setAdapter(scAdapter);

    registerForContextMenu(lvData);
}


public void onButtonClick(View view) {
    nmb1 = Integer.parseInt(nmb.getText().toString());
    db.addRec(et.getText().toString(), R.mipmap.ic_launcher, nmb1);
    cursor.requery();
}

public void onCreateContextMenu(ContextMenu menu, View v,
                                ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.add(0, CM_DELETE_ID, 0, R.string.delete_record);
}

public boolean onContextItemSelected(MenuItem item) {
    if (item.getItemId() == CM_DELETE_ID) {
        AdapterView.AdapterContextMenuInfo acmi = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        db.delRec(acmi.id);
        cursor.requery();
        return true;
    }
    return super.onContextItemSelected(item);
}

protected void onDestroy() {
    super.onDestroy();
    db.close();
}

nmb1是应该每秒更改的值。

1 个答案:

答案 0 :(得分:0)

你可以启动一个可运行的线程来做到这一点。 这是一个每1秒执行一次的Handler示例。

Handler mUiHandler = new Handler();
mUiHandler.postDelayed(new Runnable() {
    @Override
    public void run() {
        // Add your code to change the EditText here
    }
}, 1000);