开发者

Android: onClick StartActivity works only when there is no internet connection

I have a method on my app which displays random images from the SQLite database, which are fetched from the web (JSON). I put an OnClickListener to the image so when it's clicked, it will lead to a new Activity carrying the properties (from JSON saved into an SQLite database) of the random shown image through a Cursor.

The problem I'm facing is, the images are clickable and lead to a new Activity, only when my device isn't connected to the internet.

Can anybody analyze my code below and provide me a solution? Thank you in advance.

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

DetailsActivity class.

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.开发者_如何学Pythontxt_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();
                        }

                    }
                }
            }   

    }


It's just a blind shot - probably when you haven't internet connection your onCreate() your app gets answer very quick (I assume, that first listing is from onCreate method of your activity. If there is connection it waits for timeout (data doesn't load on some reason). Generally speaking, it's not good idea to download data using main UI thread. Much better approach is to shift this work to another thread - in this case you probably need to become familiar with AsyncTask class.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