开发者

Android: GSON with Fedor's LazyList

开发者 https://www.devze.com 2023-03-06 16:57 出处:网络
I\'ve just implemented GSON to Fedor\'s LazyLoading ListView. That means the app saves the downloaded images and texts from the web to the external storage though an ImageLoader class.

I've just implemented GSON to Fedor's LazyLoading ListView. That means the app saves the downloaded images and texts from the web to the external storage though an ImageLoader class.

and I wonder why how to make the listview accessible without an internet connection? The app already succesfully saves the cache data into the SD Card, and now how to call those files? So when there's no internet, it will call the data (text and Image) from the SD Card.

Thanks. I'm open to any kind of solution

here's my ListView Class:

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()开发者_JS百科) {
                return true;
            } else {
                     }
                 });
                return false;
            }
        }


    }


Change your getBitmap() method in ImageLoader class to the below one

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

But if there is no connection how you will come to know that which image is to be loaded at which position?

0

精彩评论

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

关注公众号