片段页面中对话框片段的RecyclerView卡视图 [英] RecyclerView cardview from dialogfragment in fragment page

查看:46
本文介绍了片段页面中对话框片段的RecyclerView卡视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据在DiagloFragment中写入的数据来创建recyclerview cardview片段

I need to create recyclerview cardview in fragment from the data that is written in a DiagloFragment

你会怎么做?按下按钮"HECHO"后,如何使用列表创建回收者视图?

What would you do? How can I create a recycler view with the list once I press the button "HECHO"?

图像捕获数据

您能向我解释我该怎么办,或者甚至举个例子(由您完成或在其他地方发布)?

Can you explain to me what should I do or even post an example please (done by you or posted somewhere else)?

事先非常感谢.

DialogFragment类:

DialogFragment class:

   @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
   // View v = inflater.inflate(R.layout.fragment_captura_dialog_act, container, false);
    View v = inflater.inflate(R.layout.frag_capactividades, container, false);
    Spinner spinnerA;
    spinnerA = (Spinner)v.findViewById(R.id.spinnerConf);
    bguardar = (Button) v.findViewById(R.id.bGuaradrPaga);
    codigo = (EditText) v.findViewById(R.id.tCodData) ;
    precio = (EditText) v.findViewById(R.id.tPrecioData);
    preciounidadextra = (EditText) v.findViewById(R.id.preciouextra);
    cantidadminima = (EditText) v.findViewById(R.id.tCanMinData);
    primadominical = (EditText) v.findViewById(R.id.tPrimaData);
    final String tipojornada = spinnerA.getSelectedItem().toString();
    bguardar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Code create recyclerview
            empleado.guardaempleado(codigo.getText().toString(), precio.getText().toString(), preciounidadextra.getText().toString(), cantidadminima.getText().toString()
                    , primadominical.getText().toString(), tipojornada);
        }
    });

    return v;
}

}

卡片布局:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/txtcodigoMostrar"
        android:layout_width="1dp"
        android:layout_height="1dp"
        android:visibility="invisible" />

    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_marginLeft="16sp"
        android:layout_marginTop="16sp"
        android:src="@drawable/ajusted" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="70dp"
            android:layout_marginTop="10dp"
            android:width="2dp"
            android:gravity="right"
            android:text="Apuntador"
            android:textSize="16sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/txtNombreMostrar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:paddingLeft="4dp"
            android:text="Nombre"
            android:textAlignment="center"
            android:textSize="16sp"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="70dp"
            android:layout_marginTop="10dp"
            android:width="2dp"
            android:gravity="right"
            android:text="Precio"
            android:textSize="16sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/txtPrecioMostrar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:paddingLeft="4dp"
            android:text="Precio"
            android:textAlignment="center"
            android:textSize="16sp"
            android:textStyle="bold" />
    </LinearLayout>




</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="bottom"
    android:orientation="horizontal">

    <Button
        android:id="@+id/btnEditar"
        style="@style/Widget.MaterialComponents.Button.UnelevatedButton"
        android:layout_width="100dp"
        android:layout_height="30dp"
        android:text="Editar"
        android:textSize="14dp"
        android:visibility="invisible" />

</LinearLayout>

</androidx.cardview.widget.CardView>

</androidx.cardview.widget.CardView>

片段类到cretae recyclerview:

