如何根据TextView上的值填充我的Spinner?

时间:2015-05-01 19:03:01

标签: java php android mysql spinner

我是Android编程的新手,我正在开发一个从MySQL DB中获取数据的项目。

现在,我有两个java文件。第一个提取员工及其ID,它有Spinners,点击后,它会将员工的姓名和身份的值复制到TextViews,并通过Intent将数据传输到下一个Activity。

第二个是根据员工ID获取员工的日程安排。第二个活动应该根据从第一个活动传递的员工的ID来填充Spinner。

第一项活动:

public class FirstActivity extends Activity {
    JSONObject jObj;
    JSONArray jArray;
    ProgressDialog pd;
    ArrayList < String > aestheticianList;
    ArrayList < Items > items;
    Button btnTransact;
    String service_name, price;

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

        new DownloadJSON().execute();

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

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent i = new Intent(cp000l3.flawlessfaceandbodyclinic.project.transact.FarmersAestheticianActivity.this,
                cp000l3.flawlessfaceandbodyclinic.project.transact.FarmersDateTimeActivity.class);
                String service_name = ((TextView) findViewById(R.id.service_name)).getText().toString();
                String price = ((TextView) findViewById(R.id.price)).getText().toString();
                String full_name = ((TextView) findViewById(R.id.full_name)).getText().toString();
                String aid = ((TextView) findViewById(R.id.aid)).getText().toString();

                i.putExtra("service_name", service_name);
                i.putExtra("price", price);
                i.putExtra("full_name", full_name);
                i.putExtra("aid", aid);

                startActivityForResult(i, 100);
            }
        });

        TextView txtService_name = (TextView) findViewById(R.id.service_name);
        TextView txtPrice = (TextView) findViewById(R.id.price);
        Intent i = getIntent();
        service_name = i.getStringExtra("service_name");
        price = i.getStringExtra("price");

        txtService_name.setText(service_name);
        txtPrice.setText(price);
    }
    private class DownloadJSON extends AsyncTask < Void, Void, Void > {

        @Override
        protected Void doInBackground(Void...params) {
            // TODO Auto-generated method stub
            items = new ArrayList < Items > ();
            aestheticianList = new ArrayList < String > ();

            try {
                jObj = JSONParser.getJSONfromURL("http://192.168.1.9:8013/flawlessadmin/storescripts/transaction/final/get_aesthetician2.php");
                try {
                    jArray = jObj.getJSONArray("aesthetician");
                    for (int i = 0; i < jArray.length(); i++) {
                        jObj = jArray.getJSONObject(i);

                        Items item = new Items();

                        item.setFull_name(jObj.optString("full_name"));
                        item.setAid(jObj.optString("aid"));

                        items.add(item);
                        aestheticianList.add(jObj.optString("full_name"));
                    }
                } catch (Exception e) {
                    Log.e("Error", e.getMessage());
                    e.printStackTrace();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
        protected void onPostExecute(Void args) {
            Spinner spin = (Spinner) findViewById(R.id.spin_aesthetician);

            spin.setAdapter(new ArrayAdapter < String > (FarmersAestheticianActivity.this,
            android.R.layout.simple_spinner_dropdown_item, aestheticianList));

            spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView <? > arg0, View arg1,
                int position, long arg3) {
                    // TODO Auto-generated method stub
                    TextView txt_full_name = (TextView) findViewById(R.id.full_name);
                    TextView txt_aid = (TextView) findViewById(R.id.aid);

                    txt_full_name.setText(items.get(position).getFull_name());
                    txt_aid.setText(items.get(position).getAid());

                    ////////////////////////////////

                    ////////////////////////////////
                }

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

                }
            });
        }
    }
}

第二项活动:

public class SecondActivity extends Activity {
    JSONObject jObj;
    JSONArray jArray;
    ProgressDialog pd;
    ArrayList < String > dateTimeList;
    ArrayList < Items > items;
    Button btnTransact;
    String service_name, price, full_name, aid, tid;

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

        new DownService().execute();

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

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent i = new Intent(cp000l3.flawlessfaceandbodyclinic.project.transact.FarmersDateTimeActivity.this,
                cp000l3.flawlessfaceandbodyclinic.project.transact.FarmersConfirmationActivity.class);
                String service_name = ((TextView) findViewById(R.id.service_name)).getText().toString();
                String price = ((TextView) findViewById(R.id.price)).getText().toString();
                String aid = ((TextView) findViewById(R.id.aid)).getText().toString();
                String full_name = ((TextView) findViewById(R.id.full_name)).getText().toString();
                String tid = ((TextView) findViewById(R.id.tid)).getText().toString();
                String datetime = ((TextView) findViewById(R.id.datetime)).getText().toString();

                i.putExtra("service_name", service_name);
                i.putExtra("price", price);
                i.putExtra("aid", aid);
                i.putExtra("full_name", full_name);
                i.putExtra("tid", tid);
                i.putExtra("date_time", datetime);

                startActivityForResult(i, 100);
            }
        });

        TextView txtService_name = (TextView) findViewById(R.id.service_name);
        TextView txtPrice = (TextView) findViewById(R.id.price);
        TextView txtFull_name = (TextView) findViewById(R.id.full_name);
        TextView txtAid = (TextView) findViewById(R.id.aid);
        Intent i = getIntent();
        service_name = i.getStringExtra("service_name");
        price = i.getStringExtra("price");
        full_name = i.getStringExtra("full_name");
        aid = i.getStringExtra("aid");

        txtService_name.setText(service_name);
        txtPrice.setText(price);
        txtFull_name.setText(full_name);
        txtAid.setText(aid);
    }
    private class DownService extends AsyncTask < Void, Void, Void > {

        @Override
        protected Void doInBackground(Void...params) {
            // TODO Auto-generated method stub
            items = new ArrayList < Items > ();
            dateTimeList = new ArrayList < String > ();

            /////////////////////////
            List < NameValuePair > param = new ArrayList < NameValuePair > ();
            param.add(new BasicNameValuePair("aid", aid));
            /////////////////////////

            try {
                String jObj = JSONParser.makeHttpRequest2("http://192.168.1.9:8013/flawlessadmin/storescripts/transaction/final/get_time2.php", "GET", param); //("http://192.168.1.9:8013/flawlessadmin/storescripts/transaction/final/get_time.php");
                try {
                    JSONObject jsonObj = new JSONObject(jObj);
                    for (int i = 0; i < jArray.length(); i++) {
                        jsonObj = jArray.getJSONObject(i);

                        Items item = new Items();

                        item.setDateTime(jsonObj.optString("date_time"));
                        item.setTid(jsonObj.optString("tid"));

                        items.add(item);
                        dateTimeList.add(jsonObj.optString("date_time"));
                    }
                } catch (Exception e) {
                    Log.e("Error", e.getMessage());
                    e.printStackTrace();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
        protected void onPostExecute(Void args) {
            Spinner spin = (Spinner) findViewById(R.id.spin_datetime);

            spin.setAdapter(new ArrayAdapter < String > (FarmersDateTimeActivity.this,
            android.R.layout.simple_spinner_dropdown_item, dateTimeList));

            spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView <? > arg0, View arg1,
                int position, long arg3) {
                    // TODO Auto-generated method stub
                    TextView txt_datetime = (TextView) findViewById(R.id.datetime);
                    TextView txt_tid = (TextView) findViewById(R.id.tid);

                    txt_datetime.setText(items.get(position).getDateTime());
                    txt_tid.setText(items.get(position).getTid());

                    ////////////////////////////////

                    ////////////////////////////////
                }

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

                }
            });
        }
    }
}

0 个答案:

没有答案