質問

Hi In My Application I Designed one popup type of registration page.In That I added field student name,class,roll no,parent name,Phone number.

registration.java:

public class PopupRegistration extends Activity implements OnItemSelectedListener {

    private String[] state = { "class","1", "2", "3",
             "4", "5", "6", "7","8", "9"};
    private String[] state1 = { "RollNO","1", "2", "3",
             "4", "5", "6", "7","8", "9"};


 public String log;
 public DatabaseHandlers db;
 List<String> list;
 GSfeedback cn;

 EditText etName,etClass,etrollno,etparentname,etphno; 
 Button btnregister;

 Spinner spinnerOsversions,spinnerOsversions1;


 String PHONE_REGEX;

 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.popup_registration);

  db = new DatabaseHandlers(this);

  etName=(EditText)findViewById(R.id.etName); 
  etClass=(EditText)findViewById(R.id.etClass);
     etrollno=(EditText)findViewById(R.id.etrollno);
     etparentname=(EditText)findViewById(R.id.etparentname);
     etphno=(EditText)findViewById(R.id.etphno);

     btnregister=(Button)findViewById(R.id.registerbutton);

     PHONE_REGEX = "[0-9]+";
    // spinner = (Spinner) findViewById(R.id.spinner);

     etClass.setVisibility(View.GONE);
     etrollno.setVisibility(View.GONE);
     spinnerOsversions = (Spinner) findViewById(R.id.spinner);

     spinnerOsversions1 = (Spinner) findViewById(R.id.spinner1);

     ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this,
             android.R.layout.simple_spinner_item, state);

     adapter_state.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

     spinnerOsversions.setAdapter(adapter_state);
     spinnerOsversions.setOnItemSelectedListener(this);


     ArrayAdapter<String> adapter_state1 = new ArrayAdapter<String>(this,
             android.R.layout.simple_spinner_item, state1);

     adapter_state1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

     spinnerOsversions1.setAdapter(adapter_state1);
     spinnerOsversions1.setOnItemSelectedListener(this);


     btnregister.setOnClickListener(new View.OnClickListener() 
        {
        public void onClick(View v) {
          if(!validate()) {
              //Toast.makeText(getApplicationContext(), "Not Valid", Toast.LENGTH_SHORT).show();
             } else {
            if(!validates()) {
              // Toast.makeText(getApplicationContext(), "Not valid", Toast.LENGTH_SHORT).show(); 
              } else {
               //Toast.makeText(getApplicationContext(), "valid", Toast.LENGTH_SHORT).show();
               String studentname=etName.getText().toString(); 
                String classid= etClass.getText().toString();
                String rollno=etrollno.getText().toString(); 
               String parentname=etparentname.getText().toString();
                String phno=etphno.getText().toString();

                // Save the Data in Database
                db.addGSfeedback(new GSfeedback(studentname,classid,rollno,parentname,phno));

                Toast.makeText(getApplicationContext(), "Thanks for Registration, Welcome to Lilttle Flowers Public School Android App.", Toast.LENGTH_LONG).show();

                etName.setText("");
                     etClass.setText("");
                     etrollno.setText("");
                     etparentname.setText("");
                     etphno.setText("");

                     finish();
                     Intent nextScreen = new Intent(getApplicationContext(), Splashscreen.class);
                     startActivity(nextScreen);

              }
             }
        }

        });
        }

 public void onItemSelected(AdapterView<?> parent, View view, int position,long id) {
     spinnerOsversions.setSelection(position);
     String selState = (String) spinnerOsversions.getSelectedItem();
     etClass.setText("" + selState);
 }
 public void onItemSelected1(AdapterView<?> parent, View view, int position,long id) {
     spinnerOsversions1.setSelection(position);
     String selState1 = (String) spinnerOsversions1.getSelectedItem();
     etrollno.setText("" + selState1);
 }



 private boolean validate(){
   if(etName.length() == 0 || etClass.length() == 0 || etrollno.length() == 0 || etphno.length() == 0 || etparentname.length() == 0){
   Toast.makeText(getApplicationContext(), "pls fill the empty fields", Toast.LENGTH_SHORT).show();
   return false;
   } if(etName.length() > 30 && etparentname.length() > 30){
   //Toast.makeText(getApplicationContext(), "pls enter less the 25 characher", Toast.LENGTH_SHORT).show();
   etName.setError("pls enter less the 30 charachter");    
   return true;

  } else if(etphno.length() < 6 || etphno.length() > 13){
   //etNumber.setError("Not Valid Phone Number");
   Toast.makeText(getApplicationContext(), "Enter valid Phone Number", Toast.LENGTH_SHORT).show();
   return false;
  } else {
            return true;
  }
 }

 private boolean validates(){
  if(etphno.getText().toString().trim().matches(PHONE_REGEX)) {
   return true;
        } else {  
         Toast.makeText(getApplicationContext(), "Enter Valid Phone Number", Toast.LENGTH_SHORT).show();   
    return false;          
        }
 }

@Override
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

}

}

xml:

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >       

    <EditText
        android:id="@+id/etName"
        android:hint="Student Name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"> 
        <requestFocus />       
    </EditText>
      <Spinner
        android:id="@+id/spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp" /> 

    <EditText
        android:id="@+id/etClass" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Class"/>
    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp" /> 

    <EditText
        android:id="@+id/etrollno"
        android:hint="Roll Number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"> 
        <requestFocus />       
    </EditText>

     <EditText
        android:id="@+id/etparentname"
        android:hint="Parent Name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"> 
        <requestFocus />       
    </EditText>
    <EditText
        android:id="@+id/etphno" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Phone Number" />

    <Button
        android:id="@+id/registerbutton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="REGISTER"
        android:layout_marginBottom="60dp" />

    </LinearLayout>
    </ScrollView>

