Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在一个字段中随机选择一条记录_Android_Sqlite_Android Sqlite - Fatal编程技术网

Android 如何在一个字段中随机选择一条记录

Android 如何在一个字段中随机选择一条记录,android,sqlite,android-sqlite,Android,Sqlite,Android Sqlite,我创建了如下SQLite数据库表: _id || Column1 || Column2 || 1 || test1 || a,b,c,d,e,f || 2 || test2 || g,h,i,j,k,l || 3 || test3 || m,n,o,p,q,r || _id || Column1 || Column2 || 1 || test1 || d

我创建了如下SQLite数据库表:

_id  ||   Column1   ||    Column2    ||
1    ||    test1    ||   a,b,c,d,e,f ||
2    ||    test2    ||   g,h,i,j,k,l ||
3    ||    test3    ||   m,n,o,p,q,r ||
_id  ||   Column1   ||    Column2    ||
1    ||    test1    ||   d           ||
2    ||    test2    ||   k           ||
3    ||    test3    ||   r           ||
  @Override
  public View newView(Context context, Cursor cursor, ViewGroup parent) {
      Cursor c = getCursor();
      final LayoutInflater inflater = LayoutInflater.from(context);
      View v = inflater.inflate(layout, parent, false);
      int column2Name= c.getColumnIndex(DBAdapter.COLUMN2);
      String col2Name= c.getString(column2Name);
      String[] temp;
      Random random = new Random();
          temp = col2Name.split(",");
          String col2 = temp[random.nextInt(temp.length)];
      TextView col2_name = (TextView) v.findViewById(R.id.column2_label);
      if (col2_name != null) {
          col2_name .setText(col2);
      }
      return v;
  }

  @Override
  public void bindView(View v, Context context, Cursor c) {
      int column2Name= c.getColumnIndex(DBAdapter.COLUMN2);
          String col2Name= c.getString(column2Name);
          String[] temp;
          Random random = new Random();
              temp = col2Name.split(",");
              String col2 = temp[random.nextInt(temp.length)];
          TextView col2_name = (TextView) v.findViewById(R.id.column2_label);
      if (col2_name != null) {
          col2_name .setText(col2);
      }
    }
如何从该表中随机选择第2列中的一个

我想要这样的结果:

_id  ||   Column1   ||    Column2    ||
1    ||    test1    ||   a,b,c,d,e,f ||
2    ||    test2    ||   g,h,i,j,k,l ||
3    ||    test3    ||   m,n,o,p,q,r ||
_id  ||   Column1   ||    Column2    ||
1    ||    test1    ||   d           ||
2    ||    test2    ||   k           ||
3    ||    test3    ||   r           ||
  @Override
  public View newView(Context context, Cursor cursor, ViewGroup parent) {
      Cursor c = getCursor();
      final LayoutInflater inflater = LayoutInflater.from(context);
      View v = inflater.inflate(layout, parent, false);
      int column2Name= c.getColumnIndex(DBAdapter.COLUMN2);
      String col2Name= c.getString(column2Name);
      String[] temp;
      Random random = new Random();
          temp = col2Name.split(",");
          String col2 = temp[random.nextInt(temp.length)];
      TextView col2_name = (TextView) v.findViewById(R.id.column2_label);
      if (col2_name != null) {
          col2_name .setText(col2);
      }
      return v;
  }

  @Override
  public void bindView(View v, Context context, Cursor c) {
      int column2Name= c.getColumnIndex(DBAdapter.COLUMN2);
          String col2Name= c.getString(column2Name);
          String[] temp;
          Random random = new Random();
              temp = col2Name.split(",");
              String col2 = temp[random.nextInt(temp.length)];
          TextView col2_name = (TextView) v.findViewById(R.id.column2_label);
      if (col2_name != null) {
          col2_name .setText(col2);
      }
    }
假设d,k,r是每个记录第2列的随机值

谢谢

更新: 我创建了CustomAdapter并添加了一些视图,如下所示:

_id  ||   Column1   ||    Column2    ||
1    ||    test1    ||   a,b,c,d,e,f ||
2    ||    test2    ||   g,h,i,j,k,l ||
3    ||    test3    ||   m,n,o,p,q,r ||
_id  ||   Column1   ||    Column2    ||
1    ||    test1    ||   d           ||
2    ||    test2    ||   k           ||
3    ||    test3    ||   r           ||
  @Override
  public View newView(Context context, Cursor cursor, ViewGroup parent) {
      Cursor c = getCursor();
      final LayoutInflater inflater = LayoutInflater.from(context);
      View v = inflater.inflate(layout, parent, false);
      int column2Name= c.getColumnIndex(DBAdapter.COLUMN2);
      String col2Name= c.getString(column2Name);
      String[] temp;
      Random random = new Random();
          temp = col2Name.split(",");
          String col2 = temp[random.nextInt(temp.length)];
      TextView col2_name = (TextView) v.findViewById(R.id.column2_label);
      if (col2_name != null) {
          col2_name .setText(col2);
      }
      return v;
  }

  @Override
  public void bindView(View v, Context context, Cursor c) {
      int column2Name= c.getColumnIndex(DBAdapter.COLUMN2);
          String col2Name= c.getString(column2Name);
          String[] temp;
          Random random = new Random();
              temp = col2Name.split(",");
              String col2 = temp[random.nextInt(temp.length)];
          TextView col2_name = (TextView) v.findViewById(R.id.column2_label);
      if (col2_name != null) {
          col2_name .setText(col2);
      }
    }

现在,只需在活动中使用该customAdapter,就可以将column2显示为1个随机值。

一旦从数据库中获得了数据,在Java中就更容易做到这一点

Random RANDOM = new Random(System.currentTimeMillis());

int index = cursor.getColumnIndex("Column2");
String value = cursor.getString(index);
String[] values = value.split(",");
String randomValue = values[RANDOM.nextInt(values.length)];

尝试
按random()限制1从表顺序中选择列不,我的意思是,不是所有的列都随机显示,只是列2YUP yup上的记录,或者我不想所有的行都随机选择,只是列2的值,随机选择1,就像我说的
按随机顺序从表中选择列2()限制1看第一个示例:))