2011-06-27 11 views
0

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(); 
         } 

        } 
       } 
      } 

    } 
+0

DetailsActivityクラスを貼り付けることができますか? – sparkymat

+0

はい、ちょうど今更新したように見えますか。Thx – hectichavana

+0

申し訳ありません。それを理解することはできません。 – sparkymat

答えて

2

それだけのブラインドショットだ - おそらくあなたはいないインターネットに接続しているとき、あなたのonCreate()アプリケーションが最初のリストは、あなたの活動ののonCreateメソッドからのものであることを、私は、仮定(非常に迅速に答える接続があるかどうかを取得します。 一般的に言えば、メインのUIスレッドを使用してデータをダウンロードするのは良い考えではありませんが、この作業を別のスレッドに移行する方がはるかに優れています。 AsyncTaskクラスに精通してください

関連する問題