我在google上尽可能多地搜索,我在这里找到了尽可能多的搜索,我真的很茫然,我的同事都没有帮助,我只是想有人看看这个并告诉我我错过了什么导致listview不刷新数据。日志显示包含正确数量的元素但未显示在列表中。
public class ShiftListViewFragment extends Fragment {
private static int TOTAL_ITEMS = 11;
private static String TAG = "ShiftListViewFragment";
private ListView mListView;
private ShiftItemArrayAdapter mAdapter;
private ArrayList<ShiftItem> mShiftItems = new ArrayList<ShiftItem>();
;
private ArrayList<ShiftItem> displayShiftItems = new ArrayList<ShiftItem>();
private Button mPageLeftButton;
private Button mPageRightButton;
private int mPageNumber = 0;
private int mPageIndex = 0;
public static ShiftListViewFragment newInstance() {
ShiftListViewFragment shiftListViewFragment = new ShiftListViewFragment();
return shiftListViewFragment;
}
public ShiftListViewFragment() {
}
public void populateShiftItems() {
Date clockIn = new Date("08/17/2015 11:11 AM");
Date clockOut = new Date("08/17/2015 11:11 PM");
Date breakTimeIn = new Date("08/17/2015 10:11 PM");
Date breakTimeOut = new Date("08/17/2015 10:11 PM");
for (int i = 0; i < 28; i++) {
ShiftItem shiftItem =
new ShiftItem(new ShiftDto(clockIn, clockOut, breakTimeIn, breakTimeOut));
mShiftItems.add(shiftItem);
}
}
public void populateDisplayShifts() {
int lowerBound = mPageIndex * 11;
int uppBound = lowerBound + 11;
if (uppBound > mShiftItems.size()) {
uppBound = mShiftItems.size();
}
ArrayList<ShiftItem> shifts = new ArrayList<ShiftItem>();
for (int i = lowerBound; i < uppBound; i++) {
shifts.add(mShiftItems.get(i));
}
displayShiftItems.clear();
mAdapter.clear();
displayShiftItems.addAll(shifts);
mAdapter.notifyDataSetChanged();
}
public int getPageNumber() {
int pages = 0;
pages = mShiftItems.size() / TOTAL_ITEMS;
if (pages % 11 != 0) {
pages++;
}
Log.d("TAG", "TotalPages " + pages);
Log.d("TAG", "Shift items count:" + mShiftItems.size());
return pages;
}
private void initViews(View view) {
mListView = (ListView)view.findViewById(R.id.shift_report_list);
mPageLeftButton = (Button)view.findViewById(R.id.btn_page_left);
mPageRightButton = (Button)view.findViewById(R.id.btn_page_right);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.shift_list_report_fragment, null);
initViews(view);
populateShiftItems();
mPageNumber = getPageNumber();
mAdapter = new ShiftItemArrayAdapter(getActivity(), R.layout.shift_list_item,
displayShiftItems,
R.color.shift_list_item_bg);
mListView.setAdapter(mAdapter);
populateDisplayShifts();
mPageRightButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPageIndex++;
if (mPageIndex > mPageNumber - 1) {
mPageIndex = 0;
}
populateDisplayShifts();
Log.d(TAG, "Page:" + mPageIndex + " Actual items in list:" + mAdapter.getCount());
}
});
mPageLeftButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPageIndex--;
if (mPageIndex < 0) {
mPageIndex = mPageNumber - 1;
}
populateDisplayShifts();
Log.d(TAG, "Page:" + mPageIndex + " Actual items in list:" + mAdapter.getCount());
}
});
return view;
}
}
我的适配器
public class ShiftItemArrayAdapter extends ArrayAdapter<ShiftItem> {
private LayoutInflater mLayoutInflater;
private int mAlternateColorResource;
public ShiftItemArrayAdapter(Context context, int resource,
ArrayList<ShiftItem> shiftItems,
@ColorRes int colorResID) {
super(context, resource, shiftItems);
this.mAlternateColorResource = colorResID;
this.mLayoutInflater = LayoutInflater.from(context);
}
/**
* Applies background resources in an alternating fashion, based on the view's position.
*/
protected void applyBackground(int position, @NonNull View convertView) {
if (position % 2 == 0) {
convertView.setBackgroundResource(mAlternateColorResource);
} else {
convertView.setBackgroundResource(android.R.color.white);
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
String clockedIn = getItem(position).getInTime();
String clockedOut =
getItem(position).getOutTime();
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = mLayoutInflater.inflate(R.layout.shift_list_item, null);
viewHolder.clockedInTextView = (TextView)convertView.findViewById(R.id.time_in);
viewHolder.clockedOutTextView = (TextView)convertView.findViewById(R.id.time_out);
viewHolder.dashedTextView = (TextView)convertView.findViewById(R.id.dash);
viewHolder.breakTotalTextView = (TextView)convertView.findViewById(R.id.break_total);
viewHolder.totalTimeTextView = (TextView)convertView.findViewById(R.id.total_time);
viewHolder.clockedInTextView.setText(clockedIn);
viewHolder.clockedOutTextView.setText(clockedOut);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder)convertView.getTag();
}
Date breaksIn = new Date("11/11/2015 11:20 AM");
Date breaksOut = new Date("11/11/2015 12:20 PM");
String breakHours = String.valueOf(
DateHelper.getTimeDuration(breaksIn, breaksOut, false).getStandardHours());
String breakMin = String.valueOf(
DateHelper.getTimeDuration(breaksIn, breaksOut, false).getStandardMinutes());
viewHolder.breakTotalTextView.setText(breakHours + " HR " + breakMin + " MIN ");
applyBackground(position, convertView);
return convertView;
}
private class ViewHolder {
public TextView clockedInTextView;
public TextView dashedTextView;
public TextView clockedOutTextView;
public TextView breakTotalTextView;
public TextView totalTimeTextView;
}
}
任何和所有帮助将不胜感激。
答案 0 :(得分:0)
显然Android中存在一个错误,其中列表视图中的列表项无法定义其实际大小,因此解决方案是将list_item.xml设置为特定高度而不是wrap_content。