0

I need to display 3 random images from SQLite database horizontally. Since it's impossible to make a horizontal ListView, I made 3 ListViews next to each other inside a horizontally oriented LinearLayout:

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, except the random shown images should be distinct from each other. Since I use 3 different ListViews, sometimes they show the same random images. How can I solve my problem? Maybe by modifying "RANDOM() LIMIT 1"?

user4157124
  • 2,809
  • 13
  • 27
  • 42
hectichavana
  • 1,436
  • 13
  • 41
  • 71

2 Answers2

1

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());
  }
Selvin
  • 6,598
  • 3
  • 37
  • 43
  • you told me once I better use the listview -__- anyway thanks, why are you always there following me? hehe. I wanna make the OnClickListener how should I declare the long id, OnClick only allows me View as its argument – hectichavana Jun 10 '11 at 12:46
  • how about the onClickListener? How to make it possible for me to call the long id? : public void onClick(View v,long id) { Intent listIntent = new Intent(this, DetailsActivity.class); listIntent.setData(Uri.withAppendedPath(Uri.withAppendedPath( Provider.CONTENT_URI, Database.Project.NAME), Long .toString(id))); startActivity(listIntent); } – hectichavana Jun 10 '11 at 13:32
  • ... now u need to change ImageLoader class and not use stringUrl as a tag for ImageView but `class MyTag { public String url; public Object TagInTag; }` in loop you should store value of c.getInt(c.getColumnIndex(BaseColumns._ID)) in MyTag.TagInTag and add this (MyTag) as a Tag to imageView ... you should also add onClickListener to imageView in Listener you should take back TagInTag(where you store _id of Project) and open Details with this _id ... and, no i'll not wirte code for you anymore ;P – Selvin Jun 10 '11 at 13:33
  • 1
    or ... Build static HashMap aaa; and in loop add this code aaa.put(images[counter], c.getLong(c.getColumnIndex(BaseColumns._ID))); then you can use this hash to obtain _id in onClickListener – Selvin Jun 10 '11 at 13:36
  • 1
    onClickListener has only 1 param `View v` so you can use my first advice and get id like this `MyTag tag = (MyTag)v.getTag(); long id = (long)tag.TagInTag;` or second advice and do this like `long id = aaa.get(v);` – Selvin Jun 10 '11 at 13:39
  • Looks like there's one problem, so I chose your 2nd advice it works properly! Like the 3 random images are different between each other, and clickable to the DetailsActivity..BUT, everytime I open the ListView, then I hit back, the random images aren't clickable. Do you know where's the problem? – hectichavana Jun 10 '11 at 15:12
  • `HashMap aaa = new HashMap();` ---> I put this as Global Variable. Here's the loop---> `if(c!=null && c.moveToFirst()){ do{ String url = c.getString(1); images[counter].setTag(url); loader.DisplayImage(url, this, images[counter]); aaa.put(images[counter], c.getLong(c.getColumnIndex(BaseColumns._ID))); counter++; }while(c.moveToNext()); }` – hectichavana Jun 10 '11 at 15:14
  • and here's the onClick method: ` @Override public void onClick(View v) { Intent listIntent = new Intent(MainActivity.this, DetailsActivity.class); long id = aaa.get(v); listIntent.setData(Uri.withAppendedPath(Uri.withAppendedPath( Provider.CONTENT_URI, Database.Project.NAME), Long .toString(id))); startActivity(listIntent); }` – hectichavana Jun 10 '11 at 15:15
0

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...

Emyr
  • 2,351
  • 18
  • 38