Serializable不会在意图中发送putExtra

时间:2017-01-13 08:20:22

标签: android android-intent

为什么我的意图中的putExtra无效,没有在意图中传递值。所有值都出现在列表视图中,但是当我点击列表视图中的某个项目时,当我要去DetailActivity时,现在没有值。提前感谢您的回答。

主要活动

    @Override
public void processFinish(String s) {
    Log.d(TAG,s);

    carList = new JsonConverter<Cars>().toArrayList(s, Cars.class);

    BindDictionary<Cars> dict = new BindDictionary<Cars>();
    dict.addDynamicImageField(R.id.ivImg1, new StringExtractor<Cars>() {
        @Override
        public String getStringValue(Cars product, int position) {
            return product.Image;
        }
    }, new DynamicImageLoader() {
        @Override

        public void loadImage(String url, ImageView imageView) {

            ImageLoader.getInstance().displayImage(url, imageView); // Default options will be used

        }
    });
    dict.addStringField(R.id.tvCarModel, new StringExtractor<Cars>() {
        @Override
        public String getStringValue(Cars product, int position) {
            return "Car Model: " + product.Car_Model;
        }
    });
    dict.addStringField(R.id.carID, new StringExtractor<Cars>() {
        @Override
        public String getStringValue(Cars product, int position) {
            return ""+product.Car_No;
        }
    });
    dict.addStringField(R.id.tvCarType, new StringExtractor<Cars>() {
        @Override
        public String getStringValue(Cars product, int position) {
            return "Car Type: " + product.Car_Type;
        }
    });
    dict.addStringField(R.id.tvCapacity, new StringExtractor<Cars>() {
        @Override
        public String getStringValue(Cars product, int position) {
            return "Capacity: " + product.Capacity;
        }
    });
    dict.addStringField(R.id.tvFuelType, new StringExtractor<Cars>() {
        @Override
        public String getStringValue(Cars product, int position) {
            return "Fuel Type: " + product.FuelType;
        }
    });
    dict.addStringField(R.id.tvPlateNumber, new StringExtractor<Cars>() {
        @Override
        public String getStringValue(Cars product, int position) {
            return "Plate Number: " + product.PlateNumber;
        }
    });
    dict.addStringField(R.id.tvPoster, new StringExtractor<Cars>() {
        @Override
        public String getStringValue(Cars product, int position) {
            return "Posted by: " + product.owner;
        }
    });
    dict.addStringField(R.id.tvPrice, new StringExtractor<Cars>() {
        @Override
        public String getStringValue(Cars product, int position) {
            return "Price: " + product.carPrice;
        }
    });
    dict.addStringField(R.id.tvFrom1, new StringExtractor<Cars>() {
        @Override
        public String getStringValue(Cars product, int position) {
            return "Range: " + product.rangeFrom+" ";
        }
    });
    dict.addStringField(R.id.tvTo1, new StringExtractor<Cars>() {
        @Override
        public String getStringValue(Cars product, int position) {
            return " " + product.rangeTo;
        }
    });
    dict.addStringField(R.id.tvDriver, new StringExtractor<Cars>() {
        @Override
        public String getStringValue(Cars product, int position) {
            return "Driver: " + product.carDriver;
        }
    });
    dict.addStringField(R.id.tvMaxDays, new StringExtractor<Cars>() {
        @Override
        public String getStringValue(Cars product, int position) {
            return "Max Days: " + product.carDays;
        }
    });
    dict.addStringField(R.id.tvDriverFirst, new StringExtractor<Cars>() {
        @Override
        public String getStringValue(Cars product, int position) {
            return " " + product.driverFirstName;
        }
    });
    dict.addStringField(R.id.tvDriverMiddle, new StringExtractor<Cars>() {
        @Override
        public String getStringValue(Cars product, int position) {
            return " " + product.driverMiddleName;
        }
    });
    dict.addStringField(R.id.tvDriverLast, new StringExtractor<Cars>() {
        @Override
        public String getStringValue(Cars product, int position) {
            return " " + product.driverLastName;
        }
    });
    dict.addStringField(R.id.tvOwnerContact, new StringExtractor<Cars>() {
        @Override
        public String getStringValue(Cars product, int position) {
            return " " + product.ownerContact;
        }
    });



    adapter = new FunDapter<>(
            getActivity(), carList, R.layout.layout_list, dict);

    lvCars.setAdapter(adapter);
    lvCars.setOnItemClickListener(this);

    if(swipeCar.isRefreshing()){
        swipeCar.setRefreshing(false);
    }
    adapter.notifyDataSetChanged();
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Cars selectedCars = carList.get(position);
    Intent in = new Intent(getActivity(), DetailActivity.class);
    in.putExtra("cars2", selectedCars);
    startActivity(in);
}

DetailActivity

        final Cars Cars = (Cars)getIntent().getSerializableExtra("cars2");

try {
if (Cars != null) {

    tvDriver321.setText(Cars.carDriver);
    tvMaxDays.setText(Cars.carDays);
    tvCarModel.setText(Cars.Car_Model);
    tvCarType.setText( Cars.Car_Type);
    tvCapacity.setText(""+Cars.Capacity);
    tvFuelType.setText(Cars.FuelType);
    tvPlateNumber.setText(Cars.PlateNumber);
    tvPoster.setText(Cars.owner);
    tvPrice.setText(Cars.carPrice);
    tvCarID.setText(""+Cars.Car_No);
    tvFrom.setText(Cars.rangeFrom);
    tvTo.setText(Cars.rangeTo);
    tvDriver.setText(Cars.driverFirstName);
    tvDriverLast.setText(Cars.driverLastName);
    tvContact.setText(Cars.ownerContact);

    Log.d(TAG,Cars.Car_Model);

    ImageLoader.getInstance().displayImage(Cars.Image, ivImage);

}

}catch (Exception e){
    System.out.println("Error in details: " + e.toString());
}

Cars.class

public class Cars implements Serializable {



@SerializedName("Car_No")
public int Car_No;

@SerializedName("Car_Model")
public String Car_Model;

@SerializedName("Car_Type")
public String Car_Type;

@SerializedName("Capacity")
public  int Capacity;

@SerializedName("Image")
public String Image;

@SerializedName("fuelType")
public String FuelType;

@SerializedName("carPlatenuNumber")
public String PlateNumber;

@SerializedName("owner")
public String owner;

@SerializedName("carPrice")
public String carPrice;

@SerializedName("resDate")
public String resDate;

@SerializedName("resTime")
public String resTime;

@SerializedName("resLocation")
public String resLocation;

@SerializedName("rangeFrom")
public String rangeFrom;

@SerializedName("rangeTo")
public String rangeTo;

@SerializedName("carDriver")
public String carDriver;

@SerializedName("carDays")
public String carDays;

@SerializedName("driverFirstName")
public String driverFirstName;

@SerializedName("driverMiddleName")
public String driverMiddleName;

@SerializedName("driverLastName")
public String driverLastName;

@SerializedName("ownerContact")
public String ownerContact;


}

1 个答案:

答案 0 :(得分:0)

尝试

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Car selectedCars = carList.get(position);
    Intent in = new Intent(getActivity(), DetailActivity.class);
    in.putExtra("cars2", selectedCars);
    startActivity(in);
}

在其他Activity中:

final Car Cars = getIntent().getSerializableExtra("cars2");