将自定义对象添加到自定义对象内的列表字段中

时间:2016-03-10 10:24:52

标签: java android list arraylist

我正在尝试将对象自定义为列表,这是另一个自定义对象中的字段。

让我解释一下调查问题。我有一个客户(公司)的arraylist这些客户有联系人(在客户工作的联系人)。出于这个原因,我试图将这些联系人添加到客户端对象内的arraylistfield。但我不知道如何将这些添加到所选客户端的arraylist或如何通过初始化调用clientsobjectclass中的方法,因为构造函数会询问所有客户端字段。

客户对象:

package Activities.Main.Objects;

import android.util.Log;
import Main.Arrays.ClientsArray;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Oscar on 19-02-16.
 */
public class Clients {
    private String Name;
    private String Phone;
    private String Email;
    private String URL;
    private Double Turnover;
    private String VisitAddress;
    private String VisitCity;
    private String VisitZipcode;
    private String VisitCountry;
    private String PostalAddress;
    private String PostalCity;
    private String PostalZipcode;
    private String PostalCountry;
    private List<Contacts> Contactlist =  new ArrayList<Contacts>();


    public Clients(String Name, String Phone, String Email, String URL, Double Turnover,
                   String VisitAddress, String VisitCity, String VisitZipcode, String VisitCountry,
                   String PostalAddress, String PostalCity, String PostalZipcode, String PostalCountry){
            super();
            this.Name = Name;
            this.Phone = Phone;
            this.Email = Email;
            this.URL = URL;
            this.Turnover = Turnover;
            this.VisitAddress = VisitAddress;
            this.VisitCity = VisitCity;
            this.VisitZipcode = VisitZipcode;
            this.VisitCountry = VisitCountry;
            this.PostalAddress = PostalAddress;
            this.PostalCity = PostalCity;
            this.PostalZipcode = PostalZipcode;
            this.PostalCountry = PostalCountry;
    }


    public void add(){
        Contactlist.add(new Contacts("Oscar", "Hugo", "Bokhorst", "Developer", "0616397853", "0513631325", "oscarbokhorst@gmail.com"));
        int sizeCheck = Contactlist.size();
        Log.d("Hello", "=f=f=f=f=f=f=f=f=f=f=f=f=f=f=f=f=f=f=f=f=f=f=f=f=f=f=f=f=f=f=f=f=f=f=f=f=f=f=f=f=f=f=f=f = contactlist size = " + sizeCheck);

    }


    public String getName() {
        return Name;
    }

    public String getPhone() {
        return Phone;
    }

    public String getEmail() {
        return Email;
    }

    public String getURL() {
        return URL;
    }

    public Double getTurnover() {

        return Turnover;
    }

    public String getVisitAddress() {
        return VisitAddress;
    }

    public String getVisitCity() {
        return VisitCity;
    }

    public String getVisitZipcode() {
        return VisitZipcode;
    }

    public String getVisitCountry() {
        return VisitCountry;
    }

    public String getPostalAddress() {
        return PostalAddress;
    }

    public String getPostalCity() {
        return PostalCity;
    }

    public String getPostalZipcode() {
        return PostalZipcode;
    }

    public String getPostalCountry() {
        return PostalCountry;
    }

    public List<Contacts> getContactlist(){
        return Contactlist;
    }
}

我想将联系人添加到客户端(atm仍然是一个单独的数组,但它们需要链接到客户端)

package Main.Fragments;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.hunteronlinebeta.app.R;
import Main.Arrays.ClientsArray;
import Main.Arrays.ContactsArray;
import Main.Objects.Clients;
import Main.Objects.Contacts;

public class AddContactsFragment extends Fragment implements OnClickListener {

    ContactsArray conray = new ContactsArray();
    ClientsArray cray = new ClientsArray();
    private int clientLocation;
    FragmentManager manager;
    FragmentTransaction transaction;
    Button submitContactsBtn;
    Button clearContactsFieldsBtn;

