Удалить данные из списка - PullRequest
0 голосов
/ 19 декабря 2018

Я пытаюсь создать приложение, которое состоит из двух фрагментов.В 1-м фрагменте он состоит из всех моих контактов, и у каждого контакта есть флажок. Поэтому, когда я нажимаю на свой флажок, он добавляет этот контакт к моему следующему фрагменту.он добавляет это к следующему фрагменту, но когда я снимаю флажок, он ничего не делает.

Вот мой код:

Как я хочу удалить его из второго фрагмента.

2-й фрагментный код:

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 );
}
}

2-й фрагментный адаптер 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 );
    }
}

}

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...