我使用了两种不同颜色的自定义行。但是它返回的是黑色的同一行。我也有黄色的行,但它只返回黑色的行。我无法理解它,因此在下面共享了我的源代码。请帮我
json_orders = json.getJSONArray("orders");
for(int i = 0; i < json_orders.length(); i++){
JSONObject c = json_orders.getJSONObject(i);
// Storing JSON item in a Variable
String orderDate = c.getString("orderDate");
String orderTime = c.getString("orderTime");
String orderId = c.getString("orderId");
String driverStatusId = c.getString("driverStatusId");
// Adding value HashMap key => value
HashMap<String, String> map = new HashMap<String, String>();
map.put("orderDate", orderDate);
map.put("orderTime", orderTime);
map.put("orderId", orderId);
oslist.add(map);
list=(ListView)view.findViewById(R.id.list);
ListAdapter adapter = new SimpleAdapter(getActivity(), oslist, i, null, null);
if (!driverStatusId.equals("4"))
{
adapter = new SimpleAdapter(getActivity(), oslist,
R.layout.list_row_new,
new String[] { "orderDate", "orderTime"}, new int[] {
R.id.orderDate, R.id.orderTime});
}
else {
adapter = new SimpleAdapter(getActivity(), oslist,
R.layout.list_row,
new String[] { "orderDate", "orderTime"}, new int[] {
R.id.orderDate, R.id.orderTime});
}
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//Toast.makeText(getActivity(), "You Clicked at "+oslist.get(+position).get("orderId"), Toast.LENGTH_SHORT).show();
Fragment fragment = null;
fragment = new OrderDetailsFragment();
final Bundle bundle = new Bundle();
bundle.putString("orderId", oslist.get(+position).get("orderId"));
Log.i("BUNDLE", bundle.toString());
fragment.setArguments(bundle);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment, "OrderDetailsFragment").commit();
//new UpdateDestinationInfo().execute(String.valueOf(db.getUserDetails().get("currentorderid")), oslist.get(+position).get("longlat"));
// Toast.makeText(getActivity(), "You Clicked at "+String.valueOf(db.getUserDetails().get("id")), Toast.LENGTH_SHORT).show();
}
最佳答案
如果在ListView
中有多个视图,则将需要除SimpleAdapter
以外的其他内容,因为您需要覆盖一些方法。您将最终需要扩展BaseAdapter
来创建自定义适配器。制作自己的适配器时需要的第一种方法是getViewTypeCount()
。这告诉适配器存在一定数量的视图类型,并且应该考虑这一点。您需要覆盖的第二种方法是getItemViewType()
,以便ListView
知道哪些视图具有哪种类型,以便可以相应地处理视图回收。然后,在getView()
方法中,根据位置,根据视图类型返回不同的视图类型。
关于java - 具有两个不同颜色的自定义行的SimpleAdapter,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25109018/