简体   繁体   English

Android-在ListView中避免重复

[英]Android - Duplicates in ListView avoid

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) ). 我一直在学习有关Lynda的课程,以将其实施到我的项目中( 构建适用于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. 有3个ListViews和3个按钮,当按下一个按钮时,您将进入更新列表视图的文本输入活动。

The 3 sections - Breakfast, Lunch and Dinner all have a ListView and a Button. 3个部分-早餐,午餐和晚餐都有一个ListView和一个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. 该按钮转到文本编辑器,并且当用户返回该特定部分的ListView时(无论他们单击哪个按钮)都将更新。

When the listviews are blank, I add a note which appears in one of the list view, for example the Breakfast section. 当列表视图为空白时,我添加了一个注释,该注释出现在列表视图之一中,例如Br​​eakfast部分。 If I go to a different button+listview, eg 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. 然后,当我为“晚餐”列表视图添加一个条目时,其中有3个项目,其中一个是“早餐”和“午餐”,然后是我刚刚输入的一个项目。

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. 返回主页时,ListView会更新。

@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 : 然后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? 所以我想避免重复,每个ListView是否必须有不同的mealList表? 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) { 编辑:另外,当我在“早餐”列表视图中有一个项目,然后在“午餐”列表视图中又有一个项目并删除一个项目时,未被删除的项目出现在所有ListViews中,这是代码删除,然后刷新:@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 编辑2:这是数据源类http://pastebin.com/iqrJx6Y5以及MealItem类http://pastebin.com/cpRan8jt的完整代码

These are initialised in my code private MealDataSource datasource; List<MealItem> mealsList; 这些是在我的代码private MealDataSource datasource; List<MealItem> mealsList;中初始化的private MealDataSource datasource; List<MealItem> mealsList; private MealDataSource datasource; List<MealItem> mealsList; in the same file as my other code snippets in the question. 与问题中其他代码段位于同一文件中。

You have same datasource for all three listviews. 您对所有三个列表视图都有相同的数据源。 You are doing mealsList = datasource.findAll(); 您正在做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 编辑:将您的MealDataSource类的构造函数更改为此

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 您为每顿饭使用相同的PREFKEY(实际上是文件名),这意味着您每次都将数据保存到同一文件中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM