getExtra与Intent的接口

时间:2018-04-15 21:29:52

标签: java android android-studio

我想移动名为DisplayItem

的接口数组

这是activity1代码:

   package com.example.eranp.clientpage;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class Main2Activity extends Activity implements Serializable{


    private EditText pro_device_det;
    private Button saveDataBase, btnViewAll ;
    private DatabaseReference databaseCustomer, databaseDevice, databaseProblem, db;
    private Spinner spinner;
    private String option1, option2,option3;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main3);

        db = FirebaseDatabase.getInstance().getReference();
        databaseCustomer = FirebaseDatabase.getInstance().getReference("customer");
        databaseDevice = FirebaseDatabase.getInstance().getReference("device");
        databaseProblem = FirebaseDatabase.getInstance().getReference("problem");
        option1 = getResources().getString(R.string.option1u);
        option2 = getResources().getString(R.string.option2u);
        option3 = getResources().getString(R.string.option3u);
        saveDataBase = (Button) findViewById(R.id.save_btn);
        btnViewAll = (Button)findViewById(R.id.button_viewAll);
        final List<String> list = new ArrayList<String>();
        list.add(option1);
        list.add(option2);
        list.add(option3);
        final ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, list);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(dataAdapter);


        pro_device_det = (EditText) findViewById(R.id.pro_device_det);
        final Intent intent = getIntent();
        final String fName = intent.getStringExtra("fName");
        final String lName = intent.getStringExtra("lName");
        final String phone = intent.getStringExtra("phone");
        final String email = intent.getStringExtra("email");
        final String modelDevice = intent.getStringExtra("modelDevice");
        final String nameDevice = intent.getStringExtra("nameDevice");

        saveDataBase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String proDeviceDet = pro_device_det.getText().toString().trim();
                String shortProDeviceDet = string3Words(proDeviceDet);
                DateFormat df = new SimpleDateFormat("d MMM yyyy, HH:mm");
                String date = df.format(Calendar.getInstance().getTime());
                if (!TextUtils.isEmpty(proDeviceDet)) {
                    String id = databaseCustomer.push().getKey();
                    Customer customer = new Customer(id, fName, lName, email, phone);
                    databaseCustomer.child(id).setValue(customer);


                    Device device = new Device(id, nameDevice, modelDevice);
                    databaseDevice.child(id).setValue(device);
                    int urgency = spinnerUrgency(spinner);
                    Problem problem = new Problem(id, date, proDeviceDet, urgency, shortProDeviceDet);
                    databaseProblem.child(id).setValue(problem);
                    DisplayItem[] displayItem= new DisplayItem[]{customer, problem};
                    final int REQ_CODE = 1;
                    Intent intent2 = new Intent(Main2Activity.this, CustomersPage.class);
                    intent2.putExtra("DisplayItem",displayItem);
                    startActivity(intent2);

                      Toast.makeText(Main2Activity.this , "Customer added", Toast.LENGTH_LONG).show();

                } else {
                    Toast.makeText(Main2Activity.this, "Please write on an empty cell", Toast.LENGTH_LONG).show();
                }
            }
        });
}
        });


    }

}

接口代码:

package com.example.eranp.clientpage;

import java.io.Serializable;

/**
 * Created by Eran P on 15/04/2018.
 */

public interface DisplayItem extends Serializable {

}

这是我要移动界面的第二个活动代码:

    package com.example.eranp.clientpage;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.concurrent.TimeUnit;


public class CustomersPage extends Activity {

