首页 > 解决方案 > 尝试为两个布局设置游标适配器,其中一个数据库使用 if 语句定义

问题描述

    class TransactionCursorAdapter extends CursorAdapter {

    public TransactionCursorAdapter(Context context, Cursor cursor) {

        super (context, cursor, 0);

    }


    private final DateFormat DATE_FORMATTER = SimpleDateFormat.getDateInstance ();

    static class ViewHolder {

        public LinearLayout linear_expense1;

        public LinearLayout linear_revenue1;

        TextView nameField;

        TextView valueField;

        TextView dateField;

        TextView budgetField;

        ImageView receiptIcon;

        TextView note;

        View noteLayout;

        TextView revenuenameField;

        TextView phoneField;

        TextView date1Field;

        TextView budget1Field;

        ImageView receiptIcon1;

        TextView comment;

        View commentLayout;

    }


    private int _transactionId;

    private int _type;

    private boolean _updateTransaction;

    private boolean _viewTransaction;

    private static final String TAG = "BudgetWatch";

    private static final int REQUEST_IMAGE_CAPTURE = 1;

    private static final int PERMISSIONS_REQUEST_CAMERA = 2;

    static final String ACTION_NEW_EXPENSE = "ActionAddExpense";

    static final String ACTION_NEW_REVENUE = "ActionAddRevenue";

    private void extractIntentFields(Intent intent) {

        final Bundle b = intent.getExtras ();

        String action = intent.getAction ();

        if (b != null) {

            _transactionId = b.getInt ("id");

            _type = b.getInt ("type");

            _updateTransaction = b.getBoolean ("update", false);

            _viewTransaction = b.getBoolean ("view", false);

        } else if (action != null) {

            _updateTransaction = false;

            _viewTransaction = false;

            if (action.equals (ACTION_NEW_EXPENSE)) {

                _type = DBHelper.TransactionDbIds.EXPENSE;

            } else if (action.equals (ACTION_NEW_REVENUE)) {

                _type = DBHelper.TransactionDbIds.REVENUE;

            } else {

                Log.d (TAG, "Unsupported action '" + action + "', bailing");

                finish ();
            }
        } else {
            Log.d (TAG, "Launched TransactionViewActivity without arguments, bailing");

            finish ();
        }
    }

    private void finish() {

    }

    // The newView method is used to inflate a new view and return it,

    // you don't bind any data to the view at this point.

    @Override

    public View newView(Context context, Cursor cursor, ViewGroup parent)
    {

    View view = LayoutInflater.from(context).inflate(R.layout.transaction_layout, parent, false);

        ViewHolder holder = new ViewHolder();

        holder.linear_expense1 = (LinearLayout) view.findViewById (R.id.linear_expense1);

        holder.linear_revenue1 = (LinearLayout) view.findViewById (R.id.linear_revenue1);

        if (_type == DBHelper.TransactionDbIds.EXPENSE) {

            holder.linear_expense1.setVisibility(View.VISIBLE);

            holder.linear_revenue1.setVisibility(View.GONE);
        }

        if (_type == DBHelper.TransactionDbIds.REVENUE) {

            holder.linear_expense1.setVisibility(View.GONE);

            holder.linear_revenue1.setVisibility(View.VISIBLE);

        } else {

            holder.linear_expense1.setVisibility(View.VISIBLE);

            holder.linear_revenue1.setVisibility(View.GONE);
        }

        holder.nameField = (TextView) view.findViewById(R.id.name);

        holder.valueField = (TextView) view.findViewById(R.id.value)
;
        holder.dateField = (TextView) view.findViewById(R.id.date);

        holder.budgetField = (TextView) view.findViewById(R.id.budget);

        holder.receiptIcon = (ImageView) view.findViewById(R.id.receiptIcon);

        holder.note = (TextView) view.findViewById(R.id.note);

        holder.noteLayout = view.findViewById(R.id.noteLayout);

        holder.revenuenameField = (TextView) view.findViewById(R.id.revenuename);

        holder.phoneField = (TextView) view.findViewById(R.id.phone);

        holder.date1Field = (TextView) view.findViewById(R.id.date1);

        holder.budget1Field = (TextView) view.findViewById(R.id.budget1);

        holder.receiptIcon1 = (ImageView) view.findViewById(R.id.receiptIcon1);

        holder.comment = (TextView) view.findViewById(R.id.comment);

        holder.commentLayout = view.findViewById(R.id.commentLayout);

        view.setTag(holder);

        return view;
    }

    // The bindView method is used to bind all data to a given view

    // such as setting the text on a TextView.

    @Override

    public void bindView(View view, Context context, Cursor cursor)
    {
        ViewHolder holder = (ViewHolder)view.getTag();

        if (_type == DBHelper.TransactionDbIds.EXPENSE) {

            holder.linear_expense1.setVisibility(View.VISIBLE);

            holder.linear_revenue1.setVisibility(View.GONE);
        }

        if (_type == DBHelper.TransactionDbIds.REVENUE) {

            holder.linear_expense1.setVisibility(View.GONE);

            holder.linear_revenue1.setVisibility(View.VISIBLE);

        } else {

            holder.linear_expense1.setVisibility(View.VISIBLE);

            holder.linear_revenue1.setVisibility(View.GONE);
        }

        // Extract properties from cursor

        Transaction transaction = Transaction.toTransaction(cursor);

        // Populate fields with extracted properties

        holder.nameField.setText(transaction.description);

        holder.valueField.setText(String.format(Locale.US, "%.2f", transaction.value));

        // Populate fields with extracted properties

        holder.revenuenameField.setText(transaction.description);

        holder.phonenameField.setText(transaction.phone);

        holder.budgetField.setText(transaction.budget);

        holder.dateField.setText(DATE_FORMATTER.format(transaction.dateMs));

        if(transaction.receipt.isEmpty())

        {
            holder.receiptIcon.setVisibility(View.GONE);

        }

        else

        {
            holder.receiptIcon.setVisibility(View.VISIBLE);
        }

        if(transaction.note.isEmpty())

        {
            holder.noteLayout.setVisibility(View.GONE);

            holder.note.setText("");
        }

        else

        {
            holder.noteLayout.setVisibility(View.VISIBLE);

            holder.note.setText(transaction.note);
        }

        if(transaction.comment.isEmpty())

        {
            holder.commentLayout.setVisibility(View.GONE);

            holder.comment.setText("");
        }

        else

        {
            holder.commentLayout.setVisibility(View.VISIBLE);


            holder.comment.setText(transaction.note);

        }

    }

}

--------- 崩溃开始

2020-05-27 15:42:14.929 4911-4911/protect.budgetwatch E/AndroidRuntime: FATAL EXCEPTION: main

    Process: protect.budgetwatch, PID: 4911

    java.util.IllegalFormatConversionException: f != java.lang.String

        at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4135)

        at java.util.Formatter$FormatSpecifier.printFloat(Formatter.java:2845)

        at java.util.Formatter$FormatSpecifier.print(Formatter.java:2792)

        at java.util.Formatter.format(Formatter.java:2491)

        at java.util.Formatter.format(Formatter.java:2426)

        at java.lang.String.format(String.java:2670)

        at protect.budgetwatch.TransactionCursorAdapter.bindView(TransactionCursorAdapter.java:173)

        at android.widget.CursorAdapter.getView(CursorAdapter.java:289)

        at android.widget.AbsListView.obtainView(AbsListView.java:2362)

        at android.widget.ListView.makeAndAddView(ListView.java:1970)

        at android.widget.ListView.fillDown(ListView.java:704)

        at android.widget.ListView.fillFromTop(ListView.java:765)

        at android.widget.ListView.layoutChildren(ListView.java:1744)

        at android.widget.AbsListView.onLayout(AbsListView.java:2161)

        at android.view.View.layout(View.java:17523)

        at android.view.ViewGroup.layout(ViewGroup.java:5612)

        at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)

        at android.view.View.layout(View.java:17523)

        at android.view.ViewGroup.layout(ViewGroup.java:5612)

        at androidx.viewpager.widget.ViewPager.onLayout(ViewPager.java:1775)

        at android.view.View.layout(View.java:17523)

        at android.view.ViewGroup.layout(ViewGroup.java:5612)

        at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)

        at android.view.View.layout(View.java:17523)

        at android.view.ViewGroup.layout(ViewGroup.java:5612)

        at 

com.google.android.material.appbar.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:148)

        at 

com.google.android.material.appbar.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:43)

        at 

com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.jav
a:1892)

        at androidx.coordinatorlayout.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:918)

        at android.view.View.layout(View.java:17523)

        at android.view.ViewGroup.layout(ViewGroup.java:5612)

        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)

        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)

        at android.view.View.layout(View.java:17523)

        at android.view.ViewGroup.layout(ViewGroup.java:5612)

        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)

        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)

        at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)

        at android.view.View.layout(View.java:17523)

        at android.view.ViewGroup.layout(ViewGroup.java:5612)

        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)

        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)

        at android.view.View.layout(View.java:17523)

        at android.view.ViewGroup.layout(ViewGroup.java:5612)

        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)

        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)

        at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)

        at android.view.View.layout(View.java:17523)

        at android.view.ViewGroup.layout(ViewGroup.java:5612)

        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)

        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)

        at com.android.internal.policy.DecorView.onLayout(DecorView.java:724)

        at android.view.View.layout(View.java:17523)

        at android.view.ViewGroup.layout(ViewGroup.java:5612)

        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2342)

        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2069)

        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246)

        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6301)

        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)

        at android.view.Choreographer.doCallbacks(Choreographer.java:683)

        at android.view.Choreographer.doFrame(Choreographer.java:619)

2020-05-27 15:42:14.930 4911-4911/protect.budgetwatch E/AndroidRuntime:     at 

android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)

        at android.os.Handler.handleCallback(Handler.java:751)

        at android.os.Handler.dispatchMessage(Handler.java:95)

        at android.os.Looper.loop(Looper.java:154)

        at android.app.ActivityThread.main(ActivityThread.java:6077)

        at java.lang.reflect.Method.invoke(Native Method)

        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)

        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

标签: javaandroid

解决方案


推荐阅读