Android 为什么Fragment onResume()未正确使用RecyclerView?
在我的应用程序中,当下载时间从Alarm Receiver Service到达时,我使用AlarmManager计划下载任务。我下载文件并从自定义对象的ArrayList中删除,然后使用sharedpref保存它,如果它已经像这样运行,则在启动活动后保存它Android 为什么Fragment onResume()未正确使用RecyclerView?,android,android-recyclerview,notifydatasetchanged,Android,Android Recyclerview,Notifydatasetchanged,在我的应用程序中,当下载时间从Alarm Receiver Service到达时,我使用AlarmManager计划下载任务。我下载文件并从自定义对象的ArrayList中删除,然后使用sharedpref保存它,如果它已经像这样运行,则在启动活动后保存它 if (mIsInForegroundMode == true) { Intent intent1 = new Intent(this, DownloadingActivity.clas
if (mIsInForegroundMode == true) {
Intent intent1 = new Intent(this, DownloadingActivity.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent1);
}
如果活动正在运行,则将在获取保存列表和更新的位置调用Fragment
onResume()
@Override
public void onResume() {
super.onResume();
getDestroyedData();
adapter.notifyDataSetChanged();
}
public void getDestroyedData() {
String getPendingDownloadsList = preferences.getString("plist", "");
Type type2 = new TypeToken<List<DownloadingActivity.scheduleListType>>(){}.getType();
if (!getPendingDownloadsList.equals("")) {
pending=gson.fromJson(getPendingDownloadsList, type2);
}
}
更新这是我的适配器
public class PendingAdapter extends RecyclerView.Adapter<PendingAdapter.PendingHolder> {
ArrayList<DownloadingActivity.scheduleListType> pendingList;
LayoutInflater inflater;
PendingAdapter(ArrayList<DownloadingActivity.scheduleListType> pendingList, Context c) {
this.pendingList = pendingList;
inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public PendingHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = inflater.inflate(R.layout.custom_pending, parent, false);
PendingHolder pendingHolder = new PendingHolder(v);
return pendingHolder;
}
@Override
public void onBindViewHolder(PendingHolder holder, int position) {
holder.fileName.setText(pendingList.get(position).name);
if (pendingList.get(position).reqCODE != 0) {
holder.pendingType.setText("Start At " + pendingList.get(position).hour + ":" + pendingList.get(position).minute);
}
}
@Override
public int getItemCount() {
return pendingList.size();
}
//also tried this function
/** public void swap(ArrayList<DownloadingActivity.scheduleListType> newList) {
if (pendingList != null) {
pendingList.clear();
pendingList.addAll(newList);
} else {
pendingList = newList;
}
notifyDataSetChanged();
}**/
public class PendingHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView fileName, pendingType;
Button downloadByMobileBtn, removeFromPending;
public PendingHolder(View itemView) {
super(itemView);
pendingType = (TextView) itemView.findViewById(R.id.pendingType);
fileName = (TextView) itemView.findViewById(R.id.pendingFileNameTXT);
downloadByMobileBtn = (Button) itemView.findViewById(R.id.downloadByMobileBTN);
removeFromPending = (Button) itemView.findViewById(R.id.removeFromPending);
downloadByMobileBtn.setOnClickListener(this);
removeFromPending.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
ShowRemoveDialog(position);
}
}
});
}
@Override
public void onClick(View v) {
int p = getAdapterPosition();
if(p!=RecyclerView.NO_POSITION) {
if (v.getId() == downloadByMobileBtn.getId()) {
ShowDialog(p);
}
}
}
}
公共类PendingAdapter扩展了RecyclerView.Adapter{
ArrayList pendingList;
充气机;
PendingAdapter(ArrayList pendingList,上下文c){
this.pendingList=pendingList;
充气器=(LayoutInflater)c.getSystemService(Context.LAYOUT\u充气器\u SERVICE);
}
@凌驾
公共PendingHolder onCreateViewHolder(视图组父级,int-viewType){
视图v=充气机。充气(R.layout.custom_待定,父项,false);
PendingHolder PendingHolder=新PendingHolder(v);
返回挂起者;
}
@凌驾
BindViewHolder上的公共无效(PendingHolder holder,内部位置){
holder.fileName.setText(pendingList.get(position.name));
if(pendingList.get(position).reqCODE!=0){
holder.pendingType.setText(“从“+pendingList.get(position).hour+”:“+pendingList.get(position).minute开始”;
}
}
@凌驾
public int getItemCount(){
返回pendingList.size();
}
//也尝试了这个功能
/**公共无效交换(ArrayList newList){
如果(pendingList!=null){
pendingList.clear();
pendingList.addAll(newList);
}否则{
pendingList=新列表;
}
notifyDataSetChanged();
}**/
公共类PendingHolder扩展了RecyclerView.ViewHolder实现了View.OnClickListener{
TextView文件名,pendingType;
按钮downloadByMobileBtn,从待定中移除;
公共挂起文件夹(视图项视图){
超级(项目视图);
pendingType=(TextView)itemView.findViewById(R.id.pendingType);
fileName=(TextView)itemView.findViewById(R.id.pendingFileNameTXT);
downloadByMobileBtn=(按钮)itemView.findViewById(R.id.downloadByMobileBtn);
removeFromPending=(按钮)itemView.findViewById(R.id.removeFromPending);
downloadByMobileBtn.setOnClickListener(此);
removeFromPending.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
int position=getAdapterPosition();
如果(位置!=RecyclerView.无位置){
ShowRemoveDialog(位置);
}
}
});
}
@凌驾
公共void onClick(视图v){
int p=getAdapterPosition();
如果(p!=回收视图无位置){
if(v.getId()==downloadByMobileBtn.getId()){
显示对话框(p);
}
}
}
}
确保传递给适配器的列表引用得到更新。
在我的案例中,每次创建一个新列表都是一个问题。
例如
函数内部-
GetUpdatedList(ArrayList<String> myList)
{
myList.Append("foo");
}
GetUpdatedList(ArrayList myList)
{
myList.Append(“foo”);
}
似乎挂起的
是一个列表。然后,您不应该重新分配此列表,因为您在这里得到的新指针和适配器仍将指向旧指针。您应该清除列表,然后向其中添加项。
大概是这样的:
pending.clear();
pending.addAll(gson.fromJson(getPendingDownloadsList, type2));
我已经通过
pending.clear()
和pending.addAll(tempending)尝试过了
但没有任何效果我在这里能做的,请帮助在删除行时通过removeFromPending
按钮@stepanche再次将列表更新到上面的代码这是正确的方法。无需像ArrayList
那样编写-只在创建时以类型编写随处可见的列表
e> 新建ArrayListList和ArrayList
之间有什么区别没有任何东西可以解决我的问题请提供帮助
GetUpdatedList(myList);
GetUpdatedList(ArrayList<String> myList)
{
myList.Append("foo");
}
pending.clear();
pending.addAll(gson.fromJson(getPendingDownloadsList, type2));