Fragment class to cretae recyclerview:

      public class empleado extends Fragment implements View.OnClickListener {
        FloatingActionButton btndialog;
        private SQLiteDatabase db;
        RecyclerView idrecyclerview, recyclerView;
        static List<ActividadesModel> listCont;
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View v5 = inflater.inflate(R.layout.activity_empleado, container, false);
    
            RecyclerView recyclerView = v5.findViewById(R.id.idrecyclerviewCa);
            //recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 1));
            AdapterAct viewAdapter = new AdapterAct(getContext(), listCont);
            recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
            recyclerView.setAdapter(viewAdapter);
            return v5;
        }
    
     public empleado(String codigo,String precio,String preciounidadextra,String cantidadminima,String primadominical,String tipojornada){
    
        }
    
        public static void guardaempleado(String codigo, String precio, String preciounidadextra, String cantidadminima, String primadominical, String tipojornada){
            listCont = new ArrayList<>();
            listCont.add(new ActividadesModel("codigo", "precio", "preciounidadextra", "cantidadminima", "primadominical", "tipojornada"));
        }


 private void ShowMessage() {
        final String[] actividades = {"act1", "act2", "act3", "act4", "act5"};
        final int itemSelected = 0;
        new AlertDialog.Builder(getContext())
                .setTitle("Selecciona la actividad")
                .setSingleChoiceItems(actividades, itemSelected, new DialogInterface.OnClickListener() {
                    @Override
                    //  public void onClick(DialogInterface dialogInterface, int selectedIndex) {
                    public void onClick(DialogInterface dialog, int position) {

                        // String nombreselect = empleados[position];
                        Toast.makeText(getContext(), "Position: " + position, Toast.LENGTH_SHORT).show();
                        String nombreselect = actividades[position];

                        SharedPreferences sharedPrefs = getActivity().getSharedPreferences("pref", Context.MODE_PRIVATE);
                        SharedPreferences.Editor editor = sharedPrefs.edit();
                        editor.putString("actividad", nombreselect);
                        editor.commit();

                      //  empleado.setText(empleadotext);
                    }
                })
               // .setNeutralButton("OK", null)
                .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // continue with delete
                        FragmentManager fm = getActivity().getSupportFragmentManager();
                        DialogFragment dialogs = new CapturaDialogAct(); // creating new object
                        dialogs.show(fm, "dialog");
                    }
                })
                .show();

    }

CaptureDialog类:

CaptureDialog class:

public class CapturaDialogAct extends DialogFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}
TextView textView;
Button bguardar;
EditText codigo,precio,preciounidadextra, cantidadminima,primadominical;
Adapter rvAdapter;
RecyclerView recyclerView;
private static RecyclerView.Adapter adapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
   // View v = inflater.inflate(R.layout.fragment_captura_dialog_act, container, false);
    View v = inflater.inflate(R.layout.frag_capactividades, container, false);
    Spinner spinnerA;
    spinnerA = (Spinner)v.findViewById(R.id.spinnerConf);
    bguardar = (Button) v.findViewById(R.id.bGuaradrPaga);
    codigo = (EditText) v.findViewById(R.id.tCodData) ;
    precio = (EditText) v.findViewById(R.id.tPrecioData);
    preciounidadextra = (EditText) v.findViewById(R.id.preciouextra);
    cantidadminima = (EditText) v.findViewById(R.id.tCanMinData);
    primadominical = (EditText) v.findViewById(R.id.tPrimaData);
    final String tipojornada = spinnerA.getSelectedItem().toString();
    bguardar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Code create recyclerview
            empleado.guardaempleado(codigo.getText().toString(), precio.getText().toString(), preciounidadextra.getText().toString(), cantidadminima.getText().toString()
                    , primadominical.getText().toString(), tipojornada);
        }
    });

    return v;
}

}

型号:

    public class ActividadesModel implements Serializable {
    private String  codigo ,precio, preciounidadextra, cantidadminima, primadominical, tipojordana;
   public ActividadesModel( String precio, String preciounidadextra, String cantidadminima, String primadominical, String codigo, String tipojordana){
       this.codigo = codigo;
   }
    public String getCodigo()
   {
       return codigo;
   }
   public void setCodigo(String codigo){
       this.codigo = codigo;
   }

   public String getPrecio(){
       return precio;
   }

   public void setPrecio(){
       this.precio = precio;
   }

   public String getPreciounidadextra(){
       return preciounidadextra;
   }

   public void setPreciounidadextra(){
       this.preciounidadextra = preciounidadextra;
   }

   public String getCantidadminima(){
       return cantidadminima;
   }
   public void setCantidadminima(){
       this.cantidadminima = cantidadminima;
   }

   public String getPrimadominical(){
       return primadominical;
   }
   public void setPrimadominical(){
       this.primadominical = primadominical;
   }

   public String getTipojordana(){
       return tipojordana;
   }
   public void setTipojordana(){
       this.tipojordana = tipojordana;
   }
}

添加适配器:

