在AlertDialog中选择positiveButton时,将editText输入添加到recyclerView(Adding editText input to recyclerView when

编程入门 行业动态 更新时间:2024-10-25 22:35:53
在AlertDialog中选择positiveButton时,将editText输入添加到recyclerView(Adding editText input to recyclerView when positiveButton is selected in AlertDialog)

我试图从editText字段获取输入以更新到我的recyclerView。 为了实现这一目标,我需要添加或删除我的代码? 我正在使用一个已经包含一些数据的arraylist。 现在我想要和列表中的新标题。 当用户点击FAB时,我设置了一个警告对话框,显示他们可以输入名称的位置。 一旦用户按下是,我希望该名称转到recyclerView。

public class Workout extends Fragment {
  RecyclerView recyclerView;
  RecyclerView.Adapter adapter;
  RecyclerView.LayoutManager layoutManager;
  ArrayList < RoutineList > list = new ArrayList < RoutineList > ();
  String[] name;


  @
  Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    final View rootView = inflater.inflate(R.layout.workout_list_activity, container, false);

    FloatingActionButton fab = (FloatingActionButton) rootView.findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {@
      Override
      public void onClick(View view) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // Get the layout inflater
        LayoutInflater inflater = getActivity().getLayoutInflater();

        // Inflate and set the layout for the dialog
        // Pass null as the parent view because its going in the dialog layout
        view = inflater.inflate(R.layout.dialog_fab, null);
        final EditText routineName = (EditText) view.findViewById(R.id.workoutTitle);
        builder.setView(view)

        // Add action buttons
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int id) {
              if (!TextUtils.isEmpty(routineName.getText().toString())) {
                // This block is responsible for checking if editText is empty and carrying out the action


              }
            }
          })
          .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
              // User cancelled the dialog
            }
          });
        // Create the AlertDialog object and return it
        AlertDialog dialog = builder.create();
        dialog.show();

        final Button positiveButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
        routineName.addTextChangedListener(new TextWatcher() {@
          Override
          public void beforeTextChanged(CharSequence s, int start, int count, int after) {

          }

          @
          Override
          public void onTextChanged(CharSequence s, int start, int before, int count) {


          }

          @
          Override
          public void afterTextChanged(Editable s) {

          }
        });



      }
    });


    name = VersionModel.data;
    for (String Name: name) {
      RoutineList routineList = new RoutineList(Name);
      list.add(routineList);
    }

    recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);
    layoutManager = new LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

    recyclerView.setHasFixedSize(true);

    adapter = new RoutineListAdapter(list, getActivity());
    recyclerView.setAdapter(adapter);
    return rootView;
  }

  public static Workout newInstance() {
    Workout workout = new Workout();
    return workout;
  }
} 
  
 

public class RoutineListAdapter extends RecyclerView.Adapter < RoutineListAdapter.RoutineListViewHolder > {
  ArrayList < RoutineList > routineLists = new ArrayList < RoutineList > ();
  Context context;



  public RoutineListAdapter(ArrayList < RoutineList > routineLists, Context context) {

    this.routineLists = routineLists;
    this.context = context;

  }

  public RoutineListAdapter(ArrayList < RoutineList > list) {}

  @
  Override
  public RoutineListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_list_item_1, parent, false);
    RoutineListViewHolder routineListViewHolder = new RoutineListViewHolder(view, context, routineLists);

    return routineListViewHolder;
  }

  @
  Override
  public void onBindViewHolder(RoutineListViewHolder holder, int position) {
    RoutineList routineList = routineLists.get(position);
    holder.routine_name.setText(routineList.getName());


  }


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


  public static class RoutineListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    TextView routine_name;
    ArrayList < RoutineList > routineLists = new ArrayList < RoutineList > ();
    Context context;

    public RoutineListViewHolder(View view, Context context, ArrayList < RoutineList > routineLists) {
      super(view);
      this.routineLists = routineLists;
      this.context = context;

      view.setOnClickListener(this);
      routine_name = (TextView) view.findViewById(android.R.id.text1);
    }


    @
    Override
    public void onClick(View v) {
      int position = getAdapterPosition();
      RoutineList routineList = this.routineLists.get(position);
      Intent intent = new Intent(this.context, RoutineDetails.class);
      intent.putExtra("name", routineList.getName());
      this.context.startActivity(intent);

    }
  }
} 
  
 

public class RoutineList {
    public RoutineList(String name){
        this.setName(name);
    }
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
} 
  
 

这就是arraylist正在拉取已经制作的数据的地方。

public class VersionModel {
  public static final String[] data = {
    "Full Body", "Power Legs", "Traps Day", "Arm Blast", "Butt Toner",
    "Fat Killer", "Lower Body", "Big Arms", "Tri Day",
    "Bigger Chest", "Cardio", "Back Blast", "Forearms", "Calf Day", "Train for 5K"
  };

} 
  
 

