Tag Archives: adapter

Application Structure

I’m currently adding some new features to my app. Here’s my summary of changes since my last post:

  1. Added  Navigation Drawer go the first “page” of the application which allowed me to add links to things like an About fragment, Copyright fragment, Signup and Login Fragments, etc. I really like the Navigation Drawer UI pattern and will probably roll it out to the rest of the pages once I’m done testing thoroughly. My Navigation Drawer’s a little bare now so once I’ve worked out any lingering kinks, I’ll add some icons to the left of the drawer menu items.
  2. Used Parse to manage the users of my application. Currently, the app supports user signups, user login and allows the user to use the floating context menu to save their favorite restaurants to a Parse database.

I’m trying to avoid the shiny toy syndrome so I think I will stop at this latest iteration, focus on “hardening” the application and continue testing. Check the pics about and I’ll share one cool thing I learned from tonight’s coding binge. So, I created a custom adapter to handle my Restaurant objects. to give me greater control over what was returned when selecting items from my ListView and to present a prettier display. This is what that code snippet looks like:


ArrayList[Restaurant] rr = restaurantDAO.getRestaurantsByCounty(county);
RestaurantAdapter adapter = new RestaurantAdapter(this,R.layout.itemlisting, rr);
ListView listView = (ListView) findViewById(R.id.restaurantsListView);
listView.setAdapter(adapter);

When I was handling onItemClick events on the ListView called listView, I was grabbing a String representation of my Restaurant and was doing some string manipulation on the values returned from what was essentially myRestaurantObject.toString().  While I was adding the floating context menu to listView, I realized that I didn’t need to convert my Restaurant object to a string in order to get the restaurant ID from a series of string operations! I know this seems obvious in hindsight but when I was learning how to handle onItemClick events on ListView objects, I blindly followed a recommendation to do this:

String rString = parent.getItemAtPosition(position).toString()

When I needed to do was:

Restaurant temp = (Restaurant) parent.getItemAtPosition(position);

So, long story short, if you’ve created a custom Adapter, don’t let your hard work go to waste! I saved myself an unneeded database call once I realized I could grab my Restaurant object from the ListView instead of parsing the toString() value for the restaurant ID and then querying the database.

Screenshot_2013-11-01-21-49-00 Screenshot_2013-11-01-22-40-16 Screenshot_2013-11-01-21-49-19 Screenshot_2013-11-01-22-40-00 Screenshot_2013-11-01-21-49-33

Lastly, I may have gone a little overboard on organizing my files into packages. I’m not sure what the proper way to group things so I just went with what would make my life easier. So far, here’s the layout of my Java files. That’s a lot of code for a simple app but my chief concern is readability so wherever private inner fragments were used, I pulled those out into separate classes and tried to keep the names consistent. My goal is to be able to forget about this app for a month and pick up where I left off with minimal difficulty. I’m open to any conventions or better ways of organizing. With that, I bid you a good night! 🙂

Screenshot from 2013-11-01 22:24:57

Advertisements