简体   繁体   中英

my recyclerview has an adapter that is null

i want to get data from a edit text and give to adapter but adapter is null data isnt null because i can toast it but it doesnt show in my model

My question now is: when is an adapter null in RecyclerView and what might be the solution to the problem?

Please help a green developer; this is my first app. Any suggestion would be welcomed.

this is my Main Activity

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

adapter

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

        }
    }
}

and activity for result

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

change below code in 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();
        }
    }

You have a method in the adapter to add Note. So, you can directly use that method Adapter.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM