所以我有一个listeView,每个listeView项都有“添加到收藏夹”按钮,我要做的是每当用户单击“收藏夹”按钮时,此listView项就会被添加到“收藏夹”列表中(我使用片段)。所做的就是将此代码添加到我的自定义适配器getview方法中:
final Word currentWord = getItem(position);
final Button favorite = (Button) listItemView.findViewById(R.id.favorite);
favorite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
currentWord.setFavorite();
favoritewords.add(currentWord);
Toast.makeText(getContext(), "Ringtone Added To Favorite List", Toast.LENGTH_LONG).show();
}
});
现在我想将此单词对象的收藏夹数组列表发送到收藏夹列表片段中,这有可能吗?如果没有,请问有其他解决办法吗?
答案 0 :(得分:1)
最好在ListView
ListView listview = findViewById(R.id.listview);
List<long> favoriteWordIds = new ArrayList<>();
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
favoriteWordIds.add(l);
// Then use the ids to access them from database or somewhere
}
});
}