mardi 4 août 2015

saving programatically generated value of edittext in a static class

I have an edittext field in a tab which is given a unique autogenerated value when the activity is created. I want this value passed to a static class as soon as the value is put in the edittext so that on switching tabs, the value can be called from the static class. My problem is i tried saving the value to the static class using the following code:

 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();
         }
        }

But it kept changing whenever the user returns to the 1st tab (where the value is generated) or it would not be displayed at all. I want it to save the generated value once irrespective of whether user returns to the 1st Tab.

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();
         }
        }

        );
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire