簡體   English   中英

數據庫值未顯示在android微調器中

[英]database values are not displaying in android spinner

嗨,在我的應用程序中,我創建了一個form.in,其中第一個intial是android中的spinner,這些初始字段值來自數據庫,我想將這些值顯示為intiial spinner,但值未顯示到datase中。為此,我創建了php僅在我獲得微調器值的情況下,名為get Patient.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();
        }

    }

您要在字符串數組initial []中插入值嗎?...但是您要添加list的數據

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

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

而列表中沒有任何內容...因此它將被設置為空。

嘗試這樣做

replace ==> 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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM