将数据从一个意图传递到 Spinner 形成另一个意图
Pass data from one intent to a Spinner form other intent
我的申请中有两个具体意图:
(1) google 地图在单击标记时读取一些已从 CSV 文件填充的数据和
(2) displays/saves 根据标记详细信息(新的或已保存的)详细说明该标记。我的问题是我不知道如何将从意图 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);
}
只需将这段代码设置为每个微调器的声明:
//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);
}
我的申请中有两个具体意图:
(1) google 地图在单击标记时读取一些已从 CSV 文件填充的数据和
(2) displays/saves 根据标记详细信息(新的或已保存的)详细说明该标记。我的问题是我不知道如何将从意图 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);
}
只需将这段代码设置为每个微调器的声明:
//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);
}