public class AdapterAct extends RecyclerView.Adapter<AdapterAct.MyViewHolder> implements Filterable {

private List<ActividadesModel> actividadesModelList = new ArrayList<>();
private Context context;

private List<ActividadesModel> actividadesArrayList;

private IAxiliarActividades iAxiliarActividades;

List<ActividadesModel> contactList;


public AdapterAct(Context context, List<ActividadesModel> listCont) {
    this.context = context;
    this.contactList = contactList;
}


@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
   // return null;
    View v;
    v = LayoutInflater.from(context).inflate(R.layout.card_actividad, parent, false);
    MyViewHolder myViewHolder = new MyViewHolder(v);
    return myViewHolder;
}

public static class MyViewHolder extends RecyclerView.ViewHolder {

    TextView name;
    TextView precio;



    public MyViewHolder(View itemView) {
        super(itemView);

        name = (TextView) itemView.findViewById(R.id.txtNombreMostrar);
        precio = (TextView) itemView.findViewById(R.id.txtPrecioMostrar);

    }
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
    //Codigo para crear el listado de acitividades

}

@Override
public int getItemCount() {
    return 0;
}

@Override
public Filter getFilter() {
    return null;
}

public class myViewHolder extends RecyclerView.ViewHolder {

    TextView nombre, precio;


    public myViewHolder(@NonNull View itemView) {
        super(itemView);
    }
}

}

推荐答案

请按照以下步骤创建DialogFragment输入的接口侦听器,以将其返回到片段.

Please follow below steps to create interface listener for the the DialogFragment input to be returned to your fragment.

  • 步骤1:CapturaDialogAct内部创建一个界面 DialogFragment及其实例字段:
  • 步骤2:修改CapturaDialogAct以接受此参数 界面
  • 第3步:每当您单击 DialogFragment按钮.
  • Step 1: Create an interface inside the CapturaDialogAct DialogFragment, and an instance field of it:
  • Step 2: Modify the CapturaDialogAct to accept an argument of this interface
  • Step 3: Trigger the interface method whenever you click the DialogFragment button.
class CapturaDialogAct extends DialogFragment {

    ...
    
    // Step 1
    public interface OnSelectionListener {
        void onConfirmed(String codigo, String precio, String preciounidadextra, String cantidadminima
                    , String primadominical, String tipojornada);
    }

    private OnSelectionListener mOnSelectionListener;

    // Step 2
    
    public CapturaDialogAct(OnSelectionListener listener) {
        this.mOnSelectionListener = listener;
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       // View v = inflater.inflate(R.layout.fragment_captura_dialog_act, container, false);
        View v = inflater.inflate(R.layout.frag_capactividades, container, false);      
        //..... reset of code
        
        
        bguardar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Code create recyclerview
                empleado.guardaempleado(codigo.getText().toString(), precio.getText().toString(), preciounidadextra.getText().toString(), cantidadminima.getText().toString()
                        , primadominical.getText().toString(), tipojornada);
                        
                        
                // Step 3
                if (mOnSelectionListener != null) {
                    mOnSelectionListener.onConfirmed(codigo.getText().toString(), precio.getText().toString(), preciounidadextra.getText().toString(), cantidadminima.getText().toString()
                        , primadominical.getText().toString(), tipojornada);
                }
                

            }
        });     
    }
   
}

步骤4:,在您的代码段中,更改DialogFragment的实例化以实现接口并使用其回调处理返回的文本

Step 4: at your fragment, change the instantiation of the DialogFragment to implement the interface and handle the returned text with its callback

替换

DialogFragment dialogs = new CapturaDialogAct(); // creating new object

使用

// Step 4
DialogFragment dialogs = new CapturaDialogAct(new CapturaDialogAct.OnSelectionListener() {
    @Override
    public void onConfirmed(String codigo, String precio, String preciounidadextra, String cantidadminima
                , String primadominical, String tipojornada) {
        
        // Do whatever you want with the received text from the DialogFragment

    }); 

更新

它已经执行了所有步骤,没有标记错误,但是没有创建cardview,我将适配器添加到帖子中 在步骤4中,更改RecyclerView适配器的列表,然后更新UI.

It already performs all the steps, it does not mark an error but it does not create the cardview, I will add the adapter to the post In Step 4 change the list of the RecyclerView adapter, and update the UI.

DialogFragment dialogs = new CapturaDialogAct(new CapturaDialogAct.OnSelectionListener() {
    @Override
    public void onConfirmed(String codigo, String precio, String preciounidadextra, String cantidadminima
                , String primadominical, String tipojornada) {
        
        // Do whatever you want with the received text from the DialogFragment
        listCont = new ArrayList<>();
        listCont.add(new ActividadesModel(codigo, precio, preciounidadextra, cantidadminima, primadominical, tipojornada));
        AdapterAct viewAdapter = new AdapterAct(getContext(), listCont);
        RecyclerView recyclerView = getView().findViewById(R.id.idrecyclerviewCa);
        //recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 1));
        AdapterAct viewAdapter = new AdapterAct(getContext(), listCont);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(viewAdapter);

    }); 

修改

在将RecyclerView定义为onCreateView的局部变量时会出现错误,因此您需要选择类RecyclerView字段. 所以,改变

You get an error as you define the RecyclerView as a local variable to onCreateView, so you need to select the class RecyclerView field instead. So, the change

public class empleado extends Fragment implements View.OnClickListener {

    RecyclerView idrecyclerview, recyclerView; // this is the field class variable

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v5 = inflater.inflate(R.layout.activity_empleado, container, false);

        recyclerView = v5.findViewById(R.id.idrecyclerviewCa); // here is the change

然后,在您单击对话框隐藏时添加了dismiss()以将其隐藏. 因此,在对话框片段中,按如下所示添加dismiss()

Then, added dismiss() when you hit the dialog hide in order to hide it. So, in your dialog fragment add dismiss() as below

    bguardar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Code create recyclerview
            empleado.guardaempleado(codigo.getText().toString(), precio.getText().toString(), preciounidadextra.getText().toString(), cantidadminima.getText().toString()
                    , primadominical.getText().toString(), tipojornada);
            // Step 3
            if (mOnSelectionListener != null) {
                mOnSelectionListener.onConfirmed(codigo.getText().toString(), precio.getText().toString(), preciounidadextra.getText().toString(), cantidadminima.getText().toString()
                        , primadominical.getText().toString(), tipojornada);
            }

            dismiss(); /// <<<<< here is the change
        }
    });

还将适配器设置为片段类字段,以便在关闭在empleado片段中创建的AdapterAct mViewAdapter对话框时使用它

Also made the adapter as a fragment class field in order to use it when you dismiss the dialog so created AdapterAct mViewAdapter in empleado fragment

这是修改后的整个片段


public class empleado extends Fragment implements View.OnClickListener {

    //private static ArrayList<Object> listCont;
    FloatingActionButton btndialog;
    // public static TextView empleado;
    private SQLiteDatabase db;
    RecyclerView idrecyclerview, recyclerView;
    static List<ActividadesModel> listCont;
    private AdapterAct mViewAdapter;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v5 = inflater.inflate(R.layout.activity_empleado, container, false);

