单个 ListView 中的不同对象
Posted
技术标签:
【中文标题】单个 ListView 中的不同对象【英文标题】:Different objects in single ListView 【发布时间】:2019-03-12 12:03:56 【问题描述】:我有以下问题,我有 json 响应,它有 3 种不同的对象。我需要在一个列表视图中显示这些数据。我正在使用 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。我可以对这些对象使用不同的视图吗?
【问题讨论】:
【参考方案1】:制作一个自定义的 Listview 并在元素的自定义布局中添加所有 3 个项目,并假设您不想显示某个特定元素中的所有项目,然后使用属性 visibility
使其不可见
【讨论】:
【参考方案2】:首先我改变了布局。所以,最后,我在 1 个布局中放置了 3 个 ListView。
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe"
android:layout_
android:layout_>
<android.support.v4.widget.NestedScrollView
android:layout_
android:layout_
android:fillViewport="true">
<LinearLayout
android:layout_
android:layout_
android:orientation="vertical"
android:padding="10dp">
<ListView
android:id="@+id/listviewResponse"
android:layout_
android:layout_
android:divider="@android:color/transparent"
android:dividerHeight="10sp"
android:paddingStart="15dp"
android:paddingEnd="15dp" />
<ListView
android:id="@+id/listviewDirect"
android:layout_
android:layout_
android:divider="@android:color/transparent"
android:dividerHeight="10sp"
android:paddingStart="15dp"
android:paddingEnd="15dp" />
<ListView
android:id="@+id/listviewOpen"
android:layout_
android:layout_
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);
【讨论】:
以上是关于单个 ListView 中的不同对象的主要内容,如果未能解决你的问题,请参考以下文章
使用 Kotlin 从 Android 上 ViewModel 中的 LiveData 更新 ListView 中的元素
listview点击item中的button改变item中的其它的控件的值
C#在ListView中获取CheckBox选中的值(多选)