我有一个活动显示来自网络上的 JSON 数据的 3 个随机图像。每个随机图像都是可点击的,并导致其详细信息活动,带有其标题、图像、描述等。显示的图像彼此不同。
我需要实现 SQLite 和Cursor
该活动,以便每当没有互联网连接时,应用程序从外部存储上的缓存中读取图像数据。我已经有了数据库、提供者和同步的类。我有一个包含以下列的 SQLite 数据库:
BaseColumns._ID
Database.Project.C_SMALLIMAGE
以下是我到目前为止的代码片段:
void doSync() {
Intent serviceIntent = new Intent(this, LooserSync.class);
startService(serviceIntent);
}
此方法调用我放置 URL 的 Web 服务。这是加载的图像:
public void setViewImage(ImageView v, String value) {
v.setTag(value);
loader.DisplayImage(value, context, v);
}
这是我的旧功能,可随机化图像并使其可点击:
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);
到目前为止,这是我的活动:
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);
}
}
这是一个检索图像详细信息的函数:
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");
关于如何从这些片段构建代码的任何建议?