如何在RecyclerView中显示输入数据?

时间:2018-11-06 10:26:36

标签: java android android-recyclerview

该应用程序将待办事项添加到RecycleList。我做了Add todo部分,将它插入到我遇到问题的RecyclerView中。 该应用程序应为:

添加到做

Add to do

RecycleList

RecycleList

------------ MyCode --------------

MainActivity

public class MainActivity extends AppCompatActivity {

Dialog todo_dialoge;

ArrayList<Todo> todoArrayList;

 Todo todo;
 RecyclerView todoRecyclerView;
 RecyclerView.Adapter todoAdapter;


ImageButton add_button;
ImageButton edit_imageButton;

String title;
String completed;
String priority;
String date;
String time;


TextView dateText, timeText;
EditText titleText;
RadioButton high_but, medium_but, low_but, done_but, notDone_but;



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




    todoRecyclerView = findViewById(R.id.todo_recycler_view);
    todoRecyclerView.setHasFixedSize(true);
    todoRecyclerView.setLayoutManager(new LinearLayoutManager(this));


    todoArrayList = new ArrayList<>();


    todoAdapter = new TodoAdapter (todoArrayList , this );


}

public void showTodoDialoge(View view) {



    todo_dialoge = new Dialog(this);

    todo_dialoge.setContentView(R.layout.add_todo_dialog);



    todo_dialoge.show();



}


public void resetButton(View view) { }


    public void DateButton(View view) {  }

   public void TimeButton(View view) {}

   public void CancelButton(View view) {}


    //this button is when user press submit it saves the data to an array list



  public void saveButton(View view) {
    Todo todo= new Todo(title , completed ,priority ,date ,time);

    todoArrayList.add(todo);


}

TodoAdapter类

public class TodoAdapter extends RecyclerView.Adapter<TodoAdapter.ViewHolder> {

private ArrayList <Todo> todoArrayList ;
private Context context ;

public TodoAdapter(ArrayList<Todo> todoArrayList, Context context) {
    this.todoArrayList = todoArrayList;
    this.context = context;

}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

    View v  = LayoutInflater.from(viewGroup.getContext())
            .inflate(R.layout.list_item_todo, viewGroup ,false);


    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {

   Todo todo = todoArrayList.get(i);

    viewHolder.titleText.setText(todo.getTitle());
    viewHolder.priorityText.setText(todo.getPriority());
    viewHolder.dateText.setText(todo.getDate());
    viewHolder.timeText.setText(todo.getTime());

    viewHolder.statusBox.setChecked(false);


    viewHolder.edit_image.setImageResource(R.drawable.ic_edit_black_24dp);
    viewHolder.open_image.setImageResource(R.drawable.ic_refresh_black_24dp);
    viewHolder.delet_image.setImageResource(R.drawable.ic_delete_black_24dp);






}

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

public class ViewHolder extends RecyclerView.ViewHolder{

    public TextView titleText;
    public TextView priorityText;
    public TextView dateText;
    public TextView timeText;
    public CheckBox statusBox;
    public ImageButton edit_image;
    public ImageButton open_image;
    public ImageButton delet_image;



    public ViewHolder (View itemView) {
        super(itemView);


        titleText = itemView.findViewById(R.id.title_textView);

        priorityText = itemView.findViewById(R.id.priority_text);

        dateText= itemView.findViewById(R.id.date_textView);

        timeText= itemView.findViewById(R.id.time_textView);

        statusBox  = itemView.findViewById(R.id.complete_checkBox);

        edit_image = itemView.findViewById(R.id.edit_imageButton);

        open_image = itemView.findViewById(R.id.open_imageButton);

        delet_image = itemView.findViewById(R.id.delete_imageButton);




    }
}
}

任务是将用户输入的数据添加到“添加待办事项”(如标题,日期,时间等)并插入到待办事项列表中。

问题在于数据没有显示在RecyclerView上。

1 个答案:

答案 0 :(得分:0)

您应该在致电todoAdapter.notifyDatasetChanged()之后致电todoArrayList.add()