具有内部数组类的数组pojo类的RecyclerView适配器 [英] RecyclerView adapter of array pojo class with inner array class

查看:100
本文介绍了具有内部数组类的数组pojo类的RecyclerView适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这有点令人困惑,尽管我认为它比我想象的要简单.

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

我有一个名为Commodities的类和一个名为Stops的类,stops类接收一系列商品. Stop中的所有项目都可以正确解析,如果我在调试器上检查了stops类的内容,则可以看到一切都应该放在哪里.问题在于知道从适配器中获取什么,该适配器将用于显示Stops类及其内部Commodities addapter类的内容.

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.

这是我的代码(简短)

首先是停车站类

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

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

The adapter for the information above currently reads as:

公共类StopsAdapter扩展了RecyclerView.Adapter {

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


    }

}

根据我在Stops类中拥有的内容填充显示的内容,这很好,像address(如我在可包裹部分中编码的)之类的变量也可以相应显示,但是我的问题基本上是这样.我还将如何使用商品?如果我尝试致电(在站点适配器中)

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

我收到一条错误消息,指出它无法解析商品类内部的getItem()方法.

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

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

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

推荐答案

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

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

这篇关于具有内部数组类的数组pojo类的RecyclerView适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