在新添加的ListView项目上显示/隐藏“新”徽章

时间:2018-11-08 13:41:32

标签: android android-listview

我有一个ListView,并且我想在列表项上显示ImageView(新徽章)(如果最近添加的话)。

Like so: 所以我在模型类中添加了一个布尔isNew:

public class Item {
private String name;
private String profession;
private boolean isNew = true;

//Constructor
public Item(String name, String profession) {
    this.name = name;
    this.profession = profession;
}

//Getters
public String getName() {
    return name;
}

public String getProfession() {
    return profession;
}

public boolean isNew() {
    return isNew;
}

//Setters
public void setName(String name) {
    this.name = name;
}

public void setProfession(String profession) {
    this.profession = profession;
}

public void setNew(boolean aNew) {
    isNew = aNew;
} }

“新徽章”是ImageView,默认情况下其可见性设置为“消失”。

然后,我在适配器中有一条if语句,用于检查特定项目的isNew布尔值是否为true,并基于此,通过调用ImageView使它成为“新徽章”(即ImageView,VISIBLE) .setVisibility()

public class ItemAdapter extends ArrayAdapter<Item> {
private List<Item> items;
private Context context;


public ItemAdapter(Context context, List<Item> items) {
    super(context, R.layout.single_list_item, items);
    this.context = context;
    this.items = items;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater layoutInflater = LayoutInflater.from(context);
    View view = layoutInflater.inflate(R.layout.single_list_item, parent, false);

    //Binding the views from the single_item_layout
    TextView mName = view.findViewById(R.id.mName);
    TextView mProfession = view.findViewById(R.id.mProfession);
    ImageView new_badge = view.findViewById(R.id.new_badge);

    //Get the data from the List
    Item item = items.get(position);

    //Setting the data
    mName.setText(item.getName());
    mProfession.setText(item.getProfession());

    //Make 'new_badge' VISIBLE if the item is new
    if (item.isNew()){
        new_badge.setVisibility(View.VISIBLE);
    }

    //Return the view
    return view;
} }

然后在main_activity中,我有一个ListView OnItemClickListener,并且它定义了当单击列表项时,该特定项的布尔值设置为false,然后我调用adapter.notifyDataSetChanged()

public class MainActivity extends AppCompatActivity {
//ListView
private ListView mListView;

//ArrayList
static List<Item> items = new ArrayList<>();

//Adapter
ItemAdapter itemAdapter;

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

    //Initializing the ListView
    mListView = findViewById(R.id.mListView);

    //Adding Data to the ArrayList of items
    items.add(new Item("Danyl Fernandes", "Developer"));
    items.add(new Item("Henry Yuming", "Web Designer"));
    items.add(new Item("Ashley Janberg", " Park Naturalist"));
    items.add(new Item("Mary Almen", "Civil Drafter"));
    items.add(new Item("Carola Traino", "Insurance Appraiser"));
    items.add(new Item("Daniela Mocanu", "Infantry Officer"));
    items.add(new Item("Jaden Labuguen", "Surveyor"));
    items.add(new Item("Zaketa Nichola", "Petroleum Engineer"));

    //Initializing the Adapter
    itemAdapter = new ItemAdapter(this, items);

    //Set the Adapter to the ListView
    mListView.setAdapter(itemAdapter);

    //Handle ListItem Clicks
    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
             Item item = (Item) mListView.getItemAtPosition(i);
             item.setNew(false);
             itemAdapter.notifyDataSetChanged();
        }
    });
} }

这样可以正常工作,当单击任何项​​目时,NewBadge都会消失 like so

但是问题是 ,当我重新启动应用程序时,所有歌曲再次显示为新歌曲,即使我单击的歌曲也已经显示了“新徽章”

Here's a Github link to the entire project:

请帮助! 谢谢

2 个答案:

答案 0 :(得分:0)

之所以发生这种情况,是因为每当重新启动应用程序时,总是会创建一组新的项目。一旦销毁一个类,它将清除所有临时数据。要查看项目中的更改,您必须保存它们,并在用户单击某个项目时在数据库中更改该项目的标志。

答案 1 :(得分:0)

每次您重新启动应用程序时,所有项目都是新的,因为您什么都不保存:)

我建议使用数据库来管理所有项目。这样,您可以在单击一个新用户时进行保存,并且“新”消息不会再次出现。

如果我可以为此推荐一个库,请使用Room

这是一个表格模型的示例:

@Entity
public class Item
{
    @ColumnInfo(name = "Name")
    public String name;

    @ColumnInfo(name = "Profession")
    public String profession;

    @ColumnInfo(name = "New")
    public boolean new;
}