开发者

android xml node parse array adapter weird

I have an odd problem, (see shorter version at the bottom first please)

When my activity starts for the first time, the listview shows the dataset fine. The adapter is a custom adapter which shows 2 rows of text and an image. I call an asynctask upon a click event to the listview, the dataset updates in accordance with whatever was clicked on in the listview - more specifically the arrays which are associated with the adapter become rewritten with the parsings of some xml, and then notifyachapterdatasetchanged method is called to update the listview in the onPostExecute function. However I always get NullPointerException when I am iterating through some xml (which is very well formed and validates). ALso its worth mentioing that the algorithm that parses the desired values is good, because as mentioned above, if i write to just 1 element of the array then I dont get the error I just get the last node value so its looping in the correct places. i.e If I simple try to copy the current node value I am parsing during the loop into, say, producyTypes[0] then the last value from within the loop actually makes it to the listview as it constantly overwrites this elemet of the array

Here is my code.`

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

}

@Override 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++;
                                }                                    
                            }                                
                        }
                    }

                }


            }

The code crashes at the point where I am trying to copy the value of a "title" node that I parse out of my xml file into the list-String-. I am using a list-string- to show that even if you try and copy the value into the array itself (the array that is associated with the adapter), even if you comment out the notifydatasetchanged () line the program still crashes. The xml is well formed and very consistent. I know this because (aecept the fact its really small and I have read it all) ! Why can I not copy every node value into my array/list whilst the loop is in progress?

Any help is massively appreciated. Thank you.

Shorter version:

I cannot write to the List

 if(strText.contains(selectedType)){
                   //        
        开发者_Go百科                     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;

                                 }   

                             }         

                         }


Maybe you forget initialize ls? I don't find in your code something like:

ls = new List<String>();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