• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

RecyclerView - specific design questions

RhinoCan

Well-Known Member
Jul 5, 2015
184
64
I'm working on my first app that uses RecyclerView and while I am making great progress, I strongly suspect that my design needs some changes.

I've asked in several places over recent weeks for complete examples of a RecyclerView that read, update, insert and delete and have come up empty so I'm guessing my way along based on various passing remarks. I think I've guessed wrong in at least a couple of respects. If you can answer these questions, you can help me get back on track.

First, let me explain as concisely as I can how I have designed this app. I imagine this will make my mistakes self-evident to you :)

My RecyclerView is based around sales in the small company where I work. Each sale consists of a client code, an order number, an order date, a number of tickets sold, and a seller name. My design uses a listener to react to a FAB (FloatingActionBar) and, if pressed, goes to an Add activity that prompts the user for the information needed to create a new sale. No information is passed to this activity because none is needed. The Add activity validates the data and, when the user presses an Add button, that new data is passed back to my main activity. The main activity inserts a row into a remote MySQL table via a PHP script invoked from an AsyncTask in my app. Upon returning from the database, if the insert worked (it could fail if it was a duplicate record), I add the information to my ArrayList and notifyItemInserted in the onPostExecute() method. That all seems to work okay although I usually get *two* copies of the new sale in the RecyclerView.

Each item in my RecyclerView contains all of the information for one sale, plus two clickable images, one intended for Editing (it's a blue pencil), and one intended for Remove (it's a red garbage can). If the user wants to edit that sale, he clicks on the blue pencil; if he wants to delete it, he clicks on the red garbage can. I use listeners to determine which image got clicked.

When someone clicks on the Edit graphic, I launch an Edit activity, passing the information from the existing sale to that activity. The activity displays the information and the user can modify any of the five fields. All changes are validated and, if all the validations are successful, the changed information is passed back to the Main activity which updates the existing row in the database in the doInBackground() method of an AsyncTask. If the update is successful, I try to change the information in my ArrayList and notifyItemChanged in onPostExecute(). That always fails.

When someone clicks on the Remove graphic, I launch a Remove activity, passing the information from the existing sale to that activity. The activity displays the information and the user can only press Remove to confirm that the sale should be removed or press Cancel to abort the removal. If Remove was pressed, the information from the sale is passed back to MainActivity which removes the existing row in the database in the doInBackground() method of an AsyncTask. If the database delete is successful, I try to delete the item from the ArrayList and then notifyItemRemoved in onPostExecute(). That always fails.

As you can see, the Edit and Remove both fail every time. The reason is that there is an indexOutOfBoundsException with respect to the position used in attempting to modify or remove the item from the ArrayList.

Now that you have an understanding of the situation, I can finally ask my specific questions:
  1. Is it reasonable/appropriate to use Activities to do the work I've described for Add, Edit and Remove processes in the first place or would I be better making them fragments?
  2. Is it appropriate to be doing the database activity and the adjustment of the ArrayList in the main activity or should I be letting the Add, Edit, and Remove activities (or fragments if that's better) do the work?
I think my fundamental problem is that my code can't "see" everything it needs to see at the point where it is doing its work. I think I need to redesign things a bit to make the app work better.

RecyclerView seems to be pretty fundamental so I want to make sure I write everything correctly. In fact, given the absence of good examples that show a RecyclerView that has all the functionality I'm describing, I'd like to write a tutorial or make a video series showing my finished app with all the critical parts explained so that others can learn from it.
 
Your general design is sound. Having separate Activities to add/edit a new item is a standard architectural pattern.
The issues with list updates aren't really possible to solve, given the above description. You need to work through the code, and trace what's happening. This is what code debugging is all about.
Nobody said coding was easy. Well, it is when everything works ;)
 
Upvote 0
Your general design is sound. Having separate Activities to add/edit a new item is a standard architectural pattern.
The issues with list updates aren't really possible to solve, given the above description. You need to work through the code, and trace what's happening. This is what code debugging is all about.
Nobody said coding was easy. Well, it is when everything works ;)

It's very reassuring to know that I'm going about this in a reasonable way. I seriously thought I would get an answer along the lines of "well *of course* you should use fragments, everyone knows this. You have exactly the sorts of problems you're describing if you use activities."

I'm fine with debugging the problem myself now that I know there's no fundamental design flaw in my app. :)
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones