java for循环完成了比需要更多

时间:2017-03-14 20:01:13

标签: java android

所以我有一个for循环使我的listview足够大而不能滚动,我从@HussoM使用了这个循环。 Cursor返回3行,但for循环有时会运行7次,有时会运行10次。

class dayAdapter extends CursorAdapter {
    public dayAdapter(Context context, Cursor c) {
        super(context, c);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View view = LayoutInflater.from(context).inflate(R.layout.day_item, parent, false);
        dayAdapterViewHolder viewHolder = new dayAdapterViewHolder();
        viewHolder.dayTitle = (TextView) view.findViewById(R.id.dayTitle);
        viewHolder.transactionList = (ListView) view.findViewById(R.id.day_list);
        view.setTag(viewHolder);
        return view;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        dayAdapterViewHolder dayAdapterViewHolder = (dayAdapter.dayAdapterViewHolder) view.getTag();
        Calendar calendar = Calendar.getInstance();
        DatabaseHelper dbHelper = new DatabaseHelper(context);
        int day = cursor.getInt(cursor.getColumnIndexOrThrow(dbHelper.COLUMN_DATE_DAY));
        calendar.set(year - 1900, month, day);
        Date date = new Date(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
        DateFormat dateFormat = DateFormat.getDateInstance();

        dayAdapterViewHolder.dayTitle.setText(dateFormat.format(date));
        DayItemAdapter dayItemAdapter = new DayItemAdapter(context, dbHelper.GetTransactionsInDay(day, month, year));
        dayAdapterViewHolder.transactionList.setAdapter(dayItemAdapter);
        setListViewHeightBasedOnItemsForDays(dayAdapterViewHolder.transactionList);

    }

    private class dayAdapterViewHolder {
        TextView dayTitle;
        ListView transactionList;

    }

    private class DayItemAdapter extends CursorAdapter {
        public DayItemAdapter(Context context, Cursor c) {
            super(context, c);
        }

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            View view = LayoutInflater.from(context).inflate(R.layout.transaction_item, parent, false);
            DayAdapterViewHolder viewHolder = new DayAdapterViewHolder();
            viewHolder.icon = (ImageView) view.findViewById(R.id.transactionIcon);
            viewHolder.amount = (TextView) view.findViewById(R.id.amountText);
            viewHolder.category = (TextView) view.findViewById(R.id.categoryText);
            view.setTag(viewHolder);
            return view;
        }

        @Override
        public void bindView(View view, final Context context, Cursor cursor) {
            DatabaseHelper dbHelper = new DatabaseHelper(context);
            final Utils utils = new Utils();
            DayAdapterViewHolder viewHolder = (DayAdapterViewHolder) view.getTag();
            TextView category = viewHolder.category;
            final ImageView icon = viewHolder.icon;
            final int type = cursor.getInt(cursor.getColumnIndexOrThrow(dbHelper.COLUMN_ENTRY_TYPE));
            final int entryCategory = cursor.getInt(cursor.getColumnIndexOrThrow(dbHelper.COLUMN_ENTRY_CATEGORY));
            final int entrySubCategory = cursor.getInt(cursor.getColumnIndexOrThrow(dbHelper.COLUMN_ENTRY_SUBCATEGORY));
            if (entrySubCategory == -1) {
                category.setText(utils.expense_and_incomeCategories(context).get(type).get(entryCategory).getCategoryName());
                icon.post(new Runnable() {
                    @Override
                    public void run() {
                        icon.setImageResource(utils.expense_and_incomeCategories(context).get(type).get(entryCategory).getCategoryIcon());
                    }
                });
            } else {
                category.setText(utils.expense_and_incomeCategories(context).get(type).get(entryCategory).getSubCategories().get(entrySubCategory).getSubCategoryName());
                icon.post(new Runnable() {
                    @Override
                    public void run() {
                        icon.setImageResource(utils.expense_and_incomeCategories(context).get(type).get(entryCategory).getSubCategories().get(entrySubCategory).getSubCategoryIcon());
                    }
                });
            }
            String amountString;
            TextView amountText = viewHolder.amount;
            double amount = cursor.getDouble(cursor.getColumnIndexOrThrow(dbHelper.COLUMN_AMOUNT));
            if (amount > 1000 * 1000) {
                String editedAmount;
                DecimalFormat df = new DecimalFormat("#.#");
                df.setRoundingMode(RoundingMode.HALF_UP);
                editedAmount = df.format(amount / (1000 * 1000));
                amountString = editedAmount + "Mn";
            } else if (amount > 1000) {
                String editedAmount;
                DecimalFormat df = new DecimalFormat("#.#");
                df.setRoundingMode(RoundingMode.HALF_UP);
                editedAmount = df.format(amount / (1000));
                amountString = editedAmount + "K";
            } else {
                DecimalFormat df = new DecimalFormat("#.#");
                df.setRoundingMode(RoundingMode.HALF_UP);
                amountString = df.format(amount);
            }
            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
            amountText.setText(dbHelper.getCountryABV(preferences.getLong("country", 1)) + " " + amountString);
        }

        private class DayAdapterViewHolder {
            ImageView icon;
            TextView category;
            TextView amount;

        }

    }

这里是setListViewHeightBasedOnItems()方法

public boolean setListViewHeightBasedOnItems(ListView listView) {

    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter != null) {

        int numberOfItems = listAdapter.getCount();

        // Get total height of all items.
        int totalItemsHeight = 0;
        for (int itemPos = 0; itemPos < numberOfItems; itemPos++) {
            View item = listAdapter.getView(itemPos, null, listView);
            item.measure(0, 0);
            totalItemsHeight += item.getMeasuredHeight();
        }

        // Get total height of all item dividers.
        int totalDividersHeight = listView.getDividerHeight() *
            (numberOfItems - 1);

        // Set list height.
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalItemsHeight + totalDividersHeight;
        listView.setLayoutParams(params);
        listView.requestLayout();

        return true;

    } else {
        return false;
    }

}

0 个答案:

没有答案