gpt4 book ai didi

java - android xml节点解析数组适配器很奇怪

转载 作者:行者123 更新时间:2023-11-30 11:52:56 24 4
gpt4 key购买 nike

我有一个奇怪的问题,(请先查看底部的较短版本)

当我的 Activity 第一次启动时, ListView 显示数据集正常。该适配器是一个自定义适配器,它显示 2 行文本和一个图像。我在 ListView 的单击事件上调用异步任务,数据集根据 ListView 中单击的内容进行更新 - 更具体地说,与适配器关联的数组被某些 xml 的解析重写,然后 notifyachapterdatasetchanged 方法是在 onPostExecute 函数中调用以更新 ListView 。但是,当我遍历某些 xml(格式非常好且经过验证)时,我总是得到 NullPointerException。还值得一提的是,解析所需值的算法很好,因为如上所述,如果我只写入数组的 1 个元素,那么我不会得到错误,我只是得到最后一个节点值,因此它在正确的位置循环.即,如果我简单地尝试将循环期间正在解析的当前节点值复制到 producyTypes[0] 中,那么循环中的最后一个值实际上会进入 ListView ,因为它会不断覆盖数组的这个元素

这是我的代码。`

public class main extends Activity implements OnItemClickListener

{

String selectedType="";
ListView lview3;
ImageView iv;
ListViewCustomAdapter adapter;
String test = "";
List<String> ls;

String productTypes[] = {"Monitor","Components","Systems","Laptops","Flash / Usb Memory",
"Networking","Cables","Peripherals","Sound and Vision", "Software"};

String productsIncluded[] = {"Sizes (inches) 17, 19, 20",
"Motherboards, PSU, Cases, Fans/Heatsinks/Coolers, Barebones Systems, Blue-Ray/DVD. Card Readers, Controller Cards, Drive Enclosures, Sound Cards",
"Bundles, Switches and Hubs, Print Servers, Accessories/Modules",
"Cables for: Drives, Networking, HDMI/Monitor, Audio/Video, USB/Firewire, Power, Miscellaneous",
"Mice, Connectors, Bluetooth Devices",
"Mp3/Mp4 Solar Panel",
"Anti-Virus, Internet Security, Operating Systems, Office,,
"",
""};

private static int images[] = {R.drawable.monitor, R.drawable.components, R.drawable.systems,
R.drawable.laptops, R.drawable.flashusb, R.drawable.networking, R.drawable.cables, R.drawable.
peripherals, R.drawable.soundandvision, R.drawable.software};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


iv = (ImageView)findViewById(R.id.ImageView01);
iv.setImageResource(R.drawable.logo);
iv.setVisibility(View.VISIBLE);
lview3 = (ListView) findViewById(R.id.listView3);
adapter = new ListViewCustomAdapter(main.this, productTypes, productsIncluded, images);
lview3.setAdapter(adapter);
lview3.setOnItemClickListener(main.this);

}

@覆盖 public void onItemClick(AdapterView arg0, View arg1, int position, long id) {

     selectedType = "";                    

if(position == 0)
{
selectedType= "Monitors";
}
if(position == 1)
{
selectedType= "Hard Drives";
}
Toast.makeText(this, Integer.toString(position), Toast.LENGTH_SHORT).show();
new async().execute();

/*
Intent intent = new Intent(getApplicationContext(),activitywo.class);
Bundle bundle =new Bundle();
bundle.putString("selectedType",selectedType);
intent.putExtras(bundle);
startActivity(intent);
//Toast.makeText(this, "Title => "+productTypes[position]+" \n Description => "+productsIncluded[position], Toast.LENGTH_SHORT).show();
*/
}
private class async extends AsyncTask<String, Void, Void> {

// UI Thread
protected void onPreExecute() {

}

// automatically done on worker thread (separate from UI thread)
protected Void doInBackground(final String... args) {

try{

Resources res = getResources();
InputStream in;
in = res.openRawResource(R.raw.core);

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document dom = builder.parse(in);
Element root = dom.getDocumentElement();
NodeList items = root.getElementsByTagName("entry");


count=0;
for(int i =0;i<items.getLength();i++){
Node item = items.item(i);
NodeList properties = item.getChildNodes();
//productsDefinedByTypeArray = new String[properties.getLength()];


for(int j = 0;j<properties.getLength();j++){
Node property = properties.item(j);
String name = property.getNodeName();

if(name.equalsIgnoreCase("g:product_type")){//everytime we hit g:product_type grab the value
String strText = property.getFirstChild().getNodeValue();

if(strText.contains(selectedType)){
//
for(int k = 0;k<properties.getLength();k++){
Node propertysecond = properties.item(k);
String namesecond = propertysecond.getNodeName();
if(namesecond.equalsIgnoreCase("title")){//everytime we hit title grab the value
String strTextsecond = propertysecond.getFirstChild().getNodeValue();
productTypes[0] = strTextsecond;

yo = strTextsecond;
count++;
}
}
}
}

}


}

当我试图将我从 xml 文件中解析出的“title”节点的值复制到 list-String- 时,代码崩溃了。我正在使用 list-string- 来表明即使您尝试将值复制到数组本身(与适配器关联的数组),即使您注释掉 notifydatasetchanged () 行程序仍然会崩溃。 xml 格式正确且非常一致。我知道这是因为(尽管它真的很小而且我已经全部阅读了)!为什么我不能在循环进行时将每个节点值复制到我的数组/列表中?

非常感谢任何帮助。谢谢。

较短的版本:

我无法写入列表

 if(strText.contains(selectedType)){
//
for(int k = 0;k<properties.getLength();k++){
property = properties.item(k);
name = property.getNodeName();
if(name.equalsIgnoreCase("title")){//everytime we hit title grab the value
String strTextsecond = property.getFirstChild().getNodeValue().toString();


ls.add(strTextsecond.toString());

//test = strTextsecond;

}

}

}

最佳答案

也许你忘了初始化ls?我没有在您的代码中找到类似的内容:

ls = new List<String>();

关于java - android xml节点解析数组适配器很奇怪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6576574/

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