0

我刚刚在 Fedor 的 LazyLoading ListView 中实现了 GSON。这意味着应用程序通过 ImageLoader 类将下载的图像和文本从 Web 保存到外部存储中。

我想知道为什么如何在没有互联网连接的情况下访问列表视图?应用程序已经成功将缓存数据保存到 SD 卡中,现在如何调用这些文件?因此,当没有互联网时,它会从 SD 卡中调用数据(文本和图像)。

谢谢。我愿意接受任何类型的解决方案

这是我的 ListView 类:

public class ProjectsList extends Activity {
        ListView lstTest;

        ProjectAdapter arrayAdapter;

        ArrayList<Project> prjcts=null;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.projects_list);

            //Initialize ListView
            lstTest= (ListView)findViewById(R.id.lstText);

            prjcts = new ArrayList<Project>();

            arrayAdapter = new ProjectAdapter(ProjectsList.this, R.layout.listitems,prjcts,ProjectsList.this);

                lstTest.setAdapter(arrayAdapter);
                if (isOnline())
                {
            WebService webService = new WebService("http://liebenwald.spendino.net/admanager/dev/android/projects.json");

            Map<String, String> params = new HashMap<String, String>();
            params.put("var", "");

            String response = webService.webGet("", params);

            try
            {
                Type collectionType = new TypeToken<ArrayList<Project>>(){}.getType();
                List<Project> lst= new Gson().fromJson(response, collectionType);


                for(Project l : lst)
                {
                    prjcts.add(l);
                    ConstantData.projectsList.add(l);
                }

                arrayAdapter.notifyDataSetChanged();
            }
            catch(Exception e)
            {
                Log.d("Error: ", e.getMessage());
            }
           }


            lstTest.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {              
                    Intent care = new Intent(ProjectsList.this, ProjectDetail.class);
                    care.putExtra("spendino.de.ProjectDetail.position",position);
                    startActivity(care);
                }
            });

        }
        @Override
        public void onDestroy()
        {
            yAdapter.imageLoader.stopThread();
            lstTest.setAdapter(null);
            super.onDestroy();
        }

        protected boolean isOnline() {
            ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo netInfo = cm.getActiveNetworkInfo();
            if (netInfo != null && netInfo.isConnected()) {
                return true;
            } else {
                     }
                 });
                return false;
            }
        }


    }
4

1 回答 1

1

将 ImageLoader 类中的 getBitmap() 方法更改为以下方法

private Bitmap getBitmap(String urlString)
{
    String filename = String.valueOf(urlString.substring(urlString.lastIndexOf("/") + 1));
    File f = new File(cacheDir, filename);
    // from web
    try
    {
        if(!f.exists())
        {
            Bitmap bitmap = null;
            InputStream is = new URL(urlString).openStream();
            OutputStream os = new FileOutputStream(f);
            Globals.CopyStream(is, os);
            os.close();
            bitmap = decodeFile(f);
            return bitmap;
        }
        else
        {               
            Bitmap bitmap = decodeFile(f);
            return bitmap;
        }

    }
    catch (Exception ex)
    {
        ex.printStackTrace();
        BitmapDrawable mDrawable = (BitmapDrawable) context.getResources().getDrawable(R.drawable.placeholder);
        return mDrawable.getBitmap();
    }
}

但是,如果没有连接,您将如何知道要在哪个位置加载哪个图像?

于 2011-05-16T11:20:35.417 回答