        recyclerView = v5.findViewById(R.id.idrecyclerviewCa);
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 1));

        listCont = new ArrayList<>();
        mViewAdapter = new AdapterAct(getContext(), listCont);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(mViewAdapter);
        return v5;
    }

    //public empleado(String codigo,String precio,String preciounidadextra,String cantidadminima,String primadominical,String tipojornada){
    public empleado() {
        //  listCont = new ArrayList<>();
        // listCont.add(new ActividadesModel("precio", "preciounidadextra", "cantidadminima", "primadominical", "codigo", "tipojornada"));
    }

    public static void guardaempleado(String codigo, String precio, String preciounidadextra, String cantidadminima, String primadominical, String tipojornada) {
        listCont = new ArrayList<>();
        listCont.add(new ActividadesModel(precio, preciounidadextra, cantidadminima, primadominical, codigo, tipojornada));
        //  new empleado();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        setHasOptionsMenu(true);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        //inflate menu
        inflater.inflate(R.menu.menu_main, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        //handle menu item clicks
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            //do your function here
            Toast.makeText(getActivity(), "Sincronizar", Toast.LENGTH_SHORT).show();
        }
        if (id == R.id.action_sort) {
            //do your function here
            Toast.makeText(getActivity(), "Buscar", Toast.LENGTH_SHORT).show();
        }
        if (id == R.id.action_hoy) {
            //do your function here
            Toast.makeText(getActivity(), "Hoy", Toast.LENGTH_SHORT).show();
        }
        if (id == R.id.action_anterior) {
            //do your function here
            Toast.makeText(getActivity(), "Ayer", Toast.LENGTH_SHORT).show();
        }

        return super.onOptionsItemSelected(item);
    }


    private static String PREF_NAME = "prefs";
    SharedPreferences sharedpreferences;
    public static final String mypreference = "mypref";
    private Context mContext;

    @Override
    public void onViewCreated(View v5, Bundle savedInstanceState) {
        //public void onClick(View v5) {
        FloatingActionButton btndialog = (FloatingActionButton) v5.findViewById(R.id.floatingActionButton);

        final String[] nivelItems = getResources().getStringArray(R.array.nivel);
        final int itemSelected = 0;

        try {
            btndialog.setOnClickListener(new View.OnClickListener() {
                //    JSONObject js = createJsonObjectInv();
                //    JSONArray arr = js.getJSONArray("data");
                //JSONArray arr3 = js.getJSONArray("data");
                //String[] list = new String[arr.length()];
                //String[] arr2 = arr.toString().replace("},{", " ,").split(" ");
                //@Override
                public void onClick(View v) {
                    try {
                        JSONObject js = createJsonObjectInv();
                        JSONArray arr = js.getJSONArray("data");
                        final String[] list = new String[arr.length()];
                        for (int i = 0; i <= arr.length() - 1; i++) {
                            JSONObject element = arr.getJSONObject(i);
                            String InvernaderoId = "\"invernaderoId\":\"" + element.getString("invernaderoId") + "\", ";
                            String Name = "\"name\":\"" + element.getString("name") + "\", ";
                            String Invernarder = "\"Invernarder\":\"" + element.getString("Invernarder") + "\"";

                            //list[i] = InvernaderoId + Name + Invernarder;
                            list[i] = Name.substring(8);
                        }
                        // final String[] empleados = {"Luis", "Daniel", "Juan", "Jose", "Cesar"};
                        // final String[] empleados = arr2;
                        new AlertDialog.Builder(getContext())
                                .setTitle("Selecciona el Invernadero")
                                //  .setSingleChoiceItems(empleados, itemSelected, new DialogInterface.OnClickListener() {
                                .setSingleChoiceItems(list, -1, new DialogInterface.OnClickListener() {
                                    @Override
                                    //  public void onClick(DialogInterface dialogInterface, int selectedIndex) {
                                    public void onClick(DialogInterface dialog, int position) {
                                        // String nombreselect = empleados[position];
                                        Toast.makeText(getContext(), "Position: " + position, Toast.LENGTH_SHORT).show();
                                        String empleadotext = list[position];
                                        //empleado.setText(empleadotext);

                                        // SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
                                        SharedPreferences sharedPrefs = getActivity().getSharedPreferences("pref", Context.MODE_PRIVATE);
                                        SharedPreferences.Editor editor = sharedPrefs.edit();
                                        editor.putString("inver", empleadotext);
                                        editor.commit();

                                    }
                                })
                                //.setPositiveButton("Ok", null)
                                .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which) {
                                        // continue with delete
                                        ShowMessage();
                                    }
                                })
                                .setNegativeButton("Cancel", null)
                                .show();

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void ShowMessage() {
        final String[] actividades = {"act1", "act2", "act3", "act4", "act5"};
        final int itemSelected = 0;
        new AlertDialog.Builder(getContext())
                .setTitle("Selecciona la actividad")
                .setSingleChoiceItems(actividades, itemSelected, new DialogInterface.OnClickListener() {
                    @Override
                    //  public void onClick(DialogInterface dialogInterface, int selectedIndex) {
                    public void onClick(DialogInterface dialog, int position) {

                        // String nombreselect = empleados[position];
                        Toast.makeText(getContext(), "Position: " + position, Toast.LENGTH_SHORT).show();
                        String nombreselect = actividades[position];
                        SharedPreferences sharedPrefs = getActivity().getSharedPreferences("pref", Context.MODE_PRIVATE);
                        SharedPreferences.Editor editor = sharedPrefs.edit();
                        editor.putString("actividad", nombreselect);
                        editor.commit();

                        //  empleado.setText(empleadotext);
                    }
                })
                // .setNeutralButton("OK", null)
                .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // continue with delete
                        /*   FragmentManager fm = getActivity().getSupportFragmentManager();
                        DialogFragment dialogs = new CapturaDialogAct(); // creating new object
                        dialogs.show(fm, "dialog");

                         */
                        FragmentManager fm = getActivity().getSupportFragmentManager();
                    /*    DialogFragment dialogs = new CapturaDialogAct(new CapturaDialogAct.OnSelectionListener() {
                            @Override
                            public void onConfirmed(String codigo, String precio, String preciounidadextra, String cantidadminima
                                    , String primadominical, String tipojornada) {

                                // Do whatever you want with the received text from the DialogFragment


                            }
                        });*/
                        DialogFragment dialogs = new CapturaDialogAct(new CapturaDialogAct.OnSelectionListener() {
                            @Override
                            public void onConfirmed(String codigo, String precio, String preciounidadextra, String cantidadminima
                                    , String primadominical, String tipojornada) {

                                // Do whatever you want with the received text from the DialogFragment
                            /*
                                AdapterAct viewAdapter = new AdapterAct(getContext(), listCont);
                                recyclerView.setAdapter(viewAdapter);

                             */
//                                RecyclerView recyclerView = getView().findViewById(R.id.idrecyclerviewCa);
                                //recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 1));
                                mViewAdapter.addItem(new ActividadesModel(codigo, precio, preciounidadextra, cantidadminima, primadominical, tipojornada));
                            }
                        });
                        dialogs.show(fm, "dialog");

                    }
                })
                .show();
    }


    public JSONObject createJsonObjectInv() throws JSONException {
        Cursor cursor = getAllDataInv();
        JSONObject jobj;
        JSONArray arr = new JSONArray();
        cursor.moveToFirst();
        while (cursor.moveToNext()) {
            jobj = new JSONObject();
            jobj.put("invernaderoId", cursor.getString(0));
            jobj.put("name", cursor.getString(1));
            jobj.put("Invernarder", cursor.getString(4));
            arr.put(jobj);
        }
        jobj = new JSONObject();
        jobj.put("data", arr);
        return jobj;
    }


    //Syncronizador de datos a servicio
    public Cursor getAllDataInv() {
        String selectQuery = "Select * from Invernadero";
        SQLiteDatabase db = new MyHelper(getActivity()).getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        //Cursor cursor = db.rawQuery("select * from capturas where syncstatus= ?", new String[] {"0"});
        return cursor;
    }


    @Override
    public void onClick(View v) {

    }
}

