我使用RecyclerView构建列表。当用户点击&#39; +&#39;工具栏中的按钮启动AlertDialog,提示用户在列表中添加项目。我在一个扩展DialogFragment的单独类中构建了AlertDialog。如何将项目添加到List<Task>
?
到目前为止,我在我的适配器类中做的是使List<Task> mTaskList
成为一个公共静态变量,我还创建了一个静态方法,它将返回此列表。我在DialogFragment类中调用此静态方法,以便检索列表。但我不认为这是好习惯。有没有更好的方法可以将项目添加到我的列表中?
TaskListFragment.java
public class TaskListFragment extends Fragment {
private RecyclerView mRecyclerView;
private TaskAdapter mAdapter;
private List<Task> mTaskList;
private static final String ADD_DIALOG = "add_dialog";
public TaskListFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_task_list, container, false);
mRecyclerView = view.findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
//mTaskList = createTasks();
//mAdapter = new TaskAdapter(mTaskList);
mRecyclerView.setAdapter(updateUI());
return view;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_list_item, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.add_new_task:
FragmentManager manager = getFragmentManager();
AddTaskFragment dialog = new AddTaskFragment();
dialog.show(manager, ADD_DIALOG);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/*
public List<Task> createTasks() {
List<Task> tasks = new ArrayList<>();
for(int i = 1; i <= 100; i++) {
Task task = new Task();
task.setTitle("Task #" + i);
task.setSolved(i % 2 == 0);
tasks.add(task);
}
return tasks;
}
*/
public TaskAdapter updateUI(){
if(mAdapter == null) {
mTaskList = new ArrayList<>();
mAdapter = new TaskAdapter(mTaskList);
}else {
mAdapter.notifyDataSetChanged();
}
return mAdapter;
}
}
TaskAdapter.java
public class TaskAdapter extends RecyclerView.Adapter<TaskAdapter.ViewHolder> {
public static List<Task> mTaskList;
public TaskAdapter(List<Task> taskList) {
mTaskList = taskList;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Task currentTask = mTaskList.get(position);
holder.bindData(currentTask);
}
@Override
public int getItemCount() {
return mTaskList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private TextView mTitle;
private CheckBox mSolved;
private ViewHolder(View itemView) {
super(itemView);
mTitle = itemView.findViewById(R.id.task_title);
mSolved = itemView.findViewById(R.id.task_solved);
}
private void bindData(Task task) {
mTitle.setText(task.getTitle());
mSolved.setChecked(task.isSolved());
}
}
public static List<Task> getTaskList(){
return mTaskList;
}
}
AddTaskFragment.java
public class AddTaskFragment extends DialogFragment {
private EditText mTaskTitle;
private List<Task> mTaskList;
private void addTask() {
if(!mTaskTitle.getText().toString().equals("")) {
Task task = new Task();
task.setTitle(mTaskTitle.getText().toString());
mTaskList = TaskAdapter.getTaskList();
mTaskList.add(task);
}
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
//return super.onCreateDialog(savedInstanceState);
View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_task_dialog,null);
mTaskTitle = view.findViewById(R.id.task_title);
return new AlertDialog.Builder(getContext())
.setView(view)
.setTitle("Add Task")
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
addTask();
}
})
.create();
}
}
答案 0 :(得分:0)
您可以在启动addTask()
时致电startActivityForResult,put
将Task
添加到Intent
内,您将在活动onActivityResult(int requestCode, int resultCode, Intent data)
中收到public class SomeContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
//I am using Postgres but will also accept answers to SQL Server as I want both ultimately
optionsBuilder.UseNpgsql(@"Host=localhost;Database=someDb;Username=user;Password=yeah");
}
}
}
services.AddDbContext<SomeContext>(cfg =>
{
cfg.UseSqlServer(_config.GetConnectionString("MyConnectionString"));
});
1}}。
答案 1 :(得分:0)
您可以考虑使用Google上次推出的ViewModel pattern。
关于sharing的部分在你的情况下会特别有用。