    public AddContactsFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        manager = getFragmentManager();
        transaction = manager.beginTransaction();

    }

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

        submitContactsBtn = (Button) rootView.findViewById(R.id.btn_submit_contact);
        submitContactsBtn.setOnClickListener(this);

        clearContactsFieldsBtn = (Button) rootView.findViewById(R.id.clearContactsFieldsBtn);
        clearContactsFieldsBtn.setOnClickListener(this);

        return rootView;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.clearContactsFieldsBtn:
                AddContactsFragment acf = new AddContactsFragment();
                transaction.addToBackStack(null);
                transaction.replace(R.id.group, acf, "addconfrag");
                transaction.commit();
                break;

            case R.id.btn_submit_contact:

                Clients currentClient = cray.ClientsArray.get(clientLocation);

                EditText editFirstName = (EditText) getActivity().findViewById(R.id.view_contact_firstname);
                final String etFirstName = editFirstName.getText().toString();

                EditText editMiddleName = (EditText) getActivity().findViewById(R.id.view_contact_middleName);
                final String etMiddleName = editMiddleName.getText().toString();

                EditText editSurname = (EditText) getActivity().findViewById(R.id.view_contact_surname);
                final String etSurname = editSurname.getText().toString();


                EditText editPhone = (EditText) getActivity().findViewById(R.id.view_contact_phone);
                final String etPhone = editPhone.getText().toString();

                EditText editMobile = (EditText) getActivity().findViewById(R.id.view_contact_mobile);
                final String etMobile = editMobile.getText().toString();

                EditText editEmail = (EditText) getActivity().findViewById(R.id.view_contact_email);
                final String etEmail = editEmail.getText().toString();


                EditText editPosition = (EditText) getActivity().findViewById(R.id.view_contact_position);
                final String etPosition = editPosition.getText().toString();


                if (etFirstName.trim().equals("") |
                        etMiddleName.trim().equals("") |
                        etSurname.trim().equals("") |
                        etMobile.trim().equals("") |
                        etPhone.trim().equals("") |
                        etEmail.trim().equals("") |
                        etPosition.trim().equals("")) {

                    Toast.makeText(this.getActivity(), "Plz fill in the fields", Toast.LENGTH_LONG).show();

                } else {
                    Contacts contact = new Contacts(etFirstName, etMiddleName, etSurname, etPosition, etPhone, etMobile, etEmail);


                    conray.ContactsArray.add(contact);


                    ViewClientsFragment vcf = new ViewClientsFragment();
                    transaction.replace(R.id.group, vcf, "viewcfrag");
                    transaction.commit();
                }
                break;
        }
    }

    public void setLocation(int location) {
        this.clientLocation = location;

        return;
    }
}

我通过联系人列表查看我的客户端的位置,我在其中设置客户端位置并将其提供给AddContactsFragment。

package Main.Fragments;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import android.view.View.OnClickListener;
import android.view.View.MeasureSpec;
import app.R;
import Main.Adapters.ContactsAdapter;
import Main.Arrays.ClientsArray;
import Main.Arrays.ContactsArray;
import Main.Objects.Clients;

public class ViewClientsFragment extends ListFragment implements OnClickListener {

    ClientsArray cray = new ClientsArray();
    ContactsArray conray = new ContactsArray();
    AddContactsFragment adconfrag = new AddContactsFragment();
    FragmentTransaction transaction;
    FragmentManager manager;
    private int clientlocation;
    private int contactlocation;
    Button editClientsButton;
    Button addContactsButton;



    public ViewClientsFragment(){
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        manager = getFragmentManager();
        View rootView = inflater.inflate(R.layout.fragment_view_clients,
                container, false);

        transaction = manager.beginTransaction();

        editClientsButton = (Button) rootView.findViewById(R.id.btn_edit_client);
        editClientsButton.setOnClickListener(this);

        addContactsButton = (Button) rootView.findViewById(R.id.btn_add_contact);
        addContactsButton.setOnClickListener(this);


        ListView clientList = (ListView) rootView.findViewById(android.R.id.list);

        //--------------------------------------------------------------------------------------------------------------
        int x = conray.ContactsArray.size();
        String checkSize = String.valueOf(x);
        Log.d("hello", "-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x- has the list bin populated:" + checkSize);
        //--------------------------------------------------------------------------------------------------------------

        setListViewHeightBasedOnChildren(clientList);

        Clients currentClient = cray.ClientsArray.get(clientlocation);

        Log.d("hello", "0000000000000000000000000000000000000000000000 clientlocation = " + clientlocation);

        TextView viewName = (TextView) rootView.findViewById(R.id.view_company_name);
        viewName.setText(currentClient.getName());

        TextView viewPhone = (TextView)rootView.findViewById(R.id.view_phone);
        viewPhone.setText(currentClient.getPhone());

        TextView viewEmail = (TextView)rootView.findViewById(R.id.view_Email);
        viewEmail.setText(currentClient.getEmail());

        TextView viewURL = (TextView)rootView.findViewById(R.id.view_url);
        viewURL.setText(currentClient.getURL());

        TextView viewTurnover = (TextView)rootView.findViewById(R.id.view_turnover);
        viewTurnover.setText(String.valueOf(currentClient.getTurnover()));



        TextView viewVisitAddress = (TextView)rootView.findViewById(R.id.view_visit_address);
        viewVisitAddress.setText(currentClient.getVisitAddress());

        TextView viewVisitCity = (TextView)rootView.findViewById(R.id.view_visit_city);
        viewVisitCity.setText(currentClient.getVisitCity());

        TextView viewVisitZipcode = (TextView)rootView.findViewById(R.id.view_visit_zipcode);
        viewVisitZipcode.setText(currentClient.getVisitZipcode());

        TextView viewVisitCountry = (TextView)rootView.findViewById(R.id.view_visit_country);
        viewVisitCountry.setText(currentClient.getVisitCountry());

        TextView viewPostalAddress = (TextView)rootView.findViewById(R.id.view_postal_address);
        viewPostalAddress.setText(currentClient.getPostalAddress());

        TextView viewPostalCity = (TextView)rootView.findViewById(R.id.view_postal_city);
        viewPostalCity.setText(currentClient.getPostalCity());

        TextView viewPostalZipcode = (TextView)rootView.findViewById(R.id.view_postal_zipcode);
        viewPostalZipcode.setText(currentClient.getPostalZipcode());

        TextView viewPostalCountry = (TextView)rootView.findViewById(R.id.view_postal_country);
        viewPostalCountry.setText(currentClient.getPostalCountry());

        return rootView;
    }

    public void setLocation(int location) {
        this.clientlocation = location;
    }

    public void setListViewHeightBasedOnChildren(ListView listView) {
        ArrayAdapter adapter = new ContactsAdapter(this.getActivity(), conray.ContactsArray);
        if (adapter == null)
            return;

        int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED);
        int totalHeight = 0;
        View view = null;
        for (int i = 0; i < adapter.getCount(); i++) {
            view = adapter.getView(i, view, listView);
            if (i == 0)
                view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, ViewGroup.LayoutParams.WRAP_CONTENT));

            view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
            totalHeight += view.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (conray.ContactsArray.size() - 1));
        listView.setLayoutParams(params);
        listView.setAdapter(adapter);
    }
    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        ViewContactsFragment vconf = new ViewContactsFragment();
        transaction.addToBackStack(null);
        transaction.replace(R.id.group, vconf, "vconfrag");
        contactlocation = position;
        vconf.setLocation(contactlocation);
        transaction.commit();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_edit_client:
                Toast.makeText(this.getActivity(), "Pressed the edit Client button", Toast.LENGTH_LONG).show();
                break;
            case R.id.btn_add_contact:
                AddContactsFragment acf = new AddContactsFragment();
                transaction.addToBackStack(null);
                transaction.replace(R.id.group, acf, "aconfrag");
                acf.setLocation(clientlocation);
                transaction.commit();
                break;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

使用构造函数也分配ArrayList<Contacts>。在你片段或活动中创建一个新列表 -

List<Contacts> Contactlist =  new ArrayList<Contacts>();

然后将此所有联系人添加到此特定客户的此列表中。

然后,当您使用其余详细信息创建Client类对象时,只需将其传递给构造函数 -

Client c = new Client(name, phone, ......... , Contactlist);

在Client类中修改构造函数以接受列表 -

public Clients(String name, String phone, .......... , List Contactlist){
    this.Contactlist = Contactlist; //List assigned
}

如果您想在此列表中添加更多联系人,只需通过getter方法访问列表并添加更多联系人 -

client.getContactList().add(contact);