How to improve the performance and customize the listview to avoid OMM error in Android

I am absolute beginner to Android and now I start using volley HTTP library with Listview. So I created a test project and trying on volley and Android. I am creating a newfeeds viewing app. Example , facebook newfeeds. Data are just dummy for learning purpose. My project is working as I expected. It loads new data when the scoll reach at the bottom of the listview. But after it loads the data for multiple times frequently, my App is throwing out of memory error.

My activity class

public class VolleyActivity extends Activity{
    private int defaultLastLoadedItem=0;
    private int lastSawFirstListItem;
    private int itemLoadedOn;
    private ArrayAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.volley_main);

        Button getBtn = (Button)findViewById(R.id.btn_get_request);
        getBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        ListView listView = (ListView)findViewById(R.id.volleyListView);
        adapter = new CustomAdapter(this,new ArrayList<Entity>());
        listView.setAdapter(adapter);
        updateListView();
        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if(lastSawFirstListItem<firstVisibleItem)
                {
                    int currentLastItem = firstVisibleItem+visibleItemCount;
                    if(currentLastItem!=itemLoadedOn)
                    {
                        updateListView();
                        Toast.makeText(getBaseContext(),"More item loaded",Toast.LENGTH_SHORT).show();
                        itemLoadedOn = currentLastItem;
                    }
                }
                lastSawFirstListItem = firstVisibleItem;
            }
        });
    }
    public void updatedListView(int lastloadedItem)
    {
        defaultLastLoadedItem = lastloadedItem;
        //"http://api.androidhive.info/feed/feed.json";
        String url = "http://192.168.43.82:8888/simple_json/index.json";//"http://api.androidhive.info/feed/feed.json";

        JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                List<Entity> items = new ArrayList<Entity>();
                try{
                    JSONArray jsonArray = jsonObject.getJSONArray("feed");
                    for(int i=0;i<jsonArray.length();i++)
                    {
                        JSONObject item = jsonArray.getJSONObject(i);
                        Entity en = new Entity();
                        en.setId(item.getInt("id"));
                        en.setName(item.getString("name"));
                        en.setUrl(item.getString("url"));
                        items.add(en);
                    }
                    adapter.addAll(items);

                }
                catch (JSONException e)
                {
                    Toast.makeText(getBaseContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(getBaseContext(),"Error listener",Toast.LENGTH_SHORT).show();
            }
        });

        Volley.newRequestQueue(getBaseContext()).add(jsonRequest);
    }
    public void updateListView()
    {
        updatedListView(defaultLastLoadedItem);
    }

}

What I want to know are:

  1. Will my application will throws that kind of error(omm) when it reaches to production ?

  2. If it will does, how can I fix that issue ?

  3. How should I customize listview for this kind of newfeeds app for better performance if my current code is not suitable?

Answers


I think your http request getting called all time while you are scrolling

you just have to add following code in your onScroll of setOnScrollListener

final int lastItem = firstVisibleItem + visibleItemCount;
if(lastItem == totalItemCount) {
    //your http request
}

I hope this will work for you.


Need Your Help

How to insertmany from a cursor using Pymongo?

python mongodb cursor pymongo

I am interested in using a cursor to duplicate a database from one mongod to another. I want to limit the amount of insert requests sent so instead of inserting each document in the cursor individu...

Upload event cover image on Facebook with PHP

php facebook facebook-graph-api facebook-php-sdk

I'd like to upload a cover image on a Facebook event page.