数据库值未显示在android spinner中

时间:2014-10-30 11:46:10

标签: php android

嗨在我的应用程序中我创建了一个form.in第一个是intial是android中的spinner这些来自数据库的初始字段值我希望将这些值显示到intiial spinner但是值不显示到数据库中。为此我我创建了名为getpatient.php的php文件,只有我得到了微调器值。 任何人都可以帮助我。
联系我们

public class ContactUs extends Activity implements OnClickListener
{
    EditText regno1,fname1,mname1,lname1,dob1,age1,f_hname1,adress1,tel_o1,pin_zip1,tel_r,email1,mobile1,id_prof1,b_group1,name_kin1,realition1;
    String data="";
    TextView tv1;
    Button back,home,choosefile;
    Spinner intial,pmaincat,pcatory,religion,caste;
    RadioGroup sex,payment;
    RadioButton rb;
    String initial_id;
    private EditText fromDateEtxt;
    private DatePickerDialog fromDatePickerDialog;
    private SimpleDateFormat dateFormatter;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {

        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.contactus);    
        tv1=(TextView) findViewById(R.id.tv);


        payment = (RadioGroup) findViewById(R.id.sex);
        int selectedId = payment.getCheckedRadioButtonId();
        rb = (RadioButton) findViewById(selectedId);

        back=(Button) findViewById(R.id.back);
        home=(Button) findViewById(R.id.home);
        fname1=(EditText) findViewById(R.id.fname);        
        lname1=(EditText) findViewById(R.id.lname);
        regno1=(EditText) findViewById(R.id.rgno);
        mname1=(EditText) findViewById(R.id.mname);

        age1=(EditText) findViewById(R.id.age);
        f_hname1=(EditText) findViewById(R.id.f_hname);
        adress1=(EditText) findViewById(R.id.adress);        
        tel_o1=(EditText) findViewById(R.id.telo);
        pin_zip1=(EditText) findViewById(R.id.pzipcode);
        tel_r=(EditText) findViewById(R.id.tel_r);
        email1=(EditText) findViewById(R.id.email);
        mobile1=(EditText) findViewById(R.id.mobile);        
        id_prof1=(EditText) findViewById(R.id.idprof);
        b_group1=(EditText) findViewById(R.id.bgroup);
        name_kin1=(EditText) findViewById(R.id.name_kin);
        realition1=(EditText) findViewById(R.id.relationship);


        intial=(Spinner)findViewById(R.id.intial);
        pmaincat=(Spinner)findViewById(R.id.pmaincat);
        pcatory=(Spinner)findViewById(R.id.pcatgory);
        religion=(Spinner)findViewById(R.id.religion);
        caste=(Spinner)findViewById(R.id.caste);

        sex=(RadioGroup)findViewById(R.id.sex);
        payment=(RadioGroup)findViewById(R.id.paymentmode);

        tv1.setText("PATIENT REGISTRATION");
        final Button photo1 = (Button) findViewById(R.id.photo);    
        Button Send = (Button) findViewById(R.id.Send); 

