具有内部数组类的数组pojo类的RecyclerView适配器

时间:2016-11-18 18:35:44

标签: java android android-recyclerview parcelable pojo

虽然我认为它比我想的更简单,但这有点令人困惑。

我有一个名为Commodities的类和一个名为Stops的类,停止类接收一系列商品。停止内部的所有项目都正确解析,如果我检查调试器上的停止类的内容,我可以看到它应该去的地方。问题在于知道从适配器获取什么,将用于显示Stops类及其内部Commodities addapter类的内容。

这是我的代码(简短)

首先是停止类

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];
        }
    };
}

然后是商品类

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];}
    };
}

上述信息的适配器当前显示为:

公共类StopsAdapter扩展了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());
        }
    }
}

获取信息并启动适配器的活动:

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);


    }

}

根据我在Stops类中的内容填充显示内容,这很好,像地址这样的变量(我在parcelable部分编码)会相应地显示,但我的问题基本上就是。我如何使用商品呢?如果我试着打电话(在停止适配器中)

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

我收到一条错误,指出它无法解决商品类中的方法getItem()。

基本上,我不知道如何从StopsAdapter中的商品中获取内容。

1 个答案:

答案 0 :(得分:3)

函数getCommodities()返回一个数组。

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

您应该从退回的商品中选择和项目,以便您可以访问该元素的信息

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