Android, JAVA ListView Help

sachi911

Member
Jan 31, 2010
3,363
222
0
In heR HeaRT <3
I have a ListView on my app that extends the BaseAdapter. When i debug it via usb debugging, first time the ListView works. But when i run the app again or go back to the activity that contains the ListView, it doesnt populate. I just get a blank activity.

Here is my Adapter


Code:
public class CustomListAdapter extends BaseAdapter {

    private Activity activity;
    private LayoutInflater inflater;
    private List<Guide> guideItems;

    public CustomListAdapter(Activity activity, List<Guide> guideItems) {
        this.activity = activity;
        this.guideItems = guideItems;
    }

    @Override
    public int getCount() {
        return guideItems.size();
    }
    @Override
    public Object getItem(int location) {
        return guideItems.get(location);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

       // if (inflater == null)
            inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       // if (convertView == null)
        //    convertView = inflater.inflate(R.layout.customrow, null);
        convertView = inflater.inflate(R.layout.customrow, parent, false);

        //if (imageLoader == null)
        //    imageLoader = AppController.getInstance().getImageLoader();
       // NetworkImageView thumbNail = (NetworkImageView) convertView
       //         .findViewById(R.id.thumbnail);

        TextView ngno = (TextView) convertView.findViewById(R.id.txt_ListNgno);
        TextView name = (TextView) convertView.findViewById(R.id.txt_ListName);
        TextView email = (TextView) convertView.findViewById(R.id.txt_ListEmail);

        // getting guide data for the row
        Guide g = guideItems.get(position);
        //thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);
        ngno.setText(g.getNgno());
        name.setText(g.getName());
        email.setText(g.getEmail());

        return convertView;
    }
}


This is my ListView Activity.


Code:
public class ViewGuidesActivity extends AppCompatActivity {

    private static final String TAG = ViewGuidesActivity.class.getSimpleName();
    private ProgressDialog pDialog;
    private List<Guide> guideList = new ArrayList<Guide>();
    private ListView listView;
    private CustomListAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_guides);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        this.getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        listView = (ListView) findViewById(R.id.list);
        adapter = new CustomListAdapter(this, guideList);
        listView.setAdapter(adapter);

        pDialog = new ProgressDialog(this);
        // Showing progress dialog before making http request
        pDialog.setMessage("Loading...");
        pDialog.show();

        // Creating volley request obj
        JsonArrayRequest movieReq = new JsonArrayRequest(AppConfig.URL_GET_GUIDE_LIST,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        Log.d(TAG, response.toString());
                        hidePDialog();

                        // Parsing json
                        for (int i = 0; i < response.length(); i++) {
                            try {

                                JSONObject obj = response.getJSONObject(i);
                                Guide guide = new Guide();
                                guide.setNgno(obj.getString("ngno"));
                                guide.setEmail(obj.getString("email"));
                                guide.setName(obj.getString("name"));


                                // adding guide to guide array
                                guideList.add(guide);

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }

                        // notifying list adapter about data changes                  
                        adapter.notifyDataSetChanged();
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d(TAG, error.toString());
                VolleyLog.d(TAG, "Error: " + error.getMessage());
                hidePDialog();

            }
        });

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(movieReq);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
                Guide newGuide = guideList.get(position);
                String email = newGuide.getEmail();
                String ngno = newGuide.getNgno();
                //System.out.println(email);

                Intent i = new Intent(getApplicationContext(), GuideDetails.class);
                i.putExtra("email", email);
                i.putExtra("ngno", ngno);
                startActivity(i);
            }
        });

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        hidePDialog();
    }

    private void hidePDialog() {
        if (pDialog != null) {
            pDialog.dismiss();
            pDialog = null;
        }
    }
    
}


Any ideas why this happens? :( only way to fix this is to clear my application cache thought settings and run the app again. then it works the first time and same story happens. Do i have to purge the adapter memory or something when i leave the activity?
 
Last edited: