I am trying to make a simple Todo list app using edit text and add button. the text that I type is not added into the list by clicking the add button. is there a problem with the way i am using function notifyDataSetCahnged(). please tell the right way to implement it.
code for main Activity
‘’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’
package com.example.todolist3;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.ArrayList;
public class RecyclerActivity extends AppCompatActivity {
private Button btnAdd;
private EditText etAdd;
private RecyclerView rvText;
ArrayList list;
ToDoListRecyclerAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycler);
btnAdd = findViewById(R.id.btnAdd);
etAdd = findViewById(R.id.etAdd);
rvText = findViewById(R.id.rvText);
rvText.setLayoutManager(new LinearLayoutManager(this));
list=new ArrayList<>();
list.add("Your text is entered here");
adapter = new ToDoListRecyclerAdapter(list);
rvText.setAdapter(adapter);
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String text = etAdd.getText().toString();
addText(text);
}
});
}
private void addText(String text){
list.add(text);
adapter.notifyDataSetChanged();
}
}
‘’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’
code for adapter
‘’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’
package com.example.todolist3;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class ToDoListRecyclerAdapter extends RecyclerView.Adapter<ToDoListRecyclerAdapter.ToDoListViewHolder> {
ArrayList list;
public ToDoListRecyclerAdapter(ArrayList list){
this.list=list;
}
@NonNull
@Override
public ToDoListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater li=(LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView=li.inflate(R.layout.todo_list_card,parent,false);
return new ToDoListViewHolder(itemView) ;
}
@Override
public void onBindViewHolder(@NonNull ToDoListViewHolder holder, int position) {
String TodoList=list.get(position);
holder.tvText.setText(TodoList);
}
@Override
public int getItemCount() {
return list.size();
}
class ToDoListViewHolder extends RecyclerView.ViewHolder{
TextView tvText;
public ToDoListViewHolder(@NonNull View itemView) {
super(itemView);
tvText=itemView.findViewById(R.id.tvText);
}
}
}
‘’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’