安卓的onClick StartActivity只能在没有互联网连接 [英] Android: onClick StartActivity works only when there is no internet connection

查看:105
本文介绍了安卓的onClick StartActivity只能在没有互联网连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示来自SQLite数据库随机图像,这是从网上(JSON)获取我的应用程序的方法。
我把一个OnClickListener到图像所以它的点击时,它会导致一个新的活动通过游标携带随机显示图像的属性(JSON从保存到​​一个SQLite数据库)。

我现在面临的问题是,图像是可以点击的,并导致新的活动,只有当我的设备没有连接到互联网。

任何人都可以分析我下面的code和提供给我一个解决方案吗?谢谢你在前进。

 的HashMap< ImageView的,朗GT&; AAA =新的HashMap< ImageView的,朗GT&();最终的ImageView [] =影像新ImageView的[3];
        ImageLoader的装载机=新ImageLoader的(本);        图像[0] =(ImageView的)findViewById(R.id.top1);
        图像[1] =(ImageView的)findViewById(R.id.top2);
        图像[2] =(ImageView的)findViewById(R.id.top3);        图像[0] .setOnClickListener(本);
        图像[1] .setOnClickListener(本);
        图像[2] .setOnClickListener(本);        INT计数器= 0;            光标C = managedQuery(Uri.withAppendedPath(Provider.CONTENT_URI,
                            Database.Project.NAME),新的String [] {BaseColumns._ID,
                            Database.Project.C_SMALLIMAGE},NULL,NULL,RANDOM()LIMIT 3);
          如果(C =空&放大器;!&放大器; c.moveToFirst()){
             做{
               字符串URL = c.getString(1);
               图像[窗口] .setTag(URL);
               loader.DisplayImage(URL,这样,图像[计数器]);
               aaa.put(图像[窗口],c.getLong(c.getColumnIndex(BaseColumns._ID)));
               反++;
             }而(c.moveToNext());
          }@覆盖
        公共无效的onClick(视图v)
        {
            意图listIntent =新意图(MainActivity.this,DetailsActivity.class);
            长ID = aaa.get(V);
            listIntent.setData(Uri.withAppendedPath(Uri.withAppendedPath(
                    Provider.CONTENT_URI,Database.Project.NAME),龙
                    的ToString(ID)));
            startActivity(listIntent);
        }

DetailsActivity类。

 公共类DetailsActivity扩展活动实现OnClickListener {    ImageLoader的装载机= NULL;    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.project);
        装载机=新ImageLoader的(本);
            意向意图= getIntent();
            如果(意向!= NULL){
                URI URI = intent.getData();
                如果(URI!= NULL){
                    光标光标= managedQuery(URI,新的String [] {
                            BaseColumns._ID,Database.Project.C_PROJECTTITLE,Database.Project.C_ORGANIZATIONTITLE,
                            Database.Project.C_PROJECTDESCRIPTION,Database.Project.C_SMALLIMAGE},NULL,NULL,NULL);                    如果(光标== NULL){
                        完();
                    }其他{
                        如果(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(冯+ cursor.getString(2)));                               TextView的project_description =(的TextView)findViewById(R.id.txt_project_description);
                               project_description.setText(Html.fromHtml(cursor.getString(3)));
                                串图片网址= cursor.getString(4);
                                img.setTag(图片网址);
                                loader.DisplayImage(图片网址,为此,IMG);
                        }其他{
                            完();
                        }                    }
                }
            }    }


解决方案

这只是一个瞎炮 - 也许,当你没有互联网连接你的onCreate()您的应用程序得到答案非常快(我假定,最早上市的是您的活动onCreate方法,如果有连接在等待超时(数据不会对某些原因负载)。
一般来说,这不是下载使用主UI线程数据好主意。更好的方法是对这项工作转移到另一个线程 - 在这种情况下,你可能需要熟悉的AsyncTask

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

                    }
                }
            }   

    }

解决方案

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.

这篇关于安卓的onClick StartActivity只能在没有互联网连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