为您的适配器添加了一个名为addItem的新方法,该方法接受适配器中的新行并通知最后一项更改.

And for your adapter, added a new method called addItem that accepts a new row in the adapter and notifiy change in last item.


public class AdapterAct extends RecyclerView.Adapter<AdapterAct.MyViewHolder> implements Filterable {

    private List<ActividadesModel> actividadesModelList;
    private Context context;
    private List<ActividadesModel> actividadesArrayList;
    private IAxiliarActividades iAxiliarActividades;
    List<ActividadesModel> contactList;
    // Este es nuestro constructor (puede variar según lo que queremos mostrar)
    private String[] mDataSet;
    private List<ActividadesModel> listCont;

    public AdapterAct(Context context, List<ActividadesModel> listCont) {
        this.context = context;
        this.contactList = listCont;
        this.listCont = listCont;
    }


    public AdapterAct(String[] myDataSet) {
        mDataSet = myDataSet;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // return null;
        View v;
        v = LayoutInflater.from(context).inflate(R.layout.card_actividad, parent, false);
        return new MyViewHolder(v);
    }

    public void addItem(ActividadesModel item) {
        this.listCont.add(item);
        notifyItemChanged(listCont.size() - 1);
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {
        TextView name;
        TextView precio;

        public MyViewHolder(View itemView) {
            super(itemView);
            this.name = (TextView) itemView.findViewById(R.id.txtNombreMostrar);
            this.precio = (TextView) itemView.findViewById(R.id.txtPrecioMostrar);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

        ActividadesModel actividadesModel = listCont.get(position);
        //Codigo para crear el listado de acitividades
//        holder.name.setText("nameprueba");
        holder.precio.setText(actividadesModel.getCodigo());
        holder.precio.setText(actividadesModel.getPrecio());
//        holder.precio.setText("precio23");

    }

    @Override
    public int getItemCount() {
        return listCont == null ? 0 : listCont.size();
    }

    @Override
    public Filter getFilter() {
        return null;
    }


}

这篇关于片段页面中对话框片段的RecyclerView卡视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