Notification for keyboard

柔情痞子 提交于 2019-12-11 18:27:30

问题


I have a variable 'balance' which I'm initializing to '0' onClick of a button which calls my custom adapter 'MyAdpater'. I've an EditText in my activity. When I hide/show the keyboard, I don't get notified about it. I've vainly tried configChnage Method already.

 @Override
 public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    Toast.makeText(Ledger.this, "Config Change has been called.", Toast.LENGTH_SHORT).show();
    // Checks whether a hardware or on-screen keyboard is available
    balance = 0;
        ourCursor = db.getLedger(et.getText().toString(), from.getText()
                .toString(), to.getText().toString());

        id.clear();
        adapter = new MyAdapter(Ledger.this, ourCursor, false);
        lv.setAdapter(adapter);
}

(This method isn't called when keyboard (dis)appears because the Toast doesn't appear.)

This is my code

class MyAdapter extends CursorAdapter {
    public MyAdapter(Context context, Cursor c, boolean autoRequery) {
        super(context, c, autoRequery);
        // TODO Auto-generated constructor stub
        balance = 0;
    }

    @Override
    public void bindView(View view, Context ctxt, Cursor c) {
        // TODO Auto-generated method stub

        TextView tv1 = (TextView) view.findViewById(R.id.tvAB1);
        TextView tv2 = (TextView) view.findViewById(R.id.tvAB2);
        TextView tv3 = (TextView) view.findViewById(R.id.tvAB3);
        TextView tv4 = (TextView) view.findViewById(R.id.tvAB4);
        TextView tv5 = (TextView) view.findViewById(R.id.tvAB5);
        String date = c.getString(1);
        tv1.setText(date.substring(6, 8) + "/" + date.substring(4, 6) + "/"
                + date.substring(0, 4));
        tv2.setText("" + c.getDouble(3));
        tv3.setText("" + c.getDouble(4));
        balance = balance + c.getDouble(4) - c.getDouble(3);
        tv4.setText("" + (balance));
        tv5.setText((c.getDouble(3) > c.getDouble(4) ? "CR." : "DR."));
    }

    @Override
    public View newView(Context ctxt, Cursor c, ViewGroup parent) {
        // TODO Auto-generated method stub
        LayoutInflater inflater = getLayoutInflater();
        return inflater.inflate(
                R.layout.text_view_for_list_view_account_balance, parent,
                false);
    }
}

It doesn't initialize the value of my variable 'balance = 0' as I show/hide the keyboard & therefore, the value of balance just keeps on increasing every time I show/hide the keyboard. I want to set balance = 0 whenever I show/hide the keyboard. Is there any method to do this?

Please help. Thanks. :)


回答1:


In World of ListView (or TurboCharge Your UI) Android's Romain Guy states that you cannot rely on the Adapter's build order. It may build top-down, bottom-up, or start somewhere in the middle and travel in either direction; this design choice relates to efficiency.

So you need to find a way to calculate balance that is not reliant on the build order...


Also your current Adapter does not support scrolling, since newView() is not called for every row in your database. The number of time newView() gets called in your ListView (or Spinner, etc) relates to how many rows fit on the user's screen.



来源:https://stackoverflow.com/questions/14525678/notification-for-keyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!