        Send.setOnClickListener(new OnClickListener() {             
            public void onClick(View v)
            {
                 String first_name = fname1.getText().toString();
                 String middle_name = mname1.getText().toString(); 
                 String last_name = lname1.getText().toString(); 
                 String reg_no = regno1.getText().toString();
                 String dob = fromDateEtxt.getText().toString();
                 String age = age1.getText().toString();
                 String address = adress1.getText().toString(); 
                 String phone_office = tel_o1.getText().toString(); 
                 String fh = f_hname1.getText().toString(); 
                 String pin = pin_zip1.getText().toString();
                 String phone_resi = tel_r.getText().toString();
                 String email = email1.getText().toString(); 
                 String mobile_no = mobile1.getText().toString(); 
                 String id_proof = id_prof1.getText().toString();
                 String b_group = b_group1.getText().toString();
                 String name_of_kin = name_kin1.getText().toString(); 
                 String relation = realition1.getText().toString(); 

                 String pmode = rb.getText().toString().trim();
                 String photo = photo1.getText().toString(); 

                 String pmc=pmaincat.getSelectedItem().toString();
                 String pc=pcatory.getSelectedItem().toString();

                 String religion_id=religion.getSelectedItem().toString();
                 String caste_id=caste.getSelectedItem().toString();

                 dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.US);

                findViewsById();

                setDateTimeField();

                if(first_name.equals(""))
                {           
                    fname1.setError( "Please Enter First Name" );
                }
                else if(last_name.equals(""))
                {           
                    lname1.setError( "Please Enter Last Name" );
                }   
                if(reg_no.equals(""))
                {           
                    fname1.setError( "Please Select Religon" );
                }
                else if(last_name.equals(""))
                {           
                    lname1.setError( "Please Enter Last Name" );
                }   
                /*else if(mobile.equals(""))
                {           
                    mobile1.setError( "Please Enter Mobile No." );

                }
                else if(!isValidMobile(mobile)){


                }

                else if(email.equals(""))
                {           
                    email1.setError( "Please Enter EmailId" );
                }
                else if(!isValidMail(email)){


                }*/


                else
                {

                    try{

                        String queryString ="reg_no="+ reg_no
                                 +"&pmc="+pmc+"&pc="+pc+ "&initial_id="+initial_id+"&first_name="+first_name+"&middle_name="+middle_name+"&last_name="+last_name
                                 +"&fh="+fh+"&religion_id="+religion_id+"&dob="+dob+"&caste_id="+caste_id+"&address="+address+"&pin="+pin
                                 +"&email="+email+"&id_proof="+id_proof+"&phone_office="+phone_office+"&phone_resi="+phone_resi+"&mobile_no="+mobile_no+"&name_of_kin="+name_of_kin
                                 +"&relation="+relation+"&photo="+photo+"&pmode="+pmode;
                    data = DatabaseUtility.executeQueryPhp("pat_registration",queryString);
                    /*fname1.setText("");
                    lname1.setText("");
                     mobile1.setText("");
                     altmob1.setText("");
                     email1.setText("");
                     comment1.setText("");*/
                    Toast.makeText(
                            ContactUs.this,
                            "Message:Records Saved Sucessfully",

                            Toast.LENGTH_SHORT).show();
                    }
                     catch (Exception e) {
                            e.printStackTrace();
                        }

                }   
            }   

        });
        String result = DatabaseUtility.executeQueryPhp("getpatient","");
        try
        {
            JSONArray JA=new JSONArray(result);
            JSONObject json= null;
            final String[] intial = new String[JA.length()];        


            for(int i=0;i<JA.length();i++)
            {
                json=JA.getJSONObject(i);
                intial[i] = json.getString("initial_name");

            }

            final Spinner sp = (Spinner) findViewById(R.id.intial);
            List<String> list = new ArrayList<String>();



            ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>
            (getApplicationContext(), android.R.layout.simple_spinner_item, list);
            dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            sp.setAdapter(dataAdapter);

            sp.setOnItemSelectedListener(new OnItemSelectedListener() 
            {
                public void onItemSelected(AdapterView<?> arg0, View arg1,int position, long id) 
                {
                    // TODO Auto-generated method stub

                    String initial_id=sp.getSelectedItem().toString();

                    Toast.makeText(getApplicationContext(), initial_id,
                    Toast.LENGTH_LONG).show();

                    Log.e("Item",initial_id);
                }

                public void onNothingSelected(AdapterView<?> arg0) 
                {
                    // TODO Auto-generated method stub
                }
            });
        }
        catch(Exception e)
        {
            Log.e("Fail 3", e.toString());
        }
back.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                onBackPressed();

            }
        });
 home.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent in=new Intent(getApplicationContext(),MainActivity.class);
                startActivity(in);

            }
        });
    }
    private void findViewsById() {
        fromDateEtxt = (EditText) findViewById(R.id.dob);
        fromDateEtxt.setInputType(InputType.TYPE_NULL);
        fromDateEtxt.requestFocus();

    }

    private void setDateTimeField() {
        fromDateEtxt.setOnClickListener(this);

        Calendar newCalendar = Calendar.getInstance();
        fromDatePickerDialog = new DatePickerDialog(this,
                new OnDateSetListener() {

            public void onDateSet(DatePicker view, int year,
                    int monthOfYear, int dayOfMonth) {
                Calendar newDate = Calendar.getInstance();
                newDate.set(year, monthOfYear, dayOfMonth);
                fromDateEtxt.setText(dateFormatter.format(newDate
                        .getTime()));
            }

        }, newCalendar.get(Calendar.YEAR),
        newCalendar.get(Calendar.MONTH),
        newCalendar.get(Calendar.DAY_OF_MONTH));

    }

    @Override
    public void onBackPressed(){


        super.onBackPressed();      
    }

    private boolean isValidMail(String email) 
    {
        boolean check;
        Pattern p;
        Matcher m;

        String EMAIL_STRING = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
                + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

        p = Pattern.compile(EMAIL_STRING);

        m = p.matcher(email);
        check = m.matches();

        if(!check)
        {
            email1.setError("Not Valid Email");
        }
        return check;
    }
    private boolean isValidMobile(String mobile) 
    {
        boolean check;
        if(mobile.length() < 6 || mobile.length() > 13)
        {
            check = false;
            mobile1.setError("Not Valid Number");
        }
        else
        {
            check = true;
        }
        return check;
    }
    public void onNothingSelected(AdapterView<?> arg0) {

    }

    public void onClick(View view) {
        if (view == fromDateEtxt) {
            fromDatePickerDialog.show();
        }

    }

1 个答案:

答案 0 :(得分:0)

你是在字符串数组initial []中插入值吗?...但是你要添加列表数据

            List<String> list = new ArrayList<String>();

            ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>
            (getApplicationContext(), android.R.layout.simple_spinner_item, list);

虽然列表中没有任何内容......所以它将被设置为空..

尝试这样做..

替换==&gt; final String[] intial = new String[JA.length()];

List<String> list = new ArrayList<String>();

并将您的数据添加到此列表中: -

list.add(json.getString("initial_name"));

然后添加数据适配器。并将适配器设置为微调器。

 ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>
                (getApplicationContext(), android.R.layout.simple_spinner_item, list);