选择spinner1后,我在spinner1和spinner 2中获取数据,然后在spinner2中自动填充相应的数据
以下是从URL获取的XML格式数据 -
<a:Pair>
<a:FromID>01</a:FromID> <---This should Toast---->
<a:FromName>INDIA</a:FromName><---On selecting this from spinner------->
<a:ToID>40</a:ToID>
<a:ToName>TURKEY</a:ToName>
</a:Pair>
<a:Pair>
<a:FromID>10</a:FromID>
<a:FromName>USA</a:FromName>
<a:ToID>01</a:ToID>
<a:ToName>INDIA</a:ToName>
</a:Pair>
<a:Pair>
<a:FromID>19</a:FromID>
<a:FromName>CHINA</a:FromName>
<a:ToID>10</a:ToID>
<a:ToName>USA</a:ToName>
</a:Pair>
<a:Pair>
<a:FromID>40</a:FromID>
<a:FromName>TURKEY</a:FromName>
<a:ToID>01</a:ToID>
<a:ToName>INDIA</a:ToName>
</a:Pair>
</Result>
</Response>
但问题是如果 spinner1数据是美国选择的那么我想干它的相应的ID即10 ...我将在未来使用它来传递到下一个活动也
我的下面的代码工作正常我只想在选择spinner1时选择其XML数据的id,如果spinner2那么它的相应id也应该是toast。
MainActivity.java
public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener, OnClickListener {
ArrayList<String> title;
ArrayList<String> title2;
Button button;
Spinner spinner;
Spinner spinner2;
private EditText fromDateEtxt;
//private EditText toDateEtxt;
private DatePickerDialog fromDatePickerDialog;
//private DatePickerDialog toDatePickerDialog;
private SimpleDateFormat dateFormatter;
ArrayAdapter<String> from_adapter;
ArrayAdapter<String> from_adapter2;
Map<String, List<String>> values = new HashMap<String, List<String>>();
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
title = new ArrayList<String>();
title2 = new ArrayList<String>();
button = (Button) findViewById(R.id.button1);
spinner = (Spinner) findViewById(R.id.spinner1);
spinner.setOnItemSelectedListener(this);
spinner2 = (Spinner) findViewById(R.id.spinner2);
//spinner2.setOnItemSelectedListener(this);
dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.US);
findViewsById();
setDateTimeField();
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
button.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
parse();
from_adapter=new ArrayAdapter<String>(getBaseContext(),android.R.layout.simple_spinner_item, title);
from_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
title2.clear();
from_adapter2=new ArrayAdapter<String>(getBaseContext(),android.R.layout.simple_spinner_item, title2);
from_adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(from_adapter);
//from_adapter2.clear();
//from_adapter2.notifyDataSetChanged();
spinner2.setAdapter(from_adapter2);
}
private Object from_adapter(int i) {
// TODO Auto-generated method stub
return null;
}
});
}
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
String lang_Name = parent.getItemAtPosition(pos).toString();
List<String> lang_Key = values.get(lang_Name);
from_adapter2.clear();
for(String s : lang_Key){
from_adapter2.insert(s, from_adapter2.getCount());
}
from_adapter2.notifyDataSetChanged();
Toast.makeText(parent.getContext(), ""+lang_Key,
Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
}
protected void parse() {
// TODO Auto-generated method stub
try {
URL url = new URL(
"www.example.com");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("a:Pair");
for (int i = 0; i < nodeList.getLength(); i++) {
Element el = (Element) nodeList.item(i);
// get the a:W value
String awValue = el.getElementsByTagName("a:FromName").item(0).getTextContent();
// add the above value in our map as key if it isn't present in the map, this key will
// have a list associated with it in which ALL the values for a:R will be stored, if
// the awValue key is present then you just add the new a:R value to its list
if (!values.containsKey(awValue)) {
values.put(awValue, new ArrayList<String>());
}
// also add the value pointed by a:R to the list associated with a:W
String arValue = el.getElementsByTagName("a:ToName").item(0).getTextContent();
values.get(awValue).add(arValue);
// Log.d("ADebugTag", "Value: " + arValue);
}
Log.d("ADebugTag", "Value: " + values);
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
Element fstElmnt = (Element) node;
NodeList nameList = fstElmnt.getElementsByTagName("a:FromName");
Element nameElement = (Element) nameList.item(0);
nameList = nameElement.getChildNodes();
NodeList websiteList = fstElmnt.getElementsByTagName("a:ToName");
Element websiteElement = (Element) websiteList.item(0);
websiteList = websiteElement.getChildNodes();
title.add(((Node) nameList.item(0)).getNodeValue());
}
NodeList nodeList2 = doc.getElementsByTagName("a:Pair");
for (int i = 0; i < nodeList2.getLength(); i++) {
Node node = nodeList2.item(i);
Element fstElmnt = (Element) node;
NodeList nameList = fstElmnt.getElementsByTagName("a:ToName");
Element nameElement = (Element) nameList.item(0);
nameList = nameElement.getChildNodes();
title2.add(((Node) nameList.item(0)).getNodeValue());
}
Set<String> set = new LinkedHashSet<String>(title);
title = new ArrayList<String>(set);
// Collection<String> set = new LinkedHashSet<String>(months);
Set<String> set2 = new LinkedHashSet<String>(title2);
title2 = new ArrayList<String>(set2);
System.out.println("list are");
System.out.println(set);
System.out.println("list 2 are");
System.out.println(set2);
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
}
@Override
public void onClick(View view) {
if(view == fromDateEtxt) {
fromDatePickerDialog.show();
} else {
Log.d("errorrrr", "soryy");
}
}
}
我无法将id传递给在微调器中填充的相应数据以及id
的Toast答案 0 :(得分:0)
您可以像这样在每个微调器上创建单独的操作
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// Show your toast for Spinner 1
from_adapter2.notifyDataSetChanged();// Changed
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// Show your toast for Spinner 2
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});