    ListView listViewCustomers;
    List<Customer> customers;
    List<Problem> problems;
    List<Device> devices;
    DatabaseReference databaseCustomers, databaseProblem, databaseDevice,db;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_customers_page);
        db = FirebaseDatabase.getInstance().getReference();
        databaseCustomers = FirebaseDatabase.getInstance().getReference("customer");
        databaseProblem = FirebaseDatabase.getInstance().getReference("problem");
        databaseDevice = FirebaseDatabase.getInstance().getReference("device");
        listViewCustomers = (ListView)findViewById(R.id.listViewCostumers);
        customers = new ArrayList<>();
        devices = new ArrayList<>();
        problems = new ArrayList<>();

        Intent intentl = getIntent();


        listViewCustomers.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                Customer customer = customers.get(i);
                showUpdateDeleteDialog(customer.getCustomerId(), customer.getfName() + " " + customer.getlName());
                return true;
            }
        });

        listViewCustomers.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Customer customer = customers.get(i);
                Problem problem = problems.get(i);
                Device device = devices.get(i);
                Intent intent = new Intent(CustomersPage.this, ClientPageData.class);
                intent.putExtra("fName", customer.getfName());
                intent.putExtra("lName", customer.getlName());
                intent.putExtra("phone", customer.getPhoneNum());
                intent.putExtra("email", customer.geteMail());
               intent.putExtra("problemShort", problem.getProDeviceDet());
               intent.putExtra("nameDevice", device.getDeviceName());
               intent.putExtra("modelDevice", device.getDeviceModel());
                startActivity(intent);

            }
        });




    }


    private void showUpdateDeleteDialog(final String customerId, String customerName) {

        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
        LayoutInflater inflater = getLayoutInflater();
        final View dialogView = inflater.inflate(R.layout.update_dialog, null);
        dialogBuilder.setView(dialogView);
        final EditText editTextFName = (EditText) dialogView.findViewById(R.id.ET_fName);
        final EditText editTextLName = (EditText) dialogView.findViewById(R.id.ET_lName);
        final EditText editTextPhone = (EditText) dialogView.findViewById(R.id.ET_phone);
        final EditText editTextEmail = (EditText) dialogView.findViewById(R.id.ET_email);
        final EditText editTextProDetDev = (EditText) dialogView.findViewById(R.id.ET_proDetDevice);


        final Button buttonUpdate = (Button) dialogView.findViewById(R.id.buttonUpdateArtist);
        final Button buttonDelete = (Button) dialogView.findViewById(R.id.buttonDeleteArtist);

        dialogBuilder.setTitle(customerName);
        final AlertDialog b = dialogBuilder.create();
        b.show();


        buttonUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String fName = editTextFName.getText().toString().trim();
                String lName = editTextLName.getText().toString().trim();
                String phone = editTextPhone.getText().toString().trim();
                String email = editTextEmail.getText().toString().trim();
                String proDetDevice = editTextProDetDev.getText().toString().trim();
                if (!TextUtils.isEmpty(fName)) {
                    updateCustomer(customerId, fName, lName,phone,email, proDetDevice);
                    b.dismiss();
                }
            }
        });


        buttonDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                deleteCustomer(customerId);

            }
        });
    }
    private boolean deleteCustomer(String id) {
        //getting the specified Customer reference
        DatabaseReference dR = FirebaseDatabase.getInstance().getReference("customer").child(id);
        DatabaseReference dRP = FirebaseDatabase.getInstance().getReference("device").child(id);
        DatabaseReference dRD = FirebaseDatabase.getInstance().getReference("problem").child(id);

        //removing Customer
        dR.removeValue();
        dRP.removeValue();
        dRD.removeValue();

        Toast.makeText(getApplicationContext(), "Customer Deleted", Toast.LENGTH_LONG).show();

        return true;
    }

    private boolean updateCustomer(String id, String fName, String lName, String phone, String email, String proDetDevice) {
        //getting the specified artist reference
        DatabaseReference dR = FirebaseDatabase.getInstance().getReference("customer").child(id);
        DatabaseReference dRp = FirebaseDatabase.getInstance().getReference("problem").child(id);

        //updating artist
        Customer customer= new Customer(id, fName, lName, email,phone);
        dR.setValue(customer);
        DateFormat df = new SimpleDateFormat("d MMM yyyy, HH:mm");
        String date = df.format(Calendar.getInstance().getTime());
        String shortProDeviceDet = string3Words(proDetDevice);
        Problem problem= new Problem(id, date,proDetDevice,0, shortProDeviceDet );
        dRp.setValue(problem);
        Toast.makeText(getApplicationContext(), "Customer Updated", Toast.LENGTH_LONG).show();
        return true;
    }

        //attaching value event listener
        databaseCustomers.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                //clearing the previous artist list
                customers.clear();

                //iterating through all the nodes
                for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                    //getting customer
                    Customer customer = postSnapshot.getValue(Customer.class);
                    //adding customer to the list
                    customers.add(customer);

                }
                Intent intent2 = getIntent();
               DisplayItem [] displayItem = (DisplayItem[]) intent2.getExtras().getSerializable("DisplayItem");

                //creating adapter
                CustomerAdapter customerAdapter = new CustomerAdapter(CustomersPage.this,displayItem );
                //attaching adapter to the listview
                listViewCustomers.setAdapter(customerAdapter);

            }

}
  

当我试图运行应用程序时崩溃并在日志文件中他说错误在这一行:

  DisplayItem [] displayItem = (DisplayItem[])
 intent2.getExtras().getSerializable("DisplayItem");

非常感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

您的代码不太清楚,但请尝试使用以下格式:

//When you want to pass an array as intent:
private ArrayList<String> arrayList = new ArrayList<String>();
Intent intent = new Intent();
intent.putStringArrayListExtra("data", arrayList);
startActivity(intent);

// To receive intent in the second activity
private ArrayList<String> newArrayList = new ArrayList<String>();
newArrayList = getIntent.getStringArrayListExtra("data");

答案 1 :(得分:0)

检查您的类Device和Customer是否实现了可序列化