Webから取得されたランダムな画像(JSON)を表示するアプリケーションがあります。 画像にOnClickListenerを置くので、クリックしたときにCursorを使用してランダムに表示された画像のプロパティ(SQLiteデータベースに保存されたJSONから)を保持する新しいアクティビティにつながります。Android:onClick StartActivityは、インターネットに接続していない場合にのみ動作します。
私が直面している問題は、自分のデバイスがインターネットに接続されていない場合にのみ、画像がクリック可能で新しいアクティビティにつながることです。
誰でも私のコードを分析して解決策を提供できますか?前もって感謝します。
HashMap<ImageView, Long> aaa = new HashMap<ImageView, Long>();
final ImageView[] images = new ImageView[3];
ImageLoader loader = new ImageLoader(this);
images[0] = (ImageView)findViewById(R.id.top1);
images[1] = (ImageView)findViewById(R.id.top2);
images[2] = (ImageView)findViewById(R.id.top3);
images[0].setOnClickListener(this);
images[1].setOnClickListener(this);
images[2].setOnClickListener(this);
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]);
aaa.put(images[counter], c.getLong(c.getColumnIndex(BaseColumns._ID)));
counter++;
}while(c.moveToNext());
}
@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);
}
詳細アクティビティクラス。
public class DetailsActivity extends Activity implements OnClickListener {
ImageLoader loader = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.project);
loader = new ImageLoader(this);
Intent intent = getIntent();
if (intent != null) {
Uri uri = intent.getData();
if (uri != null) {
Cursor cursor = managedQuery(uri, new String[] {
BaseColumns._ID, Database.Project.C_PROJECTTITLE, Database.Project.C_ORGANIZATIONTITLE,
Database.Project.C_PROJECTDESCRIPTION,Database.Project.C_SMALLIMAGE}, null, null, null);
if (cursor == null) {
finish();
} else {
if (cursor.moveToFirst()) {
ImageView img = (ImageView) findViewById(R.id.project_image);
TextView project_title = (TextView)findViewById(R.id.txt_project_title);
project_title.setText(cursor.getString(1));
TextView organization_title = (TextView)findViewById(R.id.txt_organization_title);
organization_title.setText(Html.fromHtml("von " +cursor.getString(2)));
TextView project_description = (TextView)findViewById(R.id.txt_project_description);
project_description.setText(Html.fromHtml(cursor.getString(3)));
String imageUrl = cursor.getString(4);
img.setTag(imageUrl);
loader.DisplayImage(imageUrl, this, img);
} else {
finish();
}
}
}
}
}
DetailsActivityクラスを貼り付けることができますか? – sparkymat
はい、ちょうど今更新したように見えますか。Thx – hectichavana
申し訳ありません。それを理解することはできません。 – sparkymat