简体   繁体   中英

How to add elements to a list from another class?

So i have my a list on my main activity that i want to be able to access in another class. The second class is supposed to add more records to the list based on the users input but i don't know how to access it. Can anybody help?

Contactos.java: This is my main class and its also where the list is kept, i did a test run on it so that's why the constructor is filled with numbers. I want to be able to add to the list from another class.

public class Contactos extends AppCompatActivity {

    private Button btnReciente;

    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnReciente = (Button) findViewById(R.id.Reciente);
        listView = (ListView) findViewById(R.id.list1);

        List<contactosLista> list1 = new ArrayList<contactosLista>();
        list1.add(new contactosLista("1","2","3","4","5","6"));

        ContactosAdapter adapter = new ContactosAdapter(this,list1);
        listView.setAdapter(adapter);

ContactosAdapter.java: This is where i inflate the list, i use it so i can use a.xml file to better display the values in the list.

public class ContactosAdapter extends BaseAdapter
{
    private Context mContext;
    private List<contactosLista>mListaContactos;

    public ContactosAdapter(Context context, List<contactosLista> list)
    {
        mContext = context;
        mListaContactos = list;
    }

    @Override
    public int getCount() {
        return mListaContactos.size();
    }

    @Override
    public Object getItem(int position) {
        return mListaContactos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        contactosLista entrada = mListaContactos.get(position);

        if(convertView == null)
        {
            LayoutInflater inflater = LayoutInflater.from(mContext);
            convertView = inflater.inflate(R.layout.contactos_row,null);
        }

        TextView Contact = (TextView) convertView.findViewById(R.id.Contacto);
        Contact.setText(entrada.getmName() + " -- " + entrada.getmEmpresa());

        return convertView;
    }
}

contactosLista.java: This the class that defines the elements that the list needs. i made a constructor that accommodates all the strings and made all the setters & getters

public class contactosLista
{
    private String mName;
    private String mEmpresa;
    private String mRazon;
    private String mDireccion;
    private String mEstatus;
    private String mPaquete;

    public contactosLista(String mName, String mEmpresa, String mRazon, String mDireccion, String mEstatus, String mPaquete)
    {
        this.mName = mName;
        this.mEmpresa = mEmpresa;
        this.mRazon = mRazon;
        this.mDireccion = mDireccion;
        this.mEstatus = mEstatus;
        this.mPaquete = mPaquete;
    }

    public String getmName() {
        return mName;
    }

    public void setmName(String mName) {
        this.mName = mName;
    }

    public String getmEmpresa() {
        return mEmpresa;
    }

    public void setmEmpresa(String mEmpresa) {
        this.mEmpresa = mEmpresa;
    }

    public String getmRazon() {
        return mRazon;
    }

    public void setmRazon(String mRazon) {
        this.mRazon = mRazon;
    }

    public String getmDireccion() {
        return mDireccion;
    }

    public void setmDireccion(String mDireccion) {
        this.mDireccion = mDireccion;
    }

    public String getmEstatus() {
        return mEstatus;
    }

    public void setmEstatus(String mEstatus) {
        this.mEstatus = mEstatus;
    }

    public String getmPaquete() {
        return mPaquete;
    }

    public void setmPaquete(String mPaquete) {
        this.mPaquete = mPaquete;
}
}

createContact.java: finally this is the class where i want to be able to access the list from Contactos.java, I have a layout file that has a bunch of Edit text so that i can record input from the user. When the user clicks on the button to save i want my void "GuardarCon" to save the input from the user to strings and then i want to use those strings as parameters for my list. This is where the problem arises, i don't know how to call the list. PLS help.

public class createContact extends AppCompatActivity
{
    EditText nombre;
    EditText empresa;
    EditText razon;
    EditText direccion;
    EditText estatus;
    EditText paquete;
    String snombre;
    String sempresa;
    String srazon;
    String sdireccion;
    String sestatus;
    String spaquete;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_contact);

        nombre = (EditText) findViewById(R.id.nombreT);
        empresa = (EditText) findViewById(R.id.empresaT);
        razon = (EditText) findViewById(R.id.razonSocialT);
        direccion = (EditText) findViewById(R.id.direccionT);
        estatus = (EditText) findViewById(R.id.EstatusT);
        paquete = (EditText) findViewById(R.id.paqueteT);
    }

    public void GuardarCon(View view)
    {
        snombre = nombre.getText().toString();
        sempresa = empresa.getText().toString();
        srazon = razon.getText().toString();
        sdireccion = direccion.getText().toString();
        sestatus = estatus.getText().toString();
        spaquete = paquete.getText().toString();

    }

Create a singleton class and access and modify data as you want !

public class DataHolder{
    public static final DataHolder instance = new DataHolder();
private List<Your_Data_Type> data;

//Your methods goes here...
}

You could use startActivityForResult method to start createContact from Contactos activity and after the addition process is done use setResult method to return the added item to the Contactos activity and handle adding the item to the list in onActivityResult .
take a look at this link for further info:

https://developer.android.com/training/basics/intents/result

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM