2016-07-12 2 views
1

私は、E-commerce.iのアンドロイドアプリを開発している最初のスピナーフェッチカテゴリの1つの活動で2つのスピナーを使用してjson .Itは、 phpjsonしかし、私はこの概念を実装する方法この概念を知らないどのように2つのスピナーを使用して1つのアクティビティでphpのmysqlと

public class ProductForm extends AppCompatActivity implements Spinner.OnItemSelectedListener { 
    Spinner prospin, prospin1; 
    EditText pdctname, pdctbrand, pdctmodel, pdctdiscount, pdctdisvalidity, pdctprice; 
    ImageView pfile; 
    Button pdctphoto, pdctupload, pdctsubmit; 
    String val1, val2, val3, val4, val5, val6, val7, val8, val9; 
    String Spinner_url1 = "category url"; 
    String Spinner_url2 = “sub category url” 
    ClassJSONParser json = new ClassJSONParser(); 
    private JSONArray data = null; 
    static final int REQUEST_TAKE_PHOTO = 1; 
    String mCurrentPhotoPath; 
    private String myJSON; 
    String monthname = "cat_name"; 
    private ArrayList<String> arrayList; 

    static String strSDCardPathName = Environment.getExternalStorageDirectory() + "/DCIM" + "/"; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.product_form); 
     StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
     StrictMode.setThreadPolicy(policy); 
     spindisplaydata(); 
     createFolder(); 
     prospin = (Spinner) findViewById(R.id.spinner); 
     prospin1 = (Spinner) findViewById(R.id.spinner1); 
     prospin.setOnItemSelectedListener(this); 

     pdctname = (EditText) findViewById(R.id.pname); 
     pdctbrand = (EditText) findViewById(R.id.pbrand); 
     pdctmodel = (EditText) findViewById(R.id.pmodel); 
     pdctdiscount = (EditText) findViewById(R.id.pdiscount); 
     pdctdisvalidity = (EditText) findViewById(R.id.pdiscountvalidity); 
     pdctprice = (EditText) findViewById(R.id.proprice); 
     pfile = (ImageView) findViewById(R.id.pfilepath); 
     pdctphoto = (Button) findViewById(R.id.prophoto); 
     pdctphoto.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
       if (takePictureIntent.resolveActivity(getPackageManager()) != null) { 
        File photoFile = null; 
        try { 
         photoFile = createImageFile(); 
        } catch (IOException ex) { 
         ex.printStackTrace(); 
        } 
        if (photoFile != null) { 
         takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, 
           Uri.fromFile(photoFile)); 
         startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO); 
        } 
       } 
      } 
     }); 

     pdctupload = (Button) findViewById(R.id.proupload); 
     pdctsubmit = (Button) findViewById(R.id.prosubmit); 
     pdctsubmit.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       val1 = pdctname.getText().toString(); 
       val2 = pdctbrand.getText().toString(); 
       val3 = pdctmodel.getText().toString(); 
       val4 = pdctdiscount.getText().toString(); 
       val5 = pdctdisvalidity.getText().toString(); 
       val6 = pdctprice.getText().toString(); 
       val7 = prospin.getSelectedItem().toString(); 
       val8 = prospin1.getSelectedItem().toString(); 
       val9 = pfile.getResources().toString(); 

       if (val1.equals("") || val2.equals("") || val3.equals("") || val4.equals("") || val5.equals("") || val6.equals("")) { 

       } 
      } 
     }); 


    } 

    private void spindisplaydata() { 
     class GetDataJSON extends AsyncTask<String, Void, String> { 
      private String line; 

      @Override 
      protected String doInBackground(String... params) { 
       InputStream is = null; 
       String result = null; 
       arrayList = new ArrayList<>(); 
       ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
       String key12 = "3015"; 
       nameValuePairs.add(new BasicNameValuePair("id", key12)); 
       try { 
        HttpClient httpclient = new DefaultHttpClient(); 
        String php_url = Spinner_url1; 
        HttpPost httppost = new HttpPost(php_url); 
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
        HttpResponse response = httpclient.execute(httppost); 
        HttpEntity entity = response.getEntity(); 
        is = entity.getContent(); 
       } catch (Exception e) { 
        Log.e("Fail 1", e.toString()); 
        Toast.makeText(getApplicationContext(), 
          "Invalid IP Address", Toast.LENGTH_LONG).show(); 
       } 
       try { 
        BufferedReader reader = new BufferedReader(
          new InputStreamReader(is, "iso-8859-1"), 8); 
        StringBuilder sb = new StringBuilder(); 
        while ((line = reader.readLine()) != null) { 
         sb.append(line + "\n"); 
        } 
        is.close(); 
        result = sb.toString(); 
        Log.e("pass 2", "connection success "); 
       } catch (Exception e) { 
        Log.e("Fail 2", e.toString()); 
       } 
       return result; 
      } 

      @Override 
      protected void onPostExecute(String result) { 
       myJSON = result; 
       showList1(); 

      } 

     } 
     GetDataJSON g = new GetDataJSON(); 
     g.execute(); 

    } 

    private void showList1() { 
     final ArrayList<HashMap<String, Object>> lista = new ArrayList<HashMap<String, Object>>(); 
     HashMap<String, Object> data; 
     try { 
      JSONObject json_data = new JSONObject(myJSON); 
      JSONArray jArray = json_data.getJSONArray("result"); 

      for (int i = 0; i < jArray.length(); i++) { 
       json_data = jArray.getJSONObject(i); 
       data = new HashMap<String, Object>(); 

       data.put("catname", json_data.getString(monthname)); 

       lista.add(data); 
       arrayList.add(json_data.getString(monthname)); 
       Log.e("Catnames", "" + monthname); 
       Log.e("Catnames", "" + arrayList); 


      } 

      SpinAdapter1 adapter = new SpinAdapter1(lista, ProductForm.this, arrayList); 
      prospin.setAdapter(adapter); 

     } catch (JSONException e) { 
      Log.e("log_tag2", "Error parsing data " + e.toString()); 
     } 


    } 


    private File createImageFile() throws IOException { 
     // Create an image file name 
     @SuppressLint("SimpleDateFormat") String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
     String imageFileName = "JPEG_" + timeStamp + "_"; 
     File storageDir = new File(strSDCardPathName); 
     File image = File.createTempFile(imageFileName, ".jpg", storageDir); 

     mCurrentPhotoPath = image.getAbsolutePath(); 
     return image; 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (resultCode == RESULT_OK) { 

      //*** Rename File 
      String strNewName = "MyPicture.jpg"; 
      String NewPath = strSDCardPathName + strNewName; 
      Log.e("Pathname", "" + NewPath); 

      //*** Resize Images 
      try { 
       ResizeImages(mCurrentPhotoPath, NewPath); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      Bitmap bitmap = BitmapFactory.decodeFile(NewPath); 
      pfile.setImageBitmap(bitmap); 
      GalleryAddPic(); 
     } 
    } 

    private void GalleryAddPic() { 
     Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 

     File f = new File(mCurrentPhotoPath); 

     Uri contentUri = Uri.fromFile(f); 

     mediaScanIntent.setData(contentUri); 

     this.sendBroadcast(mediaScanIntent); 
    } 

    public static void ResizeImages(String sPath, String sTo) throws IOException { 

     Bitmap photo = BitmapFactory.decodeFile(sPath); 
     photo = Bitmap.createScaledBitmap(photo, 720, 600, false); 
     ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
     photo.compress(Bitmap.CompressFormat.JPEG, 100, bytes); 

     File f = new File(sTo); 
     f.createNewFile(); 
     FileOutputStream fo = new FileOutputStream(f); 
     fo.write(bytes.toByteArray()); 
     fo.close(); 

     File file = new File(sPath); 
     file.delete(); 

    } 

    public static void createFolder() { 
     File folder = new File(strSDCardPathName); 
     try { 
      // Create folder 
      if (!folder.exists()) { 
       folder.mkdir(); 
      } 
     } catch (Exception ex) { 
     } 

    } 


    @Override 
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
     String selectedItemText = parent.getItemAtPosition(position).toString(); 
     Toast.makeText(ProductForm.this, selectedItemText, Toast.LENGTH_SHORT).show(); 
     try { 
      List<NameValuePair> params = new ArrayList<NameValuePair>(); 
      params.add(new BasicNameValuePair("cat_name_sub", prospin.getSelectedItem().toString())); 
      JSONObject obj1 = json.membuatHttpRequest(Spinner_url2, "GET", params); 
      data = obj1.getJSONArray(TAG_SEMUA_KOMENTAR); 
      final ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String, String>>(); 
      HashMap<String, String> map; 
     } 


    } 


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

    } 


} 
+0

最初のスピナーに依存する2番目のスピナーフェッチデータを接続する方法 – pgsoftware

答えて

1

使用このライブラリ助けてください:あなたが唯一のアダプターで、文字列のリストを設定する必要がlibにSearchable Spinner

は、これは非常に単純なスピナーですし、その後、スピナーにアダプタをセットしてください.ItemSelect Spinnerの選択あなたは最初のスピナーの選択アイテムIDを取得することができ、これに基づいて2番目のスピナーのデータを取得できます!

関連する問題