从列表中删除数据

时间:2018-12-19 04:52:59

标签: android-fragments arraylist android-recyclerview

我正在尝试创建一个包含两个片段的应用程序。在第一个片段中,它包含我的所有联系人,每个联系人都有复选框,因此,当我单击复选框时,会将该联系人添加到下一个片段中。

到目前为止,我已经创建了我的联系人列表,每当我选中复选框时,它都会将其添加到下一个片段中,但是当我取消选中它时,它什么都不做。

这是我的代码:

我想将其从第二个片段中删除。

第二个片段代码:

public class Favorite extends Fragment {
TextView text;
String name;
ArrayList<favoriteModel> getfav = new ArrayList<favoriteModel>(  );
RecyclerView favRecycler;
favoriteAdapter fAdapter;

public Favorite() {

}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate( R.layout.fragment_favorite, container, false );
    favRecycler = (RecyclerView) view.findViewById( R.id.favoriteRecycler );
    favRecycler.setLayoutManager( new LinearLayoutManager( getContext() ) );
    return view;
}

public void displayRecivedData(String favname, String id, String favnum, String status) {
    if(status.equals( "true" )){
        getfav.add( new favoriteModel( favname,favnum,id, status ));

    }else{
       Toast.makeText( getContext(), "I will remove it", Toast.LENGTH_SHORT ).show(); 
//should I remove it from here?
                }
            }
        }
    }
    fAdapter = new favoriteAdapter(getContext(), getfav);
    favRecycler.setAdapter( fAdapter );
}
}

第二个片段Recyclerview适配器:

public class favoriteAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
Context context;
ArrayList<favoriteModel> getfav = new ArrayList<favoriteModel>(  );
LayoutInflater layoutInflater;
public favoriteAdapter(Context context, ArrayList<favoriteModel> getfav) {
    this.context = context;
    this.getfav = getfav;
    layoutInflater = LayoutInflater.from( context );
}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = layoutInflater.inflate( R.layout.favorite_list, viewGroup, false );
    favoritelayout layout = new favoritelayout(view );
    return layout;
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
    favoritelayout layout = (favoritelayout) viewHolder;
    favoriteModel fav_details = getfav.get( i );
    String flag = fav_details.getFav_status();
    String id = fav_details.getFav_contactid();
    String tempname = fav_details.getFav_contactname();

    layout.contactnum.setText( "   " + fav_details.getFav_contactnumber() );
    layout.contactname.setText( "   " + fav_details.getFav_contactname() );
            }
        }

    }
    }


@Override
public int getItemCount() {
    return getfav.size();
}

private class favoritelayout extends RecyclerView.ViewHolder {
    TextView contactname, contactnum;
    public favoritelayout(View view) {
        super(view);
        contactname = (TextView) view.findViewById( R.id.fav_name );
        contactnum = (TextView) view.findViewById( R.id.fav_phonenumber );
    }
}

}

0 个答案:

没有答案