我有一项活动,显示来自网络的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");
有关如何从这些代码段构建代码的任何建议吗?
答案 0 :(得分:2)
使用ListView和这样的查询
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");
使用此查询,您可以按随机顺序从Project表中获取前3行
编辑:
Uri uri = Uri.withAppendedPath( LooserProvider.CONTENT_URI, Database.Project.NAME));
关于使用游标:
光标c = query();
如果(c.moveToFirst()){
do{
//do something with current row like c.getString(1);
}while(c.moveToNext());
}
EDIT2: 将my sample for looser中的类似行转换为:
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 }));
你可以进行菜单 - >同步以查看发生的事情
摆脱listrow的文字,你得到了你的程序
答案 1 :(得分:1)
我认为您必须提出一个相对简单的问题。 大多数人都不想为你制作代码。