Search code examples
javaandroidandroid-recyclerviewparcelablepojo

RecyclerView adapter of array pojo class with inner array class


This is somewhat confusing albeit I believe it to be simpler than what I think.

I have a class called Commodities and a class called Stops, the stops class takes in an array of commodities. All the items inside stops are parsing correctly and if I inspect the contents of the stops class on the debugger I can see that everything is where it should go. The issue lies in knowing what to get from the adapter that will be used to display the contents from the Stops class and its inner Commodities addapter class.

Here is the code that I have(it is brief)

First is the stops class

public class Stops implements Parcelable{
    private String stop_id;
    private String type;
    private String location;
    private String address;
    private String city;
    private String state;
    private String zip;
    private String from;
    private String to;

    private Commodities[] commodities;

    public Stops() {}
    public String getStop_id() {
        return stop_id;
    }

    public void setStop_id(String stop_id) {
        this.stop_id = stop_id;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }


    public String getLocation() {
        return location;
    }

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

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getZip() {
        return zip;
    }

    public void setZip(String zip) {
        this.zip = zip;
    }

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public String getTo() {
        return to;
    }

    public void setTo(String to) {
        this.to = to;
    }

    public Commodities[] getCommodities() {
        return commodities;
    }



    public void setCommodities(Commodities[] commodities) {
        this.commodities = commodities;
    }
    /**
     * The content for the actual parcelables start in here
     *
     * */
    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeTypedArray(commodities,0);
        dest.writeString(address);

    }
    private Stops(Parcel in){
        commodities = in.createTypedArray(Commodities.CREATOR);
        address = in.readString();
    }
    public static final Creator<Stops> CREATOR = new Creator<Stops>(){
        @Override
        public Stops createFromParcel(Parcel source) {
            return new Stops(source);
        }
        @Override
        public Stops[] newArray(int size){
            return new Stops[size];
        }
    };
}

Then the commodities class

public class Commodities  implements Parcelable{
    private String item;
    private String description;
    private String poNumber;
    private String qty;
    private String weight;

    public Commodities() {}

    public String getItem() {
        return item;
    }

    public void setItem(String item) {
        this.item = item;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getPoNumber() {
        return poNumber;
    }

    public void setPoNumber(String poNumber) {
        this.poNumber = poNumber;
    }

    public String getQty() {
        return qty;
    }

    public void setQty(String qty) {
        this.qty = qty;
    }

    public String getWeight() {
        return weight;
    }

    public void setWeight(String weight) {
        this.weight = weight;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(item);
        dest.writeString(description);
        dest.writeString(poNumber);
        dest.writeString(qty);
        dest.writeString(weight);
    }
    private Commodities(Parcel in){
        item = in.readString();
        description  =in.readString();
        poNumber = in.readString();
        qty = in.readString();
        weight = in.readString();
    }

    public static final Creator<Commodities> CREATOR = new Creator<Commodities>() {
        @Override
        public Commodities createFromParcel(Parcel source) {
            return new Commodities(source);
        }
        @Override
        public Commodities[] newArray(int size) {
            return new Commodities[size];}
    };
}

The adapter for the information above currently reads as:

public class StopsAdapter extends RecyclerView.Adapter{

    Stops[] como; // change this to stops and every other ocurrance of stops
    public StopsAdapter(Stops[] comms){
        como = comms;
    }
    @Override
    public StopsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.load_display_items, parent, false);
        StopsViewHolder holder = new StopsViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(StopsViewHolder holder, int position) {
        holder.bindStops(como[position]);

    }

    @Override
    public int getItemCount() {
        return como.length;
    }

    public class StopsViewHolder extends RecyclerView.ViewHolder {
        public TextView mItem;
        public StopsViewHolder(View itemView) {
            super(itemView);
            mItem = (TextView)itemView.findViewById(R.id.nameHolder);
        }
        public void bindStops(Stops stop) {
            mItem.setText(stop.getAddress());
            Log.d("<",mItem.getText().toString());
        }
    }
}

The activity that gets the information and starts the adapter:

public class LoadsActivity extends AppCompatActivity {

    // et the gold juju
    private Stops[] commodities;
    @BindView(R.id.recyclerView) RecyclerView mRecyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loads);
        ButterKnife.bind(this);

        //get that intent parcelable juju mojo baby
        Intent intent = getIntent();
        Parcelable[] parcelable = intent.getParcelableArrayExtra(MainActivity.COMM_CONTENT);

        commodities = Arrays.copyOf(parcelable, parcelable.length, Stops[].class);


        //adapter bitches
        StopsAdapter adapter = new StopsAdapter(commodities);
        mRecyclerView.setAdapter(adapter);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(layoutManager);

        mRecyclerView.setHasFixedSize(true);


    }

}

The contents of the display are populated based on what I have inside of the Stops class, which is fine, variables like address(as I coded in the parcelable portion) do display accordingly, but my question is basically. How would I go by using the items of commodity as well? if I try to call(in the stops adapter)

 mItem.setText(stop.getCommodities().getItem());

I get an error stating that it cant resolve the method getItem() which is inside of the commodities class.

Basically, I do not know how to get the Contents from commodities inside of the StopsAdapter.


Solution

  • The function getCommodities() returns an array.

     public Commodities[] getCommodities() {
        return commodities;
    }
    

    You should select and item from the commodities returned so you can access the information of that element

    mItem.setText(stop.getCommodities()[index].getItem());