Search code examples
androidxmlstringcredit-card-track-data

Android How to change String with Edittext and reuse in same activity


I have a serious problem, I have a card swiper app that passes all of card data threw a xml (the way the xml is structured in my code was writted by the sdk provider). I need to make it possible to do a manual entry using 2 EditText (1 for Card number and 1 for ExpDate) and then pass that data to the same Activity (OR A NEW Activity) to be used in the xml that processes the order. Can this be done? How can this be done? Below is my Code - and edits or corrections will be very helpful ... Thank you in advance.

String tranXmlFmt =  
        "<?xml version=\"1.0\"?><TStream>" 
      + "<Transaction>" 
      + "<IpAddress>66.173.160.125</IpAddress>" 
      + "<MerchantID>002649</MerchantID> " 
      + "<OperatorID>50</OperatorID>" 
      + "<TranType>Credit</TranType>" 
      + "<CardType>VISA</CardType> " 
      + "<TranCode>Sale</TranCode> " 
      + "<OperatorID>55</OperatorID> "
      + "<InvoiceNo>100001</InvoiceNo>" 
      + " <RefNo>100001</RefNo> " 
      + "<PartialAuth>Allow</PartialAuth>"
      + "<Account>" 
      + "<AcctNo>4104891389583005</AcctNo>"  
      + "</Account> " 
      + "<ExpDate>0214</ExpDate>"
      + "<Amount><Purchase>13.00</Purchase></Amount>" 
      + "<UserTraceData>GBTE130116</UserTraceData>"
      + "</Transaction> " 
      + "</TStream>";
String initXml = "" 
    + "<?xml version=\"1.0\"?>" 
    + "<TStream><Admin>" 
    + "    <MerchantID>002649</MerchantID>"
           + "    <TerminalID>002</TerminalID><TranCode>SecureDeviceInit</TranCode>"
           + "    <PadType>None</PadType>" + "    <TranType>Setup</TranType>"
           + "    <SecureDevice>UniMagSecureMagDsi</SecureDevice>" 
           + "    <ComPort>1</ComPort>" 
           + "  </Admin>"
           + "</TStream>";
    '

Help

Button btnNextScreen = (Button) findViewById(R.id.btnNextScreen);
    //Listening to button event
    btnNextScreen.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) { 
            //Starting a new Intent



        }
    });
      Button btnInit = (Button) findViewById(R.id.btnInit);
    btnInit.setOnClickListener(new View.OnClickListener() {

        /**
         * initialize epay client 
         */
        @Override
        public void onClick(View v) {
            libReady = true;
            ca = new ClientActivity(getApplication(), readerType);
            Log.d("MAINACTIVITY", "reader type: " + readerType.toString());
            initReturn = ca.initialize(initXml);
            Log.d("MAINACTIVITY", "init return: " + initXml);
            libReady = true;
            EditText et = (EditText) findViewById(R.id.editXml);
            et.setText(initReturn);
            Log.d("MAINACTIVITY", "Library Loaded");

        }
    });

    Button btnClose = (Button) findViewById(R.id.Close_button);
    btnClose.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            LineDisplay ld = new LineDisplay();
            ld.open(DeviceCommon.DEVICE_MODE_COMMON, null);

            ld.clear();             
            ld.setBacklight(LineDisplay.LCD_BACKLIGHT_OFF);

            ld.close();         
            finish();
        }
    });

I just need to add 2 Edittext and change the AcctNo and ExpDate STRING to be used in this same activity. please any help will be greatful.


Solution

  • You can get data from edittext and pass it to your secondactivtiy using intents.

         String s1,s2
         EditText ed1= (EditText)findViewById(R.id.editext); 
         EditText ed2= (EditText)findViewById(R.id.editext2);
    
             on Button click
    
         s1 = ed1.getText().toString(); 
         s2 = ed2.getText().toString();
         ed1.setText("");  
         ed2.setText("");  
    

    In same activity. Can keep other tags as above.

         String xml ="<AcctNo>"+s1+"</AcctNo>"  
              + "<ExpDate>"+s2+"</ExpDate>"; 
    
                 OR
    
        Intent i= new Intent("com.example.secondactivity");
        i.putExtra("key1",s1);   
        i.putExtra("key2",s2);  
        startActivity(i);
    

    In Second Activity you can retrieve the values.

        String value1,value2;   
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
        value1 = extras.getString("key1"); 
        value2 = extras.getString("key2"); 
        }
    

     Can keep other tags as above.

        String xml ="<AcctNo>"+value1+"</AcctNo>"  
              + "<ExpDate>"+value2+"</ExpDate>";
    

    EDIT :

    acitivity_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >
    
    <EditText
        android:id="@+id/editText1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="33dp"
        android:ems="10" >
    </EditText>
    <EditText
        android:id="@+id/editText2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:ems="10" />
    <TextView
       android:id="@+id/textView1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_gravity="center"
       android:layout_marginTop="36dp"
       android:text="TextView" />
     <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:text="TextView" />
      <TextView
          android:id="@+id/textView3"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="center"
          android:layout_marginTop="20dp"
          android:text="TextView" />
        <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:text="Button" />
        </LinearLayout>
    

    MainActivity

       public class MainActivity extends Activity {
    
    EditText ed1,ed2;
    TextView tv1,tv2,tv3;
    Button b;
    String s1;
    String s2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ed1= (EditText) findViewById(R.id.editText1);
        ed2= (EditText) findViewById(R.id.editText2);
        tv1 = (TextView) findViewById(R.id.textView1);
        tv2 = (TextView) findViewById(R.id.textView2);
        tv3 = (TextView) findViewById(R.id.textView3);
        b= (Button) findViewById(R.id.button1);
        b.setOnClickListener(new OnClickListener()
        {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
    
                s1= ed1.getText().toString();
                s2= ed2.getText().toString();
                System.out.println("................"+s1);
                System.out.println("................"+s2);
                tv1.setText(s1);
                tv2.setText(s2);
                String xml ="<AcctNo>"+s1+"</AcctNo>"  
                          + "<ExpDate>"+s2+"</ExpDate>";
                System.out.println("................"+xml);
                tv3.setText(xml);
                ed1.setText("");
                ed2.setText("");
            }
    
        });
    
    }
    
    }
    

    As you can see it works. See the snap shot. I have 3 text views, 2 editext's and a button. After user types in editext's i click the button and i set the values to textview. As you can see the values reflects in string xml which is the result set in textview3.

    enter image description here