I am trying to get input from the editText field to update to my recyclerView. What do I need to add or take away from my code to make this happen? I am using a arraylist with some data already in it. Now I want and new titles to the list. I have it setup when user taps the FAB an alert dialog shows where they can enter a name. Once user press yes I would like that name to go to the recyclerView.

public class Workout extends Fragment {
  RecyclerView recyclerView;
  RecyclerView.Adapter adapter;
  RecyclerView.LayoutManager layoutManager;
  ArrayList < RoutineList > list = new ArrayList < RoutineList > ();
  String[] name;


  @
  Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    final View rootView = inflater.inflate(R.layout.workout_list_activity, container, false);

    FloatingActionButton fab = (FloatingActionButton) rootView.findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {@
      Override
      public void onClick(View view) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // Get the layout inflater
        LayoutInflater inflater = getActivity().getLayoutInflater();

        // Inflate and set the layout for the dialog
        // Pass null as the parent view because its going in the dialog layout
        view = inflater.inflate(R.layout.dialog_fab, null);
        final EditText routineName = (EditText) view.findViewById(R.id.workoutTitle);
        builder.setView(view)

        // Add action buttons
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int id) {
              if (!TextUtils.isEmpty(routineName.getText().toString())) {
                // This block is responsible for checking if editText is empty and carrying out the action


              }
            }
          })
          .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
              // User cancelled the dialog
            }
          });
        // Create the AlertDialog object and return it
        AlertDialog dialog = builder.create();
        dialog.show();

        final Button positiveButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
        routineName.addTextChangedListener(new TextWatcher() {@
          Override
          public void beforeTextChanged(CharSequence s, int start, int count, int after) {

          }

          @
          Override
          public void onTextChanged(CharSequence s, int start, int before, int count) {


          }

          @
          Override
          public void afterTextChanged(Editable s) {

          }
        });



      }
    });


    name = VersionModel.data;
    for (String Name: name) {
      RoutineList routineList = new RoutineList(Name);
      list.add(routineList);
    }

    recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);
    layoutManager = new LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

    recyclerView.setHasFixedSize(true);

    adapter = new RoutineListAdapter(list, getActivity());
    recyclerView.setAdapter(adapter);
    return rootView;
  }

  public static Workout newInstance() {
    Workout workout = new Workout();
    return workout;
  }
} 
  
 

public class RoutineListAdapter extends RecyclerView.Adapter < RoutineListAdapter.RoutineListViewHolder > {
  ArrayList < RoutineList > routineLists = new ArrayList < RoutineList > ();
  Context context;



  public RoutineListAdapter(ArrayList < RoutineList > routineLists, Context context) {

    this.routineLists = routineLists;
    this.context = context;

  }

  public RoutineListAdapter(ArrayList < RoutineList > list) {}

  @
  Override
  public RoutineListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_list_item_1, parent, false);
    RoutineListViewHolder routineListViewHolder = new RoutineListViewHolder(view, context, routineLists);

    return routineListViewHolder;
  }

  @
  Override
  public void onBindViewHolder(RoutineListViewHolder holder, int position) {
    RoutineList routineList = routineLists.get(position);
    holder.routine_name.setText(routineList.getName());


  }


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


  public static class RoutineListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    TextView routine_name;
    ArrayList < RoutineList > routineLists = new ArrayList < RoutineList > ();
    Context context;

    public RoutineListViewHolder(View view, Context context, ArrayList < RoutineList > routineLists) {
      super(view);
      this.routineLists = routineLists;
      this.context = context;

      view.setOnClickListener(this);
      routine_name = (TextView) view.findViewById(android.R.id.text1);
    }


    @
    Override
    public void onClick(View v) {
      int position = getAdapterPosition();
      RoutineList routineList = this.routineLists.get(position);
      Intent intent = new Intent(this.context, RoutineDetails.class);
      intent.putExtra("name", routineList.getName());
      this.context.startActivity(intent);

    }
  }
} 
  
 

public class RoutineList {
    public RoutineList(String name){
        this.setName(name);
    }
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
} 
  
 

This is where arraylist is pulling already made data.

public class VersionModel {
  public static final String[] data = {
    "Full Body", "Power Legs", "Traps Day", "Arm Blast", "Butt Toner",
    "Fat Killer", "Lower Body", "Big Arms", "Tri Day",
    "Bigger Chest", "Cardio", "Back Blast", "Forearms", "Calf Day", "Train for 5K"
  };

} 
  
 

最满意答案

//将新例程名称添加到列表list.add(routineName);

// Then notify the your adapter that list has changed adapter.notifyDataSetChange();

// Add the new routine name to your list list.add(routineName);

// Then notify the your adapter that list has changed adapter.notifyDataSetChange();

更多推荐

本文发布于:2023-08-07 14:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464709.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:editText   positiveButton   AlertDialog   recyclerView   selected

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!