gpt4 book ai didi

java - 微调器值设置不正确

转载 作者:太空宇宙 更新时间:2023-11-03 10:39:31 24 4
gpt4 key购买 nike

我正在从在线 XML 网址获取数据并成功获取数据。我正在使用它的 hashmap 键和值...我得到的键和值在我的 LOGCAT 中如下所示-

{
Jackfruit=[Cabbage, Kesar, Amond],
Date=[van, hall, Brinjal],
Amond=[hall, van, Brinjal]
}

1. 当我第一次启动 android 应用程序时,spinner 和 spinner2 被填充到 spinner 中,所有数据都被正确填充,但在 spinner2 中,所有键值都显示......但它应该只显示spinner1 中显示的键值

2. 当我更改 spinner1 键值时,两个微调器中的列表项都正确显示,然后所有下拉列表都显示正确的值。

3. 当我点击 spinner2 值时,它充当键并开始显示微调器中的值,而不是它应该放置我点击的那个值。

我用记事本画出来看看问题

{
Jackfruit=[Cabbage, Kesar, Amond],
Date=[van, hall, Brinjal],
Amond=[hall, van, Brinjal]
}

Problem sketch

我的代码如下MainActivity.java

@SuppressLint("NewApi") 
public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener, OnClickListener {

ArrayList<String> title;
ArrayList<String> title2;
Button button;
Spinner spinner;
Spinner spinner2;
private EditText FromFruitsDateEtxt;
//private EditText toDateEtxt;

private DatePickerDialog FromFruitsDatePickerDialog;
//private DatePickerDialog toDatePickerDialog;

private SimpleDateFormat dateFormatter;

ArrayAdapter<String> FromFruits_adapter;
ArrayAdapter<String> FromFruits_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) {
parse();

FromFruits_adapter=new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_spinner_item, title);
FromFruits_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

FromFruits_adapter2 = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_spinner_item, title2);
FromFruits_adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

spinner.setAdapter(FromFruits_adapter);
FromFruits_adapter2.clear();
FromFruits_adapter2.notifyDataSetChanged();
spinner2.setAdapter(FromFruits_adapter2);
}

private Object FromFruits_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);
FromFruits_adapter2.clear();
for(String s : lang_Key){
FromFruits_adapter2.insert(s, FromFruits_adapter2.getCount());
}
FromFruits_adapter2.notifyDataSetChanged();
Toast.makeText(parent.getContext(), ""+lang_Key,
Toast.LENGTH_LONG).show();
}

public void onNothingSelected(AdapterView<?> arg0) {
}

protected void parse() {
try {
URL url = new URL("web url www.xyz.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();

NodeList nodeList = doc.getElementsByTagName("Fruits");
for (int i = 0; i < nodeList.getLength(); i++) {
Element el = (Element) nodeList.item(i);
// get the a:W value
String awValue = el.getElementsByTagName("FromFruits").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("ToFruits").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("FromFruits");
Element nameElement = (Element) nameList.item(0);
nameList = nameElement.getChildNodes();

NodeList websiteList = fstElmnt.getElementsByTagName("ToFruits");
Element websiteElement = (Element) websiteList.item(0);
websiteList = websiteElement.getChildNodes();

title.add(((Node) nameList.item(0)).getNodeValue());
}
NodeList nodeList2 = doc.getElementsByTagName("Fruits");
for (int i = 0; i < nodeList2.getLength(); i++) {
Node node = nodeList2.item(i);

Element fstElmnt = (Element) node;
NodeList nameList = fstElmnt.getElementsByTagName("ToFruits");
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 == FromFruitsDateEtxt) {
FromFruitsDatePickerDialog.show();
} else {
Log.d("errorrrr", "soryy");
}
}
}

草图中的问题可以看出

enter image description here

最佳答案

我自己和我的一位来自 stackoverflow 的 friend Ricardo Barroca 解决了这个问题看我注释掉的语句

spinner.setOnItemSelectedListener(this);
spinner2 = (Spinner) findViewById(R.id.spinner2);
//spinner2.setOnItemSelectedListener(this);

这解决了我的 spinner2 单击问题,它获取值而不是将其自身设置为微调器。

从下面的代码中,它清除了 spinner1 和 spinner2 的预填充值

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);

关于java - 微调器值设置不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36641205/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com