使用Sqlite Table为每个活动动态创建活动?

时间:2018-06-27 03:15:11

标签: android sqlite

我正在设计一个应用程序,它将允许用户创建任意数量的活动,并为每个活动创建sqlite表来存储数据。每个活动将包含与其他活动相同的设计/ UI,但会将不同客户端的数据存储在其相关的sqlite表中。

我该如何实现?

1 个答案:

答案 0 :(得分:0)

活动是UI,因此如果它们相同,则无需创建多个活动,所有您需要做的就是将不同的数据(可能只是一个或两个值)传递给一个活动。

同样,几乎没有必要每个用户都有一个表,而只需在单个表中有一个指示客户的列即可。请注意,每个表至少增加了1k(从SQLite 3.12.0开始默认为4k)。如果有1000行中的10行,您可能只需要拆分数据以提高速度。

当然,这仅是提取适当数据的问题。

作为一个例子。这是一个两个表(活动和值),允许按活动保存值(请注意活动可能不是最佳术语)。

DBHelper.java

这包括用于构建数据库及其中的表的结构/架构。它还具有一些基本方法,可以添加活动,活动值以及在每个活动的基础上从存储的值中检索活动列表和值。

public class DBHelper extends SQLiteOpenHelper {

    public static final String DBNAME = "activity.db";

    public static final int DBVERSION = 1;
    public static final String TBNAME_ACTIVITY = "_activity";
    public static final String COL_ACTIVITY_ID = BaseColumns._ID;
    public static final String COL_ACTIVITY_NAME = "_activity_name";
    public static final String COL_ACTIVITY_USERID = "_activity_userid";
    public static final String UNQCNSTRNT_ACTIVITY_USERID_NAME = "_activity_userid_name_unique";

    public static final String TBNAME_VALUE_STORE = "_value_store";
    public static final String COL_VALUE_STORE_ID = BaseColumns._ID;
    public static final String COL_VALUE_STORE_ACTIVITY = "_value_store_activity";
    public static final String COL_VALUE_STORE_NAME = "_value_store_name";
    public static final String COL_VALUE_STORE_TYPE = "_value_store_type";
    public static final String COL_VALUE_STORE_ORDER = "_value_store_order";
    public static final String COL_VALUE_STORE_VALUE = "_value_store_value";
    public static final String UNQCNSTRNT_VALUESTORE_ACTIVITY_NAME_ORDER = "_value_store_activity_name_order";

    public static final int VALUETYPE_INTEGER = 1;
    public static final int VALUETYPE_STRING = 2;

    private static final String CRTSQL_START = "CREATE TABLE IF NOT EXISTS ";
    private static final String CRTSQL_IDCOL_TYPE = " INTEGER PRIMARY KEY, ";

    SQLiteDatabase mDB;

