0

我试图从editText字段获取输入以更新到我的recyclerView。我需要添加或删除代码才能实现此目的?我正在使用一个已经包含一些数据的数组列表。现在我想要和列表中的新标题。当用户点击FAB时,我会设置一个警报对话框,显示他们可以输入名称的位置。一旦用户按是,我想这个名字去recyclerView。当在AlertDialog中选择positiveButton时,将editText输入添加到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" 
 
    }; 
 

 
}

回答

0

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

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

我会加入list.add(routineName);低于onClick或更高版本的代码? – hilllomax

+0

你可以做到这一点,你已经检查了routineName不是空的 – thunder413

+0

好吧,我把它放在哪里,它给了我一个以下消息在Arraylist中添加(packagename.Routine)不能应用于(android.widget.EditText)那么如果我按ALT +输入它说改变字段类型为'java.util.ArrayList hilllomax

相关问题