Fill the contents of an array between classes

0

I'm doing an application on android, and I need an activity from an array created in another class since from a second activity I'll get the created data, however I can not add data to the array from the first activity.

//CLASE QUE MANDA INSERTA EL ARRAY
public class ProductoDetailFragment extends Fragment {


Array array = new Array();

String K = "";

private static ProductoDetailFragment myself;

public ProductoDetailFragment() {
    myself = this;
}

public static ProductoDetailFragment getInstance() {
    return myself;
}

public static final String ARG_ITEM_ID = "item_id";

ArrayList<String> orray = new ArrayList<String>();

private DummyContent.DummyItem mItem;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (getArguments().containsKey(ARG_ITEM_ID)) {
        mItem = DummyContent.ITEM_MAP.get(getArguments().getString(ARG_ITEM_ID));
        Activity activity = this.getActivity();
        CollapsingToolbarLayout appBarLayout = (CollapsingToolbarLayout) activity.findViewById(R.id.toolbar_layout);
        if (appBarLayout != null) {
            appBarLayout.setTitle(mItem.Nombre);
        }

    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.producto_detail, container, false);
    if (mItem != null) {
        ((TextView) rootView.findViewById(R.id.txtNombre)).setText(mItem.Nombre + "\n");
        ((TextView) rootView.findViewById(R.id.txtClave)).setText(mItem.Clave + "\n");
        ((TextView) rootView.findViewById(R.id.txtDescripcion)).setText(mItem.Descripcion + "\n");
        K = mItem.Nombre;
        Log.v(TAG, "eee" + K);

    }

    return rootView;
}

public void grr(){
    array.insertar(K);
    Log.v(TAG, array.getLista().toString());
}

public ArrayList<String> listarArray(){
    return array.getLista();
}

}

It should be noted that in the OnCreate method I fill the variable k so that I do not get an error, I already check it with a Log, if there is a return value. Next the class that contains the array

//CLASE QUE CONTIENE EL ARRAY
public class Array {
private  ArrayList<String> array = new ArrayList<>();


public void insertar(String valor){
    array.add(valor);
    Log.v(TAG, valor);
}


public ArrayList<String> getLista(){
    return array;
}

}

Because I have this error I have not called the array to the second activity so I will not put it

    
asked by Juan Trinidad Mayo 06.12.2017 в 21:57
source

1 answer

1

Your Activity1

     Array array = new Array();


//aqui agregas tu lista de elementos que pertenece al objeto array
     public void grr(){
        Log.v(TAG, k);

        array.insertar(k);
     }


//con este metodo puedes llamar a los elementos del objeto array
    public ArrayList<String> listarArray(){
        return array.getLista();
    }


Tu clase array


//CLASE QUE CONTIENE EL ARRAY
public class Array {
private  ArrayList<String> array = new ArrayList<>();
ProductoDetailFragment Item=new ProductoDetailFragment();


public void insertar(String valor){
    array.add(valor);
    Log.v(TAG, valor);
}


public ArrayList<String> getLista(){
    return array;
}

}
    
answered by 06.12.2017 / 22:39
source