Reputation: 565
I'm making an app and a part of it is adding a note to a listview (it's a simple meal planner app). I've been following a course on Lynda to implement it into my project (Building a Note-Taking App for Android (2013)).
There are 3 ListViews and 3 Buttons, and when one button is pressed you go to the text-entry activity that updates the listview.
The 3 sections - Breakfast, Lunch and Dinner all have a ListView and a Button. The button goes to the text editor and when the user goes back the ListView for that specific section (whichever button they clicked on) is updated.
When the listviews are blank, I add a note which appears in one of the list view, for example the Breakfast section. If I go to a different button+listview, e.g. the Lunch section and I add a note, the note I just created and the note I created in the breakfast section are both in that listview. And then when I add an entry for the Dinner listview, 3 items are there, the one from Breakfast and Lunch and then the one I just entered.
The code for when a button is clicked:
//btnMoB = Button Monday Breakfast; btnMoL = Button Monday Lunch, etc
public void onClick(View v){
MealItem meal = MealItem.getNew();
Intent i = new Intent(this, MealEditor.class);
i.putExtra("key", meal.getKey());
i.putExtra("text", meal.getText());
if(v.getId() == R.id.btnMoB){
startActivityForResult(i, 1001);
}else if(v.getId() == R.id.btnMoL){
startActivityForResult(i, 1002);
}else if(v.getId() == R.id.btnMoD){
startActivityForResult(i, 1003);
}
}
The ListView(s) get updated when going back to the main page.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if ((requestCode == 1001 || requestCode == 1002 || requestCode == 1003) && resultCode == RESULT_OK){
MealItem meal = new MealItem();
meal.setKey(data.getStringExtra("key"));
meal.setText(data.getStringExtra("text"));
datasource.update(meal);
refreshDisplay(requestCode);
}
}
And then refreshDisplay
:
/* Refresh the Display */
private void refreshDisplay(int code) {
mealsList = datasource.findAll();
ArrayAdapter<MealItem> adapter =
new ArrayAdapter<MealItem>(this, R.layout.list_item_layout, mealsList);
if(code == 1001){
mLBMon.setAdapter(adapter);
}else if (code == 1002){
mLLMon.setAdapter(adapter);
}else if(code == 1003){
mLDMon.setAdapter(adapter);
}
}
So I want to avoid duplicates, would I have to have different mealList
for each ListView? It would be of great help if someone could guide me in the right direction and I would gladly provide more code if needed. Thanks!
Edit: Also, when I have one item in the "Breakfast" listview and then that one and another one in the "Lunch" listview and delete one, the one that is not deleted appears in all of the ListViews, here is the code that deletes then refreshes: @Override public boolean onContextItemSelected(MenuItem item) {
if(item.getItemId() == MENU_DELETE_ID){
MealItem meal = mealsList.get(currentNoteId);
datasource.remove(meal);
refreshDisplay(1001);
refreshDisplay(1002);
refreshDisplay(1003);
}
return super.onContextItemSelected(item);
}
Edit 2: Here is the full code of the data source class http://pastebin.com/iqrJx6Y5 and also the MealItem class http://pastebin.com/cpRan8jt
These are initialised in my code private MealDataSource datasource; List<MealItem> mealsList;
in the same file as my other code snippets in the question.
Upvotes: 0
Views: 156
Reputation: 999
You have same datasource for all three listviews. You are doing mealsList = datasource.findAll();
which i think returns all the items irrespective of their source. I would suggest that you create seperate datasource for each listview or put some logic to filter results for each listview.
Edit: Change constructor of you MealDataSource class to this
public MealDataSource(Context context, int code){
if (code == 1001)
mealPrefs = context.getSharedPreferences(PREFKEY, Context.MODE_PRIVATE);
else if(code == 1002)
mealPrefs = context.getSharedPreferences(PREFKEY1, Context.MODE_PRIVATE);
else if(code == 1003)
mealPrefs = context.getSharedPreferences(PREFKEY2, Context.MODE_PRIVATE);
}
You are using same PREFKEY (it is actually filename) for every meal which means you are saving data to same file every time
Upvotes: 2