Android:尝试在变量中保存行的id并创建intent

时间:2017-07-02 03:36:04

标签: java android sqlite android-sqlite

在第一个活动用户创建一个对象(A)之后,对象(A)的Id +名称将保存在SQLite中。当它保存时,用户需要转发到另一个活动,但该活动需要具有之前创建的对象(A)的Id。之后,用户将在(对象A)内部制作一些项目。当我们进行主要活动并看到对象列表(A)按下项目后,用户将看到与选择对象(A)相关的所有对象(B)。

我的代码是

    final String LOG_TAG = "myLogs";

    Thread thread;

    Button btnConfirmItemList;
    EditText etName;

    DBHelper dbHelper;    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.unit_list_item);

        btnConfirmItemList = findViewById(R.id.add_list_btn_confirm);
        btnConfirmItemList.setOnClickListener(this);

        etName = findViewById(R.id.list_item_title);

        dbHelper = new DBHelper(this);
    }




    @Override
    public void onClick(View view) {

        thread = new Thread(this, "Поток для примера");
        Log.i(LOG_TAG, "Создан второй поток " + thread);
        thread.start(); // Запускаем поток


    }


    @Override
    public List<ListModel> addList(int id, String listName) {
        return null;
    }

    @Override
    public void run() {

        try {
            Log.i(LOG_TAG, " ------ Второй поток запущен ------ ");

            ContentValues cv = new ContentValues();    
            String name = etName.getText().toString();    
            SQLiteDatabase db = dbHelper.getWritableDatabase();    
            if(btnConfirmItemList.isEnabled()){
                cv.put("name", name);

                long rowID = db.insert("mytable", null, cv);

                Log.d(LOG_TAG, "row inserted, ID = " + rowID);
            }

            dbHelper.close();

            Thread.sleep(500);
        } catch (InterruptedException e) {
            Log.i(LOG_TAG, " ------ Второй поток прерван ------ ");
        }

    }


    class DBHelper extends SQLiteOpenHelper {

        public DBHelper(Context context) {
            super(context, "myDB", null, 1);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            Log.d(LOG_TAG, "--- onCreate database ---");

            db.execSQL("create table mytable ("
                    + "id integer primary key autoincrement,"
                    + "name text,"
                    + "email text" + ");");
        }

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

        }

        public int getItemIdByPosition() {

            SQLiteDatabase db = this.getReadableDatabase();
            Cursor c = db.query("mytable", null, null, null, null, null, 
 null);
            if (c.moveToLast()) {

                int idColIndex = c.getColumnIndex("id");

                Log.d(LOG_TAG,"ID = " + c.getInt(idColIndex));

                int idIndex = idColIndex;
                return idIndex ;
            } else
                Log.d(LOG_TAG, "Id didnt selected");
            c.close();

            int idIndexx = idIndex;
            return idColIndex;
        }
    }

}

0 个答案:

没有答案