从编辑文本内容创建RecyclerView列表

时间:2016-09-01 21:17:45

标签: java android list android-recyclerview

enter image description here 我在片段中有 RecyclerView ,我希望当用户按下“添加新行”下面的按钮时它会重定向到新片段,直到这里没有问题我能实现它但现在我希望表单中填写的详细信息将在recyclerview中显示为新行

正如您在上面的图片中看到的,用户可以动态地在RecyclerView中添加行,并且还有从列表中删除行的每行的右上角。

所以,我想做的是

1)如果用户按下保存事件,然后向用户显示用户新添加的行及其输入的详细信息,我将如何向列表中添加行< / p>

简而言之,我想在列表中动态添加行,用户输入表格,当用户按行项目右上角的十字按钮时,也要从列表中删除行

1 个答案:

答案 0 :(得分:0)

我自己解决了这个问题,我将数据从第二个片段转回到第一个片段 首先,我创建interface以将结果发送到fragment_1(具有recyclerview),来自fragment_2(具有形式)

这是我的表单代码

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.ppl_details_form,container,false);
        PPL_Title_Spinner= (Spinner) view.findViewById(R.id.PPL_Spinner);
        Title= (EditText) view.findViewById(R.id.ppl_title);
        Address= (AutoCompleteTextView) view.findViewById(R.id.ppl_address);
        Year= (EditText) view.findViewById(R.id.ppl_passing_year);
        Description= (EditText) view.findViewById(R.id.ppl_description);
        btn1= (Button) view.findViewById(R.id.ppl_map_button);

        Save= (Button) view.findViewById(R.id.ppl_save);
        Save_PPL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                title=Title.getText().toString();
                address= Address.getText().toString();
                year=Year.getText().toString();
                description=Description.getText().toString();
                //PPL Wrapper
                PPL_list_wrapper list=new PPL_list_wrapper(title,year,address);

                Add_PPL=(PPL_transfer_data_to_list)getActivity();
                Add_PPL.addLocation(list);
                fragment_1 fragment1=new fragment_1();
                FragmentTransaction fragmentTransaction=getFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.Navigation_Main_Layout,fragment1);
                fragmentTransaction.commit();

            }
        });


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try{
            Add_PPL= (PPL_transfer_data_to_list) context;
        }
        catch (ClassCastException e){
            throw new ClassCastException(getActivity().toString()+" Must Implement Interface");
        }
    }

然后我将infterface实现到Parent Activity中,然后从像这样的活动中调用fragment_1方法

 @Override
    public void addEvent(PPL_list_wrapper PPL_DATA) {
        fragment_1 recycler=new fragment_1();
        recycler.PPL_eve(PPL_DATA);

    }

然后我将数据导入fragment_1并且必须在片段static中生成arraylist因为没有静态列表也将丢失其引用和数据并且它将停止向recyclerview显示数据

 public void PPL_eve(final PPL_list_wrapper ppl_list_wrapper){
        PPL_wrapper=ppl_list_wrapper;   
        event_details.add(ppl_list_wrapper);//Static ArrayList
        adapter=new ppl_Recycler_Adapter(getActivity(),event_details);
        adapter.notifyDataSetChanged();

    }

我必须确保我的列表是静态的

static List<PPL_list_wrapper> event_details=new ArrayList<PPL_list_wrapper>();