开发者

Android: unrepeated SQLite Random() Query

I need to display 3 random images from my sqlite databa开发者_如何学运维se horizontally. Since it's impossible to make a horizontal ListView, I manipulated my way by making 3 ListViews next to each other inside a horizontal oriented LinearLayout.

So I'm using this method currently:

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}));

Everything works perfectly in order, except the random shown images should be distinct between one another. With my method above, since I use 3 different ListViews, sometimes they show the same random images.

Can anybody give me a solution to my problem? maybe by modifying this line "RANDOM() LIMIT 1" or else, well I'm open to any kind of solutions anyway.

Thank you very much!


I have recently been working on something similar for an iPhone project also using SQLite.

Take a look at the following two questions I asked, I suspect they answer what you are trying to do.

SQLite select statement optimisation advice

and

SQLite Select data from multiple rows returned as one row


FSM save us ... you're still here

ListView for single item is a bad idea

just put 3 ImagesView in Layout and do something like this

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());
  }


Get the rowcount, pick your random numbers in the range 0 to rowcount-1 at the client, then use LIMIT to choose the nth row...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