Android:自定义 ArrayAdapter 不工作,getView() 未被调用
Android: Custom ArrayAdapter not working, getView() not getting called
我正在使用 CustomArrayAdapter
和 ListView
。但是,即使在设置 setAdapter
之后它也不起作用。以下是我的代码片段:
PastOrders.java 片段显示listView
public class PastOrders extends Fragment {
private View view;
private ListView pastOrdersList;
private Context context;
private ArrayList<Order> orders;
public PastOrders() {
}
@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
view = inflater.inflate(R.layout.fragment_pastorders, container, false);
context = getActivity();
pastOrdersList = (ListView) view.findViewById(R.id.pastOrdersList);
ParseQuery<ParseObject> query = ParseQuery.getQuery("Order");
query.whereEqualTo("user_id", Vars.currentUser);
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException e) {
if(e != null) {
Log.e("TAG", e.getLocalizedMessage());
} else {
ArrayList<Order> orders = new ArrayList<Order>();
Log.i("TAG", objects.toString());
for (ParseObject object: objects) {
Order temp = new Order();
temp.orderId = object.getObjectId();
temp.date = object.getCreatedAt();
temp.status = object.getString("status");
Log.i("TAG", "orderID: "+ object.getObjectId() + " temp orderID"+ temp.orderId);
orders.add(temp);
Vars.pastOrders.add(object);
}
Log.i("TAG", "after for loop past orders: "+ orders.toString());
PastOrdersAdapter pastOrdersAdapter = new PastOrdersAdapter(getActivity(), R.layout.past_orders_row, orders);
pastOrdersList.setAdapter(pastOrdersAdapter);
}
}
});
return view;
}
@Override
public void onResume() {
super.onResume();
}
}
PastOrdersAdapter.java 自定义适配器
public class PastOrdersAdapter extends ArrayAdapter<Order> {
private Context context;
private ViewHolder holder;
//private ArrayList<Order> orders;
public PastOrdersAdapter(Context context, int resource, List<Order> orders) {
super(context, resource);
// this.orders = orders;
Log.i("TAG", "POA adapter called");
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.i("TAG", "getView from POA");
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
if (convertView == null) {
convertView = inflater
.inflate(R.layout.past_orders_row, parent, false);
holder = new ViewHolder();
holder.orderId = (TextView) convertView.findViewById(R.id.orderId);
holder.date = (TextView) convertView.findViewById(R.id.date);
holder.status = (TextView) convertView.findViewById(R.id.status);
holder.details = (ImageView) convertView.findViewById(R.id.details);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ParseObject order = Vars.pastOrders.get(position);
Log.i("TAG", "order id in adapter: "+ order.getObjectId());
if(order != null) {
holder.orderId.setText(order.getObjectId());
holder.date.setText(getFormattedDate(order.getCreatedAt()));
holder.status.setText(order.getString("status"));
if(order.getString("status").equals("Delivered")) {
holder.status.setTextColor(Color.GREEN);
} else if(order.getString("status").equals("Cancelled")) {
holder.status.setTextColor(Color.RED);
} else {
holder.status.setTextColor(Color.YELLOW);
}
}
return convertView;
}
static class ViewHolder {
TextView orderId;
TextView date;
TextView status;
ImageView details;
}
}
Order.java
public class Order {
public String orderId;
public Date date;
public String status;
}
fragment_pastorders.xml 显示 ListView 的 Fragment 的布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/pastOrdersList"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</RelativeLayout>
past_orders_row.xml listview每一行的布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="150dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="150dp"
android:orientation="vertical"
android:id="@+id/LinearLayout01">
<TextView
android:id="@+id/orderId"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/date"
android:textStyle="italic"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/status"/>
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_action_next_item"
android:layout_toRightOf="@id/LinearLayout01"
android:layout_centerVertical="true"
android:id="@+id/details"/>
</RelativeLayout>
我也试过打印 getCount() 并且它是非零的。我已经尝试了一切。请帮助
谢谢,
阿皮特
在自定义适配器的构造函数中,您忘记将 orders
参数传递给父构造函数。所以你的适配器不知道要显示什么。
您正在将空列表 订单 传递给您的适配器。然后在适配器中使用不同的列表:Vars.pastOrders。
所以你的适配器有空列表。
替换为:
PastOrdersAdapter pastOrdersAdapter =
new PastOrdersAdapter(getActivity(), R.layout.past_orders_row, orders);
有了这个:
PastOrdersAdapter pastOrdersAdapter =
new PastOrdersAdapter(getActivity(), R.layout.past_orders_row, Vars.pastOrders);
我正在使用 CustomArrayAdapter
和 ListView
。但是,即使在设置 setAdapter
之后它也不起作用。以下是我的代码片段:
PastOrders.java 片段显示listView
public class PastOrders extends Fragment {
private View view;
private ListView pastOrdersList;
private Context context;
private ArrayList<Order> orders;
public PastOrders() {
}
@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
view = inflater.inflate(R.layout.fragment_pastorders, container, false);
context = getActivity();
pastOrdersList = (ListView) view.findViewById(R.id.pastOrdersList);
ParseQuery<ParseObject> query = ParseQuery.getQuery("Order");
query.whereEqualTo("user_id", Vars.currentUser);
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException e) {
if(e != null) {
Log.e("TAG", e.getLocalizedMessage());
} else {
ArrayList<Order> orders = new ArrayList<Order>();
Log.i("TAG", objects.toString());
for (ParseObject object: objects) {
Order temp = new Order();
temp.orderId = object.getObjectId();
temp.date = object.getCreatedAt();
temp.status = object.getString("status");
Log.i("TAG", "orderID: "+ object.getObjectId() + " temp orderID"+ temp.orderId);
orders.add(temp);
Vars.pastOrders.add(object);
}
Log.i("TAG", "after for loop past orders: "+ orders.toString());
PastOrdersAdapter pastOrdersAdapter = new PastOrdersAdapter(getActivity(), R.layout.past_orders_row, orders);
pastOrdersList.setAdapter(pastOrdersAdapter);
}
}
});
return view;
}
@Override
public void onResume() {
super.onResume();
}
}
PastOrdersAdapter.java 自定义适配器
public class PastOrdersAdapter extends ArrayAdapter<Order> {
private Context context;
private ViewHolder holder;
//private ArrayList<Order> orders;
public PastOrdersAdapter(Context context, int resource, List<Order> orders) {
super(context, resource);
// this.orders = orders;
Log.i("TAG", "POA adapter called");
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.i("TAG", "getView from POA");
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
if (convertView == null) {
convertView = inflater
.inflate(R.layout.past_orders_row, parent, false);
holder = new ViewHolder();
holder.orderId = (TextView) convertView.findViewById(R.id.orderId);
holder.date = (TextView) convertView.findViewById(R.id.date);
holder.status = (TextView) convertView.findViewById(R.id.status);
holder.details = (ImageView) convertView.findViewById(R.id.details);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ParseObject order = Vars.pastOrders.get(position);
Log.i("TAG", "order id in adapter: "+ order.getObjectId());
if(order != null) {
holder.orderId.setText(order.getObjectId());
holder.date.setText(getFormattedDate(order.getCreatedAt()));
holder.status.setText(order.getString("status"));
if(order.getString("status").equals("Delivered")) {
holder.status.setTextColor(Color.GREEN);
} else if(order.getString("status").equals("Cancelled")) {
holder.status.setTextColor(Color.RED);
} else {
holder.status.setTextColor(Color.YELLOW);
}
}
return convertView;
}
static class ViewHolder {
TextView orderId;
TextView date;
TextView status;
ImageView details;
}
}
Order.java
public class Order {
public String orderId;
public Date date;
public String status;
}
fragment_pastorders.xml 显示 ListView 的 Fragment 的布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/pastOrdersList"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</RelativeLayout>
past_orders_row.xml listview每一行的布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="150dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="150dp"
android:orientation="vertical"
android:id="@+id/LinearLayout01">
<TextView
android:id="@+id/orderId"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/date"
android:textStyle="italic"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/status"/>
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_action_next_item"
android:layout_toRightOf="@id/LinearLayout01"
android:layout_centerVertical="true"
android:id="@+id/details"/>
</RelativeLayout>
我也试过打印 getCount() 并且它是非零的。我已经尝试了一切。请帮助
谢谢,
阿皮特
在自定义适配器的构造函数中,您忘记将 orders
参数传递给父构造函数。所以你的适配器不知道要显示什么。
您正在将空列表 订单 传递给您的适配器。然后在适配器中使用不同的列表:Vars.pastOrders。 所以你的适配器有空列表。 替换为:
PastOrdersAdapter pastOrdersAdapter =
new PastOrdersAdapter(getActivity(), R.layout.past_orders_row, orders);
有了这个:
PastOrdersAdapter pastOrdersAdapter =
new PastOrdersAdapter(getActivity(), R.layout.past_orders_row, Vars.pastOrders);