Spinner选择了Item Issue

时间:2017-07-25 11:07:47

标签: android

我已经制作了两个片段。 付款片段和其他是添加付款片段。 付款片段包含商店Spinner女巫多家商店用户选择商店付款历史显示如果添加付款按钮点击添加片段屏幕显示添加付款并提交付款后将付款屏幕返回到片段列表。我的问题是在片段列表屏幕后面,所选的shopSpinner值更改显示第一个微调器值未显示用户选择的微调器。 PaymentFragemt

public class PaymentFragment extends Fragment {
    View myView;
    ArrayList<HashMap<String, String>> getShop;
    String shopId;
    Fragment fragment;
    int totalpayment,shopOrderTotalPayment;
    ArrayList<HashMap<String, String>> getPaypent;
    ListPaymentHistoryAdapter listPaymentHistoryAdapter;
    Spinner spinnerShop;
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        myView = inflater.inflate(R.layout.payment_listview, container, false);
        final Context context = myView.getContext();
        final DatabaseHelper myDBHelper = DatabaseHelper.getInstance(context);
        spinnerShop = (Spinner) myView.findViewById(R.id.searhshop);
        final TextView totalpaymentTextView=(TextView) myView.findViewById(R.id.totalPayment);
        final TextView totalOrderpaymentTextView=(TextView) myView.findViewById(R.id.totalOrderPayment);
        final TextView leftpaymentTextView=(TextView) myView.findViewById(R.id.leftpayment);
        final ListView lvpayment=(ListView)myView.findViewById(R.id.payment_listview);
        getShop=JavaHelper.setShopSpinner(spinnerShop,myView);
        spinnerShop.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                shopId= getShop.get(position).get(Shop.getColId());
                /*Get total Payment*/
                totalpayment= JavaHelper.shoptotapayment(Integer.parseInt(shopId),context);
               /* Get Shop Order Payment*/
                shopOrderTotalPayment=JavaHelper.shopOrderTotaPayment(Integer.parseInt(shopId),context);
                /*Set Left Payment*/
                leftpaymentTextView.setText(Integer.toString(shopOrderTotalPayment-totalpayment));
                totalpaymentTextView.setText(Integer.toString(totalpayment));
                totalOrderpaymentTextView.setText(Integer.toString(shopOrderTotalPayment));
                /* Show Payment List History*/
                String[] projectionPayment = {
                        Payment.getColpayment(),
                        Payment.getColcreated_at()
                };
                String wherclause=Payment.getColshopId()+"="+shopId;
                getPaypent= myDBHelper.thread_safe_select(Payment.getTablName(),projectionPayment,wherclause,null,null,null);
                if (getPaypent!=null){
                    listPaymentHistoryAdapter=new ListPaymentHistoryAdapter(myView.getContext(), getPaypent);
                    lvpayment.setAdapter(listPaymentHistoryAdapter);
                }
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                Toast.makeText(context,"NotSelectClick",Toast.LENGTH_SHORT).show();
            }
        });
        ((Button) myView.findViewById(R.id.paymentAdd)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            Bundle bundle=new Bundle();
            bundle.putInt("shop_id",Integer.parseInt(shopId));
            fragment=new AddPayment();
            fragment.setArguments(bundle);
            FragmentManager fragmentManager=getFragmentManager();
            String title = "Add Payment";
            if (fragment != null) {
                fragmentManager.beginTransaction().replace(R.id.content_frame,fragment).commit();
            }
            }
        });
        return myView;
    }
}

添加付款片段

public class AddPayment extends Fragment {
    View myView;
    Context context;
    DatabaseHelper myDBHelper;
    Fragment fragment = null;;
    int colorID;
    Bundle bundle;
    long leftPayment;
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        myView=inflater.inflate(R.layout.payment_add,container,false);
        myDBHelper = DatabaseHelper.getInstance(context);
        context=myView.getContext();
        myDBHelper = DatabaseHelper.getInstance(context);
        final EditText addPaymentEditText=(EditText) myView.findViewById(R.id.paymentAdd);
        final TextView leftpaymentText=(TextView) myView.findViewById(R.id.leftpayment) ;
        bundle=getArguments();

        if (bundle!=null){
            final int shopId=bundle.getInt("shop_id");
            leftPayment=JavaHelper.shopOrderTotaPayment(shopId,context)-JavaHelper.shoptotapayment(shopId,context);
            leftpaymentText.setText(Long.toString(leftPayment));
            ((Button) myView.findViewById(R.id.addPaymentbtn)).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String getPayment=addPaymentEditText.getText().toString();
                    Payment payment=new Payment(getPayment,shopId,JavaHelper.getCurrentTimeDate());
                    if (myDBHelper.thread_safe_insert(Payment.getTablName(),payment.getContentValues() ,context) != -1) {
                        Toast.makeText(context, "Add Success", Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(context, "Some thing wrong", Toast.LENGTH_LONG).show();
                    }
                    FragmentManager fragmentManager=getFragmentManager();
                    fragment=new PaymentFragment();
                    String title = "payment";
                    JavaHelper.hideSoftKeyboard(getActivity());
                    if (fragment != null) {
                        fragmentManager.beginTransaction().replace(R.id.content_frame,fragment).commit();
                    }
                }
            });
        }
        addPaymentEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                Log.v("beforetextChange","textchange event");
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                Log.v("ontextextChange","textchange event");
            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (!(editable.length()<=0)){
                    Long enterPayment=Long.parseLong(editable.toString());
                    leftpaymentText.setText(Long.toString(leftPayment-enterPayment));
                }
                else {
                    int length=editable.length();
                    Log.v("length",Integer.toString(length));
                }
            }
        });
        return myView;
    }
}

0 个答案:

没有答案