嗨,我已经研究了一些有关此的文章,并尝试了人们一直在回答的所有内容,但是我没有任何运气。
我要执行的操作是,用户触摸浮动按钮并转到另一个活动,在该活动中,用户必须输入名称和访问次数才能添加带有该信息的其他卡片视图。我已经尝试了所有notifydatasetchanged(),notifyitemsinserted(),但是我没有任何运气。
我所做的是:
private RecyclerView recycler;
private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager lManager;
List<Anime> items = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Inicializar Animes
items.add(new Anime(R.drawable.angel, "Angel Beats", 230));
items.add(new Anime(R.drawable.death, "Death Note", 456));
items.add(new Anime(R.drawable.fate, "Fate Stay Night", 342));
items.add(new Anime(R.drawable.nhk, "Welcome to the NHK", 645));
items.add(new Anime(R.drawable.suzumiya, "Suzumiya Haruhi", 459));
// Obtener el Recycler
recycler = (RecyclerView) findViewById(R.id.reciclador);
recycler.setHasFixedSize(true);
// Usar un administrador para LinearLayout
lManager = new LinearLayoutManager(this);
recycler.setLayoutManager(lManager);
// Crear un nuevo adaptador
adapter = new AnimeAdapter(items);
recycler.setAdapter(adapter);
}
public void agregar (View view){
Intent intent = new Intent(this ,AgregarActivity.class);
startActivity(intent);
}
public void agregarItem(Anime anime){
items.add(anime);
adapter.notifyItemInserted(items.size() -1);
}
我的适配器
public class AnimeAdapter extends RecyclerView.Adapter<AnimeAdapter.AnimeViewHolder> {
private List<Anime> items;
public static class AnimeViewHolder extends RecyclerView.ViewHolder {
// Campos respectivos de un item
public ImageView imagen;
public TextView nombre;
public TextView visitas;
public AnimeViewHolder(View v) {
super(v);
imagen = (ImageView) v.findViewById(R.id.imagen);
nombre = (TextView) v.findViewById(R.id.nombre);
visitas = (TextView) v.findViewById(R.id.visitas);
((ImageButton) v.findViewById(R.id.detalle)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent (v.getContext(), AgregarActivity.class);
intent.putExtra("nom", nombre.getText().toString());
v.getContext().startActivity(intent);
}
});
}
}
public AnimeAdapter(List<Anime> items) {
this.items = items;
}
@Override
public int getItemCount() {
return items.size();
}
@Override
public AnimeViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.anime_card, viewGroup, false);
return new AnimeViewHolder(v);
}
@Override
public void onBindViewHolder(AnimeViewHolder viewHolder, int i) {
viewHolder.imagen.setImageResource(items.get(i).getImagen());
viewHolder.nombre.setText(items.get(i).getNombre());
viewHolder.visitas.setText("Visitas:" + items.get(i).getVisitas());
}
public void agregarItem(Anime anim){
items.add(anim);
notifyDataSetChanged();
}
和我的其他活动
MainActivity main = new MainActivity();
public void agregar(View view) {
EditText nom = ((EditText) findViewById(R.id.etNombre));
EditText vis = ((EditText) findViewById(R.id.etVisitas));
Anime anime = new Anime(R.drawable.house, nom.getText().toString(), Integer.parseInt(vis.getText().toString()));
MainActivity activity = new MainActivity();
activity.agregarItem(anime);
}
已解决:
我要做的是代替在mainActivity上使用startActivity,而是使用startActivityForResult,然后返回数据并使用重写方法onActivityResult对其进行处理