在静态类中保存编程文本的编程生成值

时间:2015-08-04 14:07:57

标签: android tabs android-edittext

我在选项卡中有一个edittext字段,在创建活动时,该字段被赋予唯一的自动生成值。我希望只要将值放入edittext中,就将此值传递给静态类,以便在切换选项卡上,可以从静态类调用该值。 我的问题是我尝试使用以下代码将值保存到静态类:

 invoiceNo.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {
            //GlobalApp.data().id = invoiceNo.getText().toString();
        }

        public void beforeTextChanged(CharSequence s, int a, int b, int c){
            GlobalApp.data().id = invoiceNo.getText().toString();
        }

         public void onTextChanged(CharSequence s, int a, int b, int c){
            // GlobalApp.data().id = invoiceNo.getText().toString();
         }
        }

但是只要用户返回第一个选项卡(生成值),它就会不断变化,或者根本不会显示。无论用户是否返回第一个选项卡,我希望它保存生成的值。

public class clientFragmentTab  extends Fragment  {

ArrayList<String> saleRecord;
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
String formattedDate = df.format(c.getTime());
EditText currDate, invoiceNo , vehicle,territory ;
//Spinner clientName, territory;
View rootView = null;
int invoice_id = 0;
String invoice_no;
public String[] item = new String[] {"Please search..."};
public String territory1;
CustomAutoCompleteView myAutoComplete;
ProductsDbHelper db;

// adapter for auto-complete
ArrayAdapter<String> myAdapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    rootView = inflater.inflate(R.layout.client_layout, container, false);



    return rootView;
}

public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);
    myAutoComplete = (CustomAutoCompleteView) rootView.findViewById(R.id.myautocomplete);

    // add the listener so it will tries to suggest while the user types
    myAutoComplete.addTextChangedListener(new CustomAutoCompleteTextChangedListener(this, getActivity()));

    db = new ProductsDbHelper(getActivity());
    // set our adapter
    myAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_dropdown_item_1line, item);
    myAutoComplete.setAdapter(myAdapter);
    currDate = (EditText) rootView.findViewById(R.id.editText4);
    //clientName = (Spinner) rootView.findViewById(R.id.spinner);
    invoiceNo = (EditText) rootView.findViewById(R.id.editText3);
    vehicle = (EditText) rootView.findViewById(R.id.editText6);
    territory = (EditText) rootView.findViewById(R.id.editText9);invoice_id = UniqueRandomNumbers();


    invoice_no = "invoice_" + invoice_id;

    currDate.setText(formattedDate);
    invoiceNo.setText(invoice_no);




    //List<String> list = new ArrayList<String>();
    //list.add("Select Client");
    //list.add("Item 2");
    //list.add("Item 3");
    //list.add("Item 4");
    //list.add("Item 5");

    //ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_dropdown_item , list);
    //adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    //clientName.setAdapter(adapter);





   // if(invoiceNo.getText().toString() != ""){

   // }

    invoiceNo.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {
            //GlobalApp.data().id = invoiceNo.getText().toString();
        }

        public void beforeTextChanged(CharSequence s, int a, int b, int c){
            GlobalApp.data().id = invoiceNo.getText().toString();
        }

         public void onTextChanged(CharSequence s, int a, int b, int c){
            // GlobalApp.data().id = invoiceNo.getText().toString();
         }
        }

        );
}

1 个答案:

答案 0 :(得分:1)

你似乎正在接近这一切都是错误的,当你觉得你不得不强迫它时,它通常表明项目架构存在问题。解决方案应该类似于以下内容:

1. onCreate generate that id and store it in a *member variable*, lets call it mInvoiceId
2. onCreateView or onViewCreated do invoiceNo.setText(mInvoiceId)
3. if it's still required, in onTextChanged do mInvoiceId = s; (the 's' param should be the same result as invoiceNo.getText().toString() I believe)
4. if you're really set on using a static class you can replace any use of mInvoiceId  with GlobalApp.data().id but static classes are usually bad. 
   - You can pass things between fragments correctly using saveInstanceState or intents with extras (not sure what you want to do)

我希望这能让你朝着正确的方向前进