    public DBHelper(Context context) {
        super(context, DBNAME, null, DBVERSION);
        mDB = this.getWritableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String crt_activity_table_sql = CRTSQL_START +
                TBNAME_ACTIVITY + "(" +
                COL_ACTIVITY_ID + CRTSQL_IDCOL_TYPE +
                COL_ACTIVITY_NAME + " TEXT, " +
                COL_ACTIVITY_USERID + " TEXT " +
                ", CONSTRAINT " +
                UNQCNSTRNT_ACTIVITY_USERID_NAME +

                " UNIQUE (" +
                COL_ACTIVITY_USERID +
                "," +
                COL_ACTIVITY_NAME +
                "))";
        String crt_value_store_table_sql = CRTSQL_START +
                TBNAME_VALUE_STORE + "(" +
                COL_VALUE_STORE_ID + CRTSQL_IDCOL_TYPE +
                COL_VALUE_STORE_ACTIVITY + " INTEGER, " +
                COL_VALUE_STORE_NAME + " TEXT," +
                COL_VALUE_STORE_TYPE + " INTEGER, " +
                COL_VALUE_STORE_ORDER + " INTEGER, " +
                COL_VALUE_STORE_VALUE + " unspecified " +
                ", CONSTRAINT " +
                UNQCNSTRNT_VALUESTORE_ACTIVITY_NAME_ORDER +
                " UNIQUE (" +
                COL_VALUE_STORE_ACTIVITY +
                "," +
                COL_VALUE_STORE_NAME +
                "," +
                COL_VALUE_STORE_ORDER +
                "))";
        db.execSQL(crt_activity_table_sql);
        db.execSQL(crt_value_store_table_sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
    }

    public Cursor getActivities() {
        return mDB.query(TBNAME_ACTIVITY,null,null,null,null,null,null);
    }

    public  String getValueString(String value_name, long activity_id) {
        String rv = "no value?? for activity " + String.valueOf(activity_id) + " value_name of " + value_name;
        Cursor csr = mDB.query(TBNAME_VALUE_STORE,
                new String[]{COL_VALUE_STORE_VALUE},
                COL_VALUE_STORE_ACTIVITY + "=? AND " +
                        COL_VALUE_STORE_NAME + "=?",
                new String[]{String.valueOf(activity_id),value_name},
                null,null,COL_VALUE_STORE_ORDER
        );
        if (csr.moveToFirst()) {
            rv = csr.getString(csr.getColumnIndex(COL_VALUE_STORE_VALUE));
        }
        csr.close();
        return rv;
    }

    public long addActivity(String activity_name, long userid ) {
        ContentValues cv = new ContentValues();
        cv.put(COL_ACTIVITY_NAME,activity_name);
        cv.put(COL_ACTIVITY_USERID,userid);
        return mDB.insert(TBNAME_ACTIVITY,null,cv);
    }

    public long addStringValueStore(String name, long order, String value, long owning_activity ) {
        ContentValues cv = new ContentValues();
        cv.put(COL_VALUE_STORE_VALUE,value);
        return addValueStore(name,order,cv,owning_activity, VALUETYPE_STRING);
    }

    public long addIntegerValueStore(String name, long order, long value, long owning_activity) {
        ContentValues cv = new ContentValues();
        cv.put(COL_VALUE_STORE_VALUE,value);
        return addValueStore(name,order,cv,owning_activity, VALUETYPE_INTEGER);

    }

    private long addValueStore(String name, long order, ContentValues cv, long owningactivity, int type) {
        cv.put(COL_VALUE_STORE_NAME,name);
        cv.put(COL_VALUE_STORE_ORDER,order);
        cv.put(COL_VALUE_STORE_ACTIVITY,owningactivity);
        cv.put(COL_VALUE_STORE_TYPE,type);
        return  mDB.insert(TBNAME_VALUE_STORE,null,cv);
    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {

    public static final String INTENTKEY_ACTIVITYID = "iextra_actviityid";

    DBHelper mDBHlpr;
    Cursor mCsr;
    SimpleCursorAdapter mAdapter;
    ListView mActivityList;
    Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = this;
        mActivityList = this.findViewById(R.id.activity_list);
        mDBHlpr = new DBHelper(this);
        bldDemoDBActivities();
        mCsr = mDBHlpr.getActivities();
        mAdapter = new SimpleCursorAdapter(this,
                android.R.layout.simple_list_item_1,
                mCsr,
                new String[]{DBHelper.COL_ACTIVITY_NAME},
                new int[]{android.R.id.text1},
                0
        );
        mActivityList.setAdapter(mAdapter);
        mActivityList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(mContext,BaseActivity.class);
                intent.putExtra(INTENTKEY_ACTIVITYID,l);
                startActivity(intent);
            }
        });

    }

    private void bldDemoDBActivities() {
        long current_activity;
        current_activity = mDBHlpr.addActivity("Activity 1",0);
        mDBHlpr.addStringValueStore("mystring",0,"Rumplestiltskin",current_activity);
        if (current_activity > 0) {
            mDBHlpr.addIntegerValueStore("A number", 0, 42, current_activity);
            mDBHlpr.addIntegerValueStore("intarray", 0, 33, current_activity);
            mDBHlpr.addIntegerValueStore("intarray", 1, 44, current_activity);
            mDBHlpr.addIntegerValueStore("intarray", 2, 55, current_activity);
        }

        current_activity = mDBHlpr.addActivity("Activity 2",0);
        if (current_activity > 0) {
            mDBHlpr.addStringValueStore("mystring", 0, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", current_activity);
        }
    }
}

BaseActivity.java

这是具有不同数据传递的调用活动(拥有/发起活动的id仅1个值,这足以从值存储表中获取值):-

public class BaseActivity extends AppCompatActivity {

    DBHelper mDBHlpr;
    TextView mMyString;
    long mActivityId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);
        mMyString = this.findViewById(R.id.mystring);
        mActivityId = this.getIntent().getLongExtra(MainActivity.INTENTKEY_ACTIVITYID,-1);
        mDBHlpr = new DBHelper(this);
        if (mActivityId > 0) {
            mMyString.setText(mDBHlpr.getValueString("mystring", mActivityId));
        } else {
            mMyString.setText("Oooops no valid activity passed.");
        }
    }
}

结果应用程序:-

初始

enter image description here

点击活动1

enter image description here

点击活动2

enter image description here