mardi 27 octobre 2015

Not sure what setText should write

In my claims.xml, I have 2 + button and they used to open the radio button dialog. The textView in these two button are used to get the user input from other activities by using startActivityForResult().

enter image description here

claims.xml

 <RelativeLayout ....>

       <Button
            android:id="@+id/button10"
            android:layout_width="41dp"
            android:layout_height="40dp"
            android:background="#454545"
            android:text="+"
            android:textColor="#FFFFFF"
            android:textSize="30sp"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/textView49"
            android:layout_alignParentTop="true"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_toLeftOf="@+id/button10"
            android:layout_toStartOf="@+id/button10" />

    </RelativeLayout>

 <RelativeLayout...>

        <Button
            android:id="@+id/button9"
            android:layout_width="41dp"
            android:layout_height="40dp"
            android:background="#454545"
            android:text="+"
            android:textColor="#FFFFFF"
            android:textSize="30sp"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/textView51"
            android:layout_alignParentTop="true"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_toLeftOf="@+id/button9"
            android:layout_toStartOf="@+id/button9" />
    </RelativeLayout>

Below is my code for Claims.java

Claims.java

   Button button1 = (Button) claims.findViewById(R.id.button10);
   Button button = (Button) claims.findViewById(R.id.button9);

  button1.setOnClickListener(listener);
  button.setOnClickListener(listener);

    c=(TextView)claims.findViewById(R.id.textView49);
    d=(TextView)claims.findViewById(R.id.textView51);

  View.OnClickListener listener = new View.OnClickListener() {
            public void onClick(View v) {
                AlertDialogRadio();

            }
        };

  public void AlertDialogRadio() {
        final CharSequence[] ClaimsModel = {"Project", "Petrol", "Car };

        AlertDialog.Builder alt_bld = new AlertDialog.Builder(getActivity());
        alt_bld.setTitle("Select a Claims");
        alt_bld.setSingleChoiceItems(ClaimsModel, -1, new DialogInterface
                .OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                if (item == 0) {
                    Intent intent = new Intent(getActivity().getApplicationContext(), Project1.class);
                    startActivityForResult(intent, 0);
                } else if (item == 1) {
                    Intent intent = new Intent(getActivity().getApplicationContext(), Petrol.class);
                    startActivityForResult(intent, 1);
                } else if (item == 2) {
                    Intent intent = new Intent(getActivity().getApplicationContext(), CarMainten.class);
                    startActivityForResult(intent, 2);
                }
                }
                dialog.dismiss();

            }
        });
        AlertDialog alert = alt_bld.create();
        alert.show();

    }

Assume the user select Petrol on the first + button then follow by Project.

In Petrol.java, it has an editText for user to key in the amount, and a save button. Once the same button get clicked, returnIntent.putExtra("text", text); will be called and back to Claims.java

Same as Project.java,it has an editText for user to key in the amount, and a save button.

Continue in Claims.java....

    @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch(requestCode)
        {
            case 0:
                String result = data.getStringExtra("text");
                c.setText( "RM " + result); 
                break;

            case 1:
                String result1=data.getStringExtra("text");
                c.setText( "RM " + result1);
                break;


            case 2:
                String result2 = data.getStringExtra("text");
                c.setText("RM " + result2);
                break;

         }
}

The things that confused me is that I don't know how to write the setText. I came out with a concept, if button1 get clicked, go to selected page, then c.setText(result) and if button get clicked, go to selected page, then d.setText(result). But where can I put the if-else statement? Anyone can help me or have an idea ? Thanks and sorry for my poor English.

Aucun commentaire:

Enregistrer un commentaire