sqlite 无重复随机()查询

sqxo8psd  于 2023-10-23  发布在  SQLite
关注(0)|答案(2)|浏览(136)

我需要显示3从SQLite数据库随机图像水平。由于不可能创建一个水平的ListView,我在一个水平方向的LinearLayout中创建了3个相邻的ListView:

final ListView g = (ListView)findViewById(R.id.lstText1);
final ListView h = (ListView)findViewById(R.id.lstText2);
final ListView i = (ListView)findViewById(R.id.lstText3);

g.setOnItemClickListener(this);
h.setOnItemClickListener(this);
i.setOnItemClickListener(this);
// Set the adapter to our custom adapter (below)

g.setAdapter(new MySimpleCursorAdapter(this, R.layout.toplist,
    managedQuery(Uri.withAppendedPath(Provider.CONTENT_URI,
    Database.Project.NAME), new String[] { BaseColumns._ID,
    Database.Project.C_SMALLIMAGE}, null, null, "RANDOM() LIMIT 1"),
    new String[] { Database.Project.C_SMALLIMAGE }, new int[] {R.id.image1}));

h.setAdapter(new MySimpleCursorAdapter(this, R.layout.toplist,
    managedQuery(Uri.withAppendedPath(Provider.CONTENT_URI,
    Database.Project.NAME), new String[] { BaseColumns._ID,
    Database.Project.C_SMALLIMAGE}, null, null, "RANDOM() LIMIT 1"),
    new String[] { Database.Project.C_SMALLIMAGE }, new int[] {R.id.image1}));

i.setAdapter(new MySimpleCursorAdapter(this, R.layout.toplist,
    managedQuery(Uri.withAppendedPath(Provider.CONTENT_URI,
    Database.Project.NAME), new String[] { BaseColumns._ID,
    Database.Project.C_SMALLIMAGE}, null, null, "RANDOM() LIMIT 1"),
    new String[] { Database.Project.C_SMALLIMAGE }, new int[] {R.id.image1}));

一切正常,除了随机显示的图像应该彼此不同。由于我使用3个不同的列表视图,有时他们显示相同的随机图像。如何解决我的问题?修改"RANDOM() LIMIT 1"

drkbr07n

drkbr07n1#

FSM保存我们.你还在这里
对于单个项目,ListView是一个坏主意
只需将3个ImagesView放在布局中,然后执行以下操作

final ImageView[] images = new ImageView[3];
ImageLoader loader = new ImageLoader(this);

images[0] = (ImageView)findViewById(R.id.imageView1);
images[1] = (ImageView)findViewById(R.id.imageView2);
images[2] = (ImageView)findViewById(R.id.imageView3);

int counter = 0;

    Cursor c = managedQuery(Uri.withAppendedPath(Provider.CONTENT_URI,
                    Database.Project.NAME), new String[] { BaseColumns._ID,
                    Database.Project.C_SMALLIMAGE}, null, null, "RANDOM() LIMIT 3");
  if(c!=null && c.moveToFirst()){
     do{
       String url = c.getString(1);
       images[counter].setTag(url);
       loader.DisplayImage(url, this, images[counter]);
       counter++;
     }while(c.moveToNext());
  }
dl5txlt9

dl5txlt92#

获取行数,在客户端从0到rowcount-1范围内选择随机数,然后使用LIMIT选择第n行。

相关问题