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"
};
}
我會加入list.add(routineName);低於onClick或更高版本的代碼? – hilllomax
你可以做到這一點,你已經檢查了routineName不是空的 – thunder413
好吧,我把它放在哪裏,它給了我一個以下消息在Arraylist中添加(packagename.Routine)不能應用於(android.widget.EditText)那麼如果我按ALT +輸入它說改變字段類型爲'java.util.ArrayList –
hilllomax