开发者

Issues implementing SQLite and Cursor in an Activity

I have an activity which shows 3 random images from JSON data off the Web. Each random image is clickable and leads to its details activity, carrying its title, image, description, etc. The shown images are distinct from each other.

I need to implement SQLite and a Cursor to that activity so that whenever there's no internet connection, the application reads the image data from the cache on the external storage. I already have the classes of the database, provider and sync. I have an SQLite database with the following columns:

  • BaseColumns._ID
  • Database.Project.C_SMALLIMAGE

Here are the snippets of code that I have so far:

void doSync() {
    Intent serviceIntent = new Intent(this, LooserSync.class);
    startService(serviceIntent);
} 

This method calls the Web service where I put my URL. This is the image is loaded:

public void setViewImage(ImageView v, String value) {
    v.setTag(value);
    loader.DisplayImage(value, context, v);
}

This is my old function that randomizes the images and makes them clickable:

prjcts = new ArrayList<Project>();
int max = prjcts.size();
System.out.println(max);
List<Integer> indices = new ArrayList<Integer>(max);
for(int c = 1; c < max; ++c) {
    indices.add(c);
}
Random r = new Random();
int arrIndex = r.nextInt(indices.size());
int randomIndex1 = indices.get(arrIndex);
indices.remove(arrIndex);

This is what I have in my activity so far:

int arrIndex2 = r.nextInt(indices.size());
int randomIndex2 = indices.get(arrIndex2);
indices.remove(arrIndex2);

int arrIndex3 = r.nextInt(indices.size());
int randomIndex3 = indices.get(arrIndex3);
indices.remove(arrIndex3);

imageLazy(image1, prjcts.get(randomIndex1),Main.this);
imageLazy(image2, prjcts.get(randomIndex2),Main.this);
imageLazy(image3, prjcts.get(randomIndex3),Main.this);

image1.setOnClickListener(new RandomClickListener(randomIndex1));
image2.setOnClickListener(new RandomClickListener(randomIndex2));
image3.setOnClickListener(new RandomClickListener(randomIndex3));

public void imageLazy(final ImageView image,Project pro,Activity activity) {
     String imageurl = pro.smallImageUrl;
     image.setTag(imageurl);
     loader.DisplayImage(imageurl, activity,image);
}

public class RandomClickListener implements View.OnClickListener {
     private final int randomIndex;

     public RandomClickListener(final int randomIndex) {
           this开发者_开发技巧.randomIndex = randomIndex;
     }
     @Override
     public void onClick(View v) {
          Intent top = new Intent(Main.this, DetailsActivity.class);
          top.putExtra("spendino.de.ProjectDetail.position", randomIndex);
          /*
          top.setData(Uri.withAppendedPath(Uri.withAppendedPath(
          LooserProvider.CONTENT_URI, Database.Project.NAME), Long.toString(id)));
           */
          startActivity(top);
     }
}

Here is a function that retrieves the image details:

Uri uri = Uri.withAppendedPath(Uri.withAppendedPath( LooserProvider.CONTENT_URI, Database.Project.NAME), Long .toString(id));

Cursor managedCursor = managedQuery( uri, new String[] { BaseColumns._ID, Database.Project.C_BIGIMAGE }, null, null, "RANDOM() LIMIT 3");

Any suggestions on how to build the code from these snippets?


for FSM's sake use ListView with query like this

Cursor managedCursor = managedQuery( //it seems like you should change your internship
                uri, //to somthing esier than programming
                projection,    // Which columns to return.
                null,          // WHERE clause.
                null,          // WHERE clause value substitution
                "RANDOM() LIMIT 3");   

with this query u get top 3 rows from Project table in random order

EDIT:

  1. use ListView
  2. wrong uri ... you didn't made your homework (by reading this). Try this one. Uri uri = Uri.withAppendedPath( LooserProvider.CONTENT_URI, Database.Project.NAME));
  3. About using Cursors:

    Cursor c = query();

    if(c.moveToFirst()){

    do{
    
        //do something with current row like c.getString(1);
    
    }while(c.moveToNext());
    

    }

  4. but why oh why you don't using ListView

EDIT2: chang similar lines in my sample for looser to:

    listView.setAdapter(new MySimpleCursorAdapter(this, R.layout.itemrow,
            managedQuery(Uri.withAppendedPath(LooserProvider.CONTENT_URI,
                    Database.Project.NAME), new String[] { BaseColumns._ID,
                    Database.Project.C_PROJECTTITLE,
                    Database.Project.C_ORGANIZATIONTITLE,
                    Database.Project.C_SMALLIMAGE }, null, null, "RANDOM() LIMIT 3"),
            new String[] { Database.Project.C_PROJECTTITLE,
                    Database.Project.C_ORGANIZATIONTITLE,
                    Database.Project.C_SMALLIMAGE }, new int[] {
                    R.id.tName, R.id.tDescription, R.id.iItem }));

you can do Menu->Synchronize to see what happend

get rid of text from listrow and you got your program


I think you would have to ask a question that is relatively simple to answer. Most people won't want to produce code for you.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