简体   繁体   中英

Adding item dynamically to recyclerview

I am building a notepad app. I am trying to add another item to the recyclerview as soon as the new note is saved by the user.

 public class NotePad extends AppCompatActivity {

    public static final String EXTRA_MESSAGE = "key";

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

      final FloatingActionButton button = 
      (FloatingActionButton)findViewById(R.id.save_button);

      button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           SaveData();
        }
      });
   }

   // To Send Note Title to MainActivity
   public void SendData() {
     EditText editText = findViewById(R.id.textView3);

     final String str = editText.getText().toString();
     Log.d("Check","str:"+str);
     Intent intent = new Intent(NotePad.this,MainActivity.class);
     intent.putExtra(EXTRA_MESSAGE, str);
     startActivity(intent);
   }



   // To Save Note Content
   public void SaveData() {
     FileOutputStream outputStream;

     try {
        EditText editText = findViewById(R.id.textView3);
        final String str = editText.getText().toString();
        String content = findViewById(R.id.textView).toString();
        Log.d("Test","inside try block");
        outputStream = openFileOutput(str, Context.MODE_PRIVATE);
        outputStream.write(content.getBytes());

        Log.d("Test","save done");
        SendData();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
  }

I am saving a note and sending the title(str) that is to be displayed in recyclerview row to MainActivity as shown above.

  public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager layoutManager;
    ArrayList<String> notes = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      setContentView(R.layout.activity_main);

      Intent intent = getIntent();
      String title = intent.getStringExtra(NotePad.EXTRA_MESSAGE);
      Log.d("Check","title added"+title);
      notes.add(title);
      BuildRecyclerView();
    }

    public void BuildRecyclerView() {
      recyclerView = (RecyclerView)findViewById(R.id.recycle_list);

      layoutManager = new LinearLayoutManager(this);
      recyclerView.setLayoutManager(layoutManager);
      mAdapter = new MyAdapter(notes);
      recyclerView.setAdapter(mAdapter);
    }

But whenever I add a new note it overwrites the first entry in the recyclerview instead of occupying the next row of the recyclerview . This is my adapter code:

    public class MyAdapter extends 
    RecyclerView.Adapter<MyAdapter.MyViewHolder> {

      ArrayList<String> Notes_list = new ArrayList<String>();

      public MyAdapter(ArrayList<String> notes) {
        Notes_list = notes;
      }

      @NonNull
      @Override
      public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int 
      viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.list_items,parent,false);
        return new MyViewHolder(view);
      }

      @Override
      public void onBindViewHolder(@NonNull MyViewHolder holder, int 
      position) {
        holder.mTextView.setText(Notes_list.get(position));
      }

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

      public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView mTextView;
        public MyViewHolder(@NonNull View itemView) {
          super(itemView);
          mTextView = (TextView) itemView.findViewById(R.id.view_holder);
        }
      } 
   }

Because you pass last item to MainActivity and generate a new RecyclerView with passed item. You have to keep notes array on first class and pass whole array to MainActivity with intent.

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