myrecyclerview有一个为null的适配器

l7wslrjt  于 2021-07-12  发布在  Java
关注(0)|答案(2)|浏览(340)

我想从编辑文本中获取数据并将其提供给适配器,但适配器为空数据不为空,因为我可以toast它,但它不会显示在我的模型中
我现在的问题是:当一个适配器在recyclerview中为空时,该问题的解决方案是什么?
请帮助绿色开发商;这是我的第一个应用程序。任何建议都将受到欢迎。
这是我的主要活动

public class MainActivity extends AppCompatActivity {
    public static final int request_code=1001;
    RecyclerView recyclerView;
    NoteListAdapter noteListAdapter;
    List<Notes> Notes_list=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView=findViewById(R.id.mainAc_recyclerview_notes);
        noteListAdapter = new NoteListAdapter(this,Notes_list);
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(noteListAdapter);
        Button newnote= findViewById(R.id.mainAc_button_new);
        newnote.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this,GenerateNote.class);
                startActivityForResult(intent,request_code);

            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==request_code && resultCode==RESULT_OK && data!=null){
            String name=data.getExtras().getString(result_name);
            String description=data.getExtras().getString(result_description);
            Notes notes=new Notes();
            notes.setSnTitle(name);
            notes.setSnDescription(description);
            NoteListAdapter adapter=new NoteListAdapter(this,Notes_list);
            adapter.addNote(notes);
            Toast.makeText(MainActivity.this,name,Toast.LENGTH_LONG).show();
        }
    }
}

适配器

public class NoteListAdapter extends RecyclerView.Adapter<NoteListAdapter.notesViewHolder> {
    @NonNull
    private Context context;
    private List<Notes> notes_list=new ArrayList<>();

    public NoteListAdapter(Context context, List<Notes> notes_list) {
        this.context = context;
        this.notes_list=notes_list;
    }

    @Override
    public NoteListAdapter.notesViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        View view = layoutInflater.inflate(R.layout.activity_sample_note, parent, false);
        return new notesViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull NoteListAdapter.notesViewHolder holder, int position) {
        holder.bindNotes(notes_list.get(position));
    }

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

    public void addNote(Notes notes){
        notes_list.add(notes);
        notifyItemInserted(1);
    }

    public  class notesViewHolder extends RecyclerView.ViewHolder {
        private  TextView title;
        private TextView description;

        notesViewHolder(@NonNull View itemView) {
            super(itemView);
            title = (TextView)itemView.findViewById(R.id.sn_textview_title);
            description = (TextView)itemView.findViewById(R.id.sn_textview_description);
        }

        public void bindNotes(Notes notes) {
            title.setText(notes.getSnTitle());
            description.setText(notes.getSnDescription());

        }
    }
}

为结果而努力

public class GenerateNote extends AppCompatActivity {
    public static final String result_name="result_name";
    public static final String result_description="result_description";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_generate_note);

        final EditText name=findViewById(R.id.gn_edittext_name);
        final EditText description=findViewById(R.id.gn_edittext_family);

        Button save=findViewById(R.id.gn_button_save);
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.putExtra(result_name,name.getText().toString());
                intent.putExtra(result_description,description.getText().toString());
                setResult(RESULT_OK,intent);
                finish();
            }
        });
    }
}
q8l4jmvw

q8l4jmvw1#

在onactivityresult();)中更改以下代码

@Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==request_code && resultCode==RESULT_OK && data!=null){
            String name=data.getExtras().getString(result_name);
            String description=data.getExtras().getString(result_description);
            Notes notes=new Notes();
            notes.setSnTitle(name);
            notes.setSnDescription(description);
            Notes_list.add(notes); // change here
            adapter.notifyDataSetChanged(); // change here
            Toast.makeText(MainActivity.this,name,Toast.LENGTH_LONG).show();
        }
    }
4ioopgfo

4ioopgfo2#

适配器中有一个方法可以添加注解。因此,您可以直接使用该方法适配器。

@Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==request_code && resultCode==RESULT_OK && data!=null){
            String name=data.getExtras().getString(result_name);
            String description=data.getExtras().getString(result_description);
            Notes notes=new Notes();
            notes.setSnTitle(name);
            notes.setSnDescription(description);
            adapter.addNote(notes); // Use Adapter method
            adapter.notifyDataSetChanged(); 
            Toast.makeText(MainActivity.this,name,Toast.LENGTH_LONG).show();
        }
    }

相关问题