Android: Make LazyLoading ListView from GSON available without internet

依然范特西╮ 提交于 2019-12-13 05:14:40

问题


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 this listview accessible without an internet connection. Here I give you a snippet of 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()) {
            return true;
        } else {
                 }
             });
            return false;
        }
    }


}

Please advise if more of my codes are required. Thanks


回答1:


Look, you need to have the data in your application so that you can call them when no internet connection is available...

When you are getting the data save it somewhere in your application.Then pass the data in your Adapter..

The images will not be downloaded again...

In Fedor's lazylist the url of the images are static but here they are coming dynamically.

Hope this will help you.



来源:https://stackoverflow.com/questions/6014809/android-make-lazyloading-listview-from-gson-available-without-internet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!