</LinearLayout>

In this i used spinner for two fields i.e class and roll no for that one i wrote the code like this but it not working.before i took one spinner and applied to class it working correctly but that same code only i used for roll no it not working can anyone please tell me what i did mistake and resolve it.

Thank you,

役に立ちましたか?

解決

Remove the method onItemSelected1(). and Replace the @Override onItemSelected() with following code.

public void onItemSelected(AdapterView<?> parent, View view, int position,
        long id) {

    switch (view.getId()) {
    case R.id.spinner:
        spinnerOsversions.setSelection(position);
        String selState = (String) spinnerOsversions.getSelectedItem();
        etClass.setText("" + selState);
        break;
    case R.id.spinner1:
        spinnerOsversions1.setSelection(position);
        String rollNoState = (String) spinnerOsversions1.getSelectedItem();
        etrollno.setText("" + rollNoState);
    default:
        break;
    }

}

Hope your validation is not correct. Kindly check that too.

他のヒント

Hi Copy and Replace and your Code:

private String[] state = { "class", "1", "2", "3", "4", "5", "6", "7", "8",
        "9" };
private String[] state1 = { "RollNO", "1", "2", "3", "4", "5", "6", "7",
        "8", "9" };

public String log;
List<String> list;

EditText etName, etClass, etrollno, etparentname, etphno;
Button btnregister;

Spinner spinnerOsversions, spinnerOsversions1;

String PHONE_REGEX;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    etName = (EditText) findViewById(R.id.etName);
    etClass = (EditText) findViewById(R.id.etClass);
    etrollno = (EditText) findViewById(R.id.etrollno);
    etparentname = (EditText) findViewById(R.id.etparentname);
    etphno = (EditText) findViewById(R.id.etphno);

    btnregister = (Button) findViewById(R.id.registerbutton);

    PHONE_REGEX = "[0-9]+";
    // spinner = (Spinner) findViewById(R.id.spinner);

    etClass.setVisibility(View.GONE);
    etrollno.setVisibility(View.GONE);
    spinnerOsversions = (Spinner) findViewById(R.id.spinner);

    spinnerOsversions1 = (Spinner) findViewById(R.id.spinner1);

    ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, state);

    adapter_state
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    spinnerOsversions.setAdapter(adapter_state);
    spinnerOsversions.setOnItemSelectedListener(this);

    ArrayAdapter<String> adapter_state1 = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, state1);

    adapter_state1
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    spinnerOsversions1.setAdapter(adapter_state1);
    spinnerOsversions1.setOnItemSelectedListener(this);

    btnregister.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if (validate()) {
                String studentname = etName.getText().toString();
                String classid = etClass.getText().toString();
                String rollno = etrollno.getText().toString();
                String parentname = etparentname.getText().toString();
                String phno = etphno.getText().toString();

                // Save the Data in Database

                Toast.makeText(
                        getApplicationContext(),
                        "Thanks for Registration, Welcome to Lilttle Flowers Public School Android App.",
                        Toast.LENGTH_LONG).show();
                etName.setText("");
                etClass.setText("");
                etrollno.setText("");
                etparentname.setText("");
                etphno.setText("");
                finish();
            }
        }

    });
}

public void onItemSelected(AdapterView<?> parent, View view, int position,
        long id) {

    switch (view.getId()) {
    case R.id.spinner:
        spinnerOsversions.setSelection(position);
        String selState = (String) spinnerOsversions.getSelectedItem();
        etClass.setText("" + selState);
        break;
    case R.id.spinner1:
        spinnerOsversions1.setSelection(position);
        String rollNoState = (String) spinnerOsversions1.getSelectedItem();
        etrollno.setText("" + rollNoState);
    default:
        break;
    }

}

private boolean validate() {
    String studentname = etName.getText().toString();
    String classid = spinnerOsversions.getSelectedItem().toString();
    String rollno = spinnerOsversions1.getSelectedItem().toString();
    String parentname = etparentname.getText().toString();
    String phno = etphno.getText().toString();

    if (TextUtils.isEmpty(studentname) || TextUtils.isEmpty(classid)
            || TextUtils.isEmpty(rollno) || TextUtils.isEmpty(parentname)
            || TextUtils.isEmpty(phno)) {
        Toast.makeText(getApplicationContext(),
                "pls fill the empty fields", Toast.LENGTH_SHORT).show();
        return false;
    } else if (etName.getText().toString().length() > 30
            && etparentname.getText().toString().length() > 30) {
        etName.setError("pls enter less the 30 charachter");
        return true;

    } else if (etphno.getText().toString().length() < 6
            || etphno.getText().toString().length() > 13) {
        Toast.makeText(getApplicationContext(), "Enter valid Phone Number",
                Toast.LENGTH_SHORT).show();
        return false;
    } else {
        if (etphno.getText().toString().trim().matches(PHONE_REGEX)) {
            return true;
        } else {
            Toast.makeText(getApplicationContext(),
                    "Enter Valid Phone Number", Toast.LENGTH_SHORT).show();
            return false;
        }
    }
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {

}

Let me know the feedback.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top