将数据从一种意图传递到Spinner形式从另一种意图

时间:2018-08-19 12:11:12

标签: google-maps android-studio google-maps-api-3 spinner autocompletetextview

我在应用程序中有两个特定意图:

(1)谷歌地图,当单击标记和

时,读取一些已经从CSV文件填充的数据

(2),根据标记详细信息(新的或保存的)显示/保存有关该标记的详细信息。我的问题是,我不知道如何将意图1中从CSV读取的值设置为意图2中的Spinners / autocomplete文本字段(这是.csv中的值)。

MapsActivity(用于发送从CSV读取的数据的意图):

 bt_da.setOnClickListener(new View.OnClickListener()

        {
            @Override
            public void onClick(View v)  {

                dialog.dismiss();
                File file = new File(getExternalFilesDir(null), "date.csv");

                //file exists
                if (file.exists()) {
                    Intent intent = new Intent(MapsActivity.this, MainActivity.class);
                //do the reading
                InputStream instream = null;
                try {
                    instream = new FileInputStream(file);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                InputStreamReader inputreader = new InputStreamReader(instream);
                BufferedReader reader = new BufferedReader(inputreader);
                String line;

                try {
                    while ((line = reader.readLine()) != null  ) {

                        String[] RowData = line.split(",");
                        Double latitudine = Double.valueOf(RowData[0]);
                        Double longitudine = Double.valueOf(RowData[1]);


                        //citire
                        String specie = String.valueOf(RowData[2]);
                        String grosime = RowData[3];
                        String inaltime = RowData[4];
                        String tip = String.valueOf(RowData[5]);
                        String risc = String.valueOf(RowData[6]);
                        String viabilitate = String.valueOf(RowData[7]);
                        String gresit = String.valueOf(RowData[8]);


                        //verificare daca exista
                        if (latitudine == coords.latitude && longitudine == coords.longitude ) {

                            Toast.makeText(MapsActivity.this, gresit, Toast.LENGTH_SHORT).show();
                            control = true;

                            //scriere
                            intent.putExtra("markerLat", coords.latitude);
                            intent.putExtra("markerLong", coords.longitude);
                            intent.putExtra("control", control);
                            intent.putExtra("specie", specie);
                            intent.putExtra("grosime", grosime);
                            intent.putExtra("inaltime", inaltime);
                            intent.putExtra("tip", tip);
                            intent.putExtra("risc", risc);
                            intent.putExtra("viabilitate", viabilitate);
                            intent.putExtra("gresit", gresit);
                            control = false;

                        }

                        //daca nu exista
                        else {

                            intent.putExtra("markerLat", coords.latitude);
                            intent.putExtra("markerLong", coords.longitude);

                        }

                    }
                    startActivity(intent);
                }
                catch (IOException e)
                {
                    Log.w("ExternalStorage", "Error reading " + file, e);
                }
            }

            else {

               //file does not exist
                Intent intent = new Intent(MapsActivity.this, MainActivity.class);
                intent.putExtra("markerLat", coords.latitude);
                intent.putExtra("markerLong", coords.longitude);
                startActivity(intent);
            }
        }
    });

MainActivity(显示数据的意图):

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent i = getIntent();
    lat = i.getDoubleExtra("markerLat", 0.0);
    longi = i.getDoubleExtra("markerLong", 0.0);
    control = i.getBooleanExtra("control", false);
    String specie = i.getStringExtra("specie");
    String grosime = i.getStringExtra("grosime");
    String inaltime = i.getStringExtra("inaltime");
    String tip = i.getStringExtra("tip");
    String risc = i.getStringExtra("risc");
    String viabilitate = i.getStringExtra("viabilitate");
    String gresit = i.getStringExtra("gresit");


    Salveaza = findViewById(R.id.button4);
    editText4 = findViewById(R.id.editText4);
    editText7 = findViewById(R.id.editText7);
    checkBox = (CheckBox) findViewById(R.id.checkBox);
    checkBox.setChecked(false);

    gr_osime = findViewById(R.id.editText3);
    in_altime = findViewById(R.id.editText6);

    editText4.setText(lat + " ");
    editText7.setText(longi + " ");



  //set data to fields
  if(control!=false) {

      gr_osime.setText(grosime);
      in_altime.setText(inaltime);

        if(gresit.equals("Gresit"))
        {
            checkBox.setChecked(true);
        }
        else
        if(gresit.equals("Bun")) {
            checkBox.setChecked(false);
        }
            }

 else
{
        Toast.makeText(getApplicationContext(), "Nu e salvat in csv!", Toast.LENGTH_SHORT).show();
    }


    mView = findViewById(R.id.autoCompleteTextView);
    mAdapter = new CSVAdapter(this, -1);
    mView.setAdapter(mAdapter);

    mView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(view.getContext(), mAdapter.getItem(position).getDenumire(), Toast.LENGTH_SHORT).show();
        }
    });

    spinner6 = findViewById(R.id.spinner2);
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this,
            android.R.layout.simple_dropdown_item_1line, SpinnerTip);
    arrayAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
    spinner6.setAdapter(arrayAdapter);

    spinner6.setOnItemSelectedListener(this);

    spinner7 = findViewById(R.id.spinner3);
    ArrayAdapter<String> arrayAdapter2 = new ArrayAdapter<>(this,
            android.R.layout.simple_dropdown_item_1line, SpinnerRisc);
    arrayAdapter2.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
    spinner7.setAdapter(arrayAdapter2);
    spinner7.setOnItemSelectedListener(this);

    spinner8 = findViewById(R.id.spinner);
    ArrayAdapter<String> arrayAdapter3 = new ArrayAdapter<>(this,
            android.R.layout.simple_dropdown_item_1line, SpinnerViabilitate);
    arrayAdapter3.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
    spinner8.setAdapter(arrayAdapter3);
    spinner8.setOnItemSelectedListener(this);

}

1 个答案:

答案 0 :(得分:0)

只需将这段代码设置为每个微调器的声明即可:

//if some data is passed
if(control==true)
    {
       //get the position of the passed string in our adapter
        int position1 = arrayAdapter2.getPosition(risc);
        spinner7.setSelection(position1);
    }