单个ListView中的不同对象

时间:2018-10-07 18:45:48

标签: java android json listview

我有以下问题,我有3种不同对象的json响应。我需要在一个列表视图中显示此数据。我正在使用GSON库反序列化JSON。

Json文件

{
"status": "success",
"data": {
    "responses": [
        {
            "id": 3,
            "status": 0,
            "order": {
                "id": 1,
                "address": "3249-11-4910-0000, Burgaw, NC 28425, США",
                "description": "работа работа ",
                "created_at": 1535671020,
                "status": 0,
                "employer": {
                    "id": 11,
                    "name": "Test1",
                    "surname": "TESTOVICH2",
                    "phone": "777777777777",
                    "email": "qwe@gmail.com",
                    "photo": {
                        "id": 11,
                        "url": "http://link.com/storage/users/photos/a6e0ec9e4bf3e0b3ef0e2f06231169b0.png"
                    }
                }
            },
            "employee": {
                "id": 11,
                "name": "Test1",
                "surname": "TESTOVICH2",
                "phone": "777777777777",
                "email": "qwe@gmail.com",
                "photo": {
                    "id": 11,
                    "url": "http://link.com/storage/users/photos/a6e0ec9e4bf3e0b3ef0e2f06231169b0.png"
                }
            }
        }          
    ],
    "open_orders": [
        {
            "id": 2,
            "address": "улица Шакарима, Алматы 050000, Казахстан",
            "description": "пляжоажга",
            "created_at": 1535676060,
            "status": 0,
            "employer": {
                "id": 12,
                "name": "юзер",
                "surname": "юзер"
            },
            "employee": null,
            "category": {
                "id": 2,
                "name": "Уборка квартир"
            },
            "photos": [
                {
                    "id": 18,
                    "url": "http://link.com/storage/orders/open/2/45686fb353eacc7ff9d78eb90a6e8663.JPG"
                }
            ]
        }
    ],
    "direct_orders": [
        {
            "id": 13,
            "address": "Maevatanana, Мадагаскар",
            "description": "прямой заказ ",
            "created_at": 1540435200,
            "status": 0,
            "employer": {
                "id": 12,
                "name": "юзер",
                "surname": "юзер"
            },
            "category": {
                "id": 6,
                "name": "Услуги переводчика"
            },
            "district": {
                "id": 13,
                "name": "шшшш"
            },
            "photos": [
                {
                    "id": 36,
                    "url": "http://link.com/storage/orders/direct/13/5104e6605f400703ac702c1a59905889.jpg"
                }
            ]
        }
    ]
}

}

这是我的适配器

public class RequestInListAdapter extends ArrayAdapter<Data> {
private Context context;
private ArrayList<Data> RequestInListItem;

public RequestInListAdapter(Context context, ArrayList<Data> arrayList) {
    super(context, 0, arrayList);
    this.context = context;
    this.RequestInListItem = arrayList;
}

@SuppressLint("ResourceAsColor")
@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    final Data card_item = RequestInListItem.get(position);
for (Response order : responses) {
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.order_item, parent, false);
        }
        TextView desc = convertView.findViewById(R.id.description);
        TextView address = convertView.findViewById(R.id.address);
        TextView dnt = convertView.findViewById(R.id.dnt);
        TextView category = convertView.findViewById(R.id.category);
        TextView status = convertView.findViewById(R.id.status);
        RelativeLayout card_layout = convertView.findViewById(R.id.card_layout);
        de.hdodenhof.circleimageview.CircleImageView photo_template = convertView.findViewById(R.id.photo_template);

        desc.setText(order.getOrder().getDescription());
        address.setText(order.getOrder().getAddress());
        Picasso.with(context).load(String.valueOf(order.getOrder().getEmployer().getPhoto())).resize(70, 70).into(photo_template);
        long timestamp = order.getOrder().getCreatedAt() * 1000L;
        dnt.setText(getDate(timestamp));
        status.setText("Response");
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        return convertView;
    }
    return convertView;
}

}

这段代码来自我的主班

private ArrayList<Data> arrayList = new ArrayList<>();
RequestInListAdapter adapter;


JSONReader jsonReader = new JSONReader();
BellIn bellIn = jsonReader.getBellIn(responseObj.toString());
arrayList.add(bellIn.getData());
adapter = new RequestInListAdapter(getContext(), arrayList);
listView.setAdapter(adapter);

现在Listview仅显示1个元素,但我应该得到更多。问题是因为我传递了 bellIn 对象,该对象包含3个不同的列表

public class Data {

private List<Response> responses = null;
private List<OpenOrder> open_orders = null;
private List<DirectOrder> direct_orders = null;

public List<Response> getResponses() {
    return responses;
}

public void setResponses(List<Response> responses) {
    this.responses = responses;
}

public List<OpenOrder> getOpenOrders() {
    return open_orders;
}

public void setOpenOrders(List<OpenOrder> openOrders) {
    this.open_orders = openOrders;
}

public List<DirectOrder> getDirectOrders() {
    return direct_orders;
}

public void setDirectOrders(List<DirectOrder> directOrders) {
    this.direct_orders = directOrders;
}

1。如何在1个Listview中显示3个不同的对象?

2。如何在ListView中显示所有项目(不仅是第一个项目)?

3。我可以为此对象使用不同的视图吗?

2 个答案:

答案 0 :(得分:0)

进行自定义的Listview,并在元素的自定义布局中添加所有3个项目,并假设您不想在某个特定元素中显示所有项目,然后使用属性visibility使它不可见

答案 1 :(得分:0)

首先,我更改了布局。因此,最后,我在1种布局中放置了3个ListView。

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/swipe"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="10dp">

            <ListView
                android:id="@+id/listviewResponse"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:divider="@android:color/transparent"
                android:dividerHeight="10sp"
                android:paddingStart="15dp"
                android:paddingEnd="15dp" />

            <ListView
                android:id="@+id/listviewDirect"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:divider="@android:color/transparent"
                android:dividerHeight="10sp"
                android:paddingStart="15dp"
                android:paddingEnd="15dp" />

            <ListView
                android:id="@+id/listviewOpen"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:divider="@android:color/transparent"
                android:dividerHeight="10sp"
                android:paddingStart="15dp"
                android:paddingEnd="15dp" />
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>
</android.support.v4.widget.SwipeRefreshLayout>

我创建了3个不同的适配器并将适配器设置为Listviews

        @BindView(R.id.listviewResponse)
        ListView listViewResponse;
        @BindView(R.id.listviewOpen)
        ListView listViewOpen;
        @BindView(R.id.listviewDirect)
        ListView listViewDirect;

        private List<Response> responses = new ArrayList<>();
        private List<OpenOrder> open_orders = new ArrayList<>();
        private List<DirectOrder> direct_orders = new ArrayList<>();

        RequestListResponseAdapter adapterResponse;
        RequestListOpenAdapter adapterOpen;
        RequestListDirectAdapter adapterDirect;

        adapterResponse = new RequestListResponseAdapter(getContext(), responses);
        adapterOpen = new RequestListOpenAdapter(getContext(), open_orders, openType);
        adapterDirect = new RequestListDirectAdapter(getContext(), direct_orders);
        listViewResponse.setAdapter(adapterResponse);
        listViewDirect.setAdapter(adapterDirect);
        listViewOpen.setAdapter(adapterOpen);