Android:GSON与Fedor的LazyList

时间:2011-05-16 10:29:16

标签: android gson lazylist

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

我想知道如何在没有互联网连接的情况下访问listview? 该应用程序已成功将缓存数据保存到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;
            }
        }


    }

1 个答案:

答案 0 :(得分: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();
    }
}

但是如果没有联系你怎么知道哪个图像要加载到哪个位置?