Listview is not working

Put your problem here if it does not fit any of the other categories.

Listview is not working

Postby reza » Wed Feb 16, 2011 11:13 am

Hi.
I am trying to read an XML from web and display them in a listview. The listview I am trying to display will be multi line.
But I do not know why, I do not see the listview. I see only a blank screen but no data.
I am writing my code below. Will be a great help if anyone can help me to identify my mistakes.

Layout: custom_list_view.xml
Code: Select all
<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <TextView android:id="@android:id/text1"
        android:layout_marginTop="1dip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        android:textStyle="bold" />

    <TextView android:id="@android:id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@android:id/text1"
        android:layout_alignLeft="@android:id/text1"
        android:paddingBottom="4dip"
        android:includeFontPadding="false"
        android:textSize="15sp"
        android:textStyle="normal" />

</TwoLineListItem>


Class: SearchItem
Simple getter setter

Code: Select all
public class SearchItem {
   private String itemName;
   private String itemAddress;
   private double latitude;
   private double longitude;
   
   public SearchItem(){}
   
   public String getItemName(){
      return this.itemName;
   }
   public void setItemName(String itemName){
      this.itemName=itemName;
   }
   public String getItemAddress(){
      return this.itemAddress;
   }
   public void setItemAddress(String itemAddress){
      this.itemAddress=itemAddress;
   }
   public double getLatitude(){
      return this.latitude;
   }
   public void setLatitude(double lat){
      this.latitude=lat;
   }
   public double getLongitude(){
      return this.longitude;
   }
   public void setLongitude(double lon){
      this.longitude=lon;
   }

}


Class: Handler
Code: Select all
public class SearchItemHandler extends DefaultHandler{
   private boolean in_itemName=false;
   private boolean in_itemAddress=false;
   private boolean in_itemLatitude=false;
   private boolean in_itemLongitude=false;
   private boolean in_business=false;
   private boolean in_local=false;
   
   public ArrayList<SearchItem> itemList;

   private SearchItem sItem;
   
   @Override
    public void startDocument() throws SAXException {
            this.sItem = new SearchItem();
    }

    @Override
    public void endDocument() throws SAXException {
            // Nothing to do
    }
    @Override
    public void startElement(String namespaceURI,String localname,String qName,Attributes atts)throws SAXException{
       if(localname.equalsIgnoreCase("Business")){
          
          this.in_business=true;
       }else if(localname.equalsIgnoreCase("local")){
          sItem=new SearchItem();
          this.in_local=true;
       }else if(localname.equalsIgnoreCase("ItemName")){
          String itemname=atts.getValue("name");
          sItem.setItemName(itemname);
          
          this.in_itemName=true;
       }else if(localname.equalsIgnoreCase("ItemAddress")){
          String itemaddress=atts.getValue("address");
          sItem.setItemAddress(itemaddress);
          
          this.in_itemAddress=true;
       }else if(localname.equalsIgnoreCase("Latitude")){
          double lat=Double.parseDouble(atts.getValue("lat"));
          sItem.setLatitude(lat);
          this.in_itemLatitude=true;
       }else if(localname.equalsIgnoreCase("Longitude")){
          double lon=Double.parseDouble(atts.getValue("lon"));
          sItem.setLatitude(lon);
          this.in_itemLongitude=true;
    }
       
}
    @Override
    public void endElement(String namespaceURI, String localname, String qName)throws SAXException {
       if(localname.equalsIgnoreCase("Business")){
          this.in_business=false;
       }else if(localname.equalsIgnoreCase("local")){
          this.in_local=false;
       }else if(localname.equalsIgnoreCase("ItemName")){
          this.in_itemName=false;
       }else if(localname.equalsIgnoreCase("ItemAddress")){
          this.in_itemAddress=false;
       }else if(localname.equalsIgnoreCase("Latitude")){
          this.in_itemLatitude=false;
       }else if(localname.equalsIgnoreCase("Longitude")){
          this.in_itemLongitude=false;
       }   
}
    @Override
    public void characters(char ch[], int start, int length) {
                if(this.in_local){
               
        }
    }
    public ArrayList<SearchItem> getItems(){
      itemList.add(sItem);
      return itemList;
   }
}
   


Class: adapter
Code: Select all
public class SearchArrayAdapter extends ArrayAdapter{
   
   private final int resourceId;
   public SearchArrayAdapter(Context context,int textViewResourceId, List objects){
      super(context, textViewResourceId, objects);
      resourceId = textViewResourceId;
   }
   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
      SearchItem s=(SearchItem) getItem(position);
      if(s==null){
         return null;
      }
      LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      TwoLineListItem view;
      if (convertView == null) {
         view = (TwoLineListItem) inflater.inflate(resourceId, parent, false);
      } else {
         view = (TwoLineListItem) convertView;
      }
      if(view.getText1()!=null){
         view.getText1().setText(s.getItemName());
      }
      if(view.getText2()!=null){
         view.getText2().setText(s.getItemAddress());
      }
      return view;
   }
}


Class: main activity
Code: Select all
public class SearchItemListActivity extends ListActivity{
   String param="Bar";
   @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      
   try {
       URL url = new URL("http://www.webitour.dk/service/localbusiness.aspx?cat="+param);
       SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();

        /* Get the XMLReader of the SAXParser we created. */
        XMLReader xr = sp.getXMLReader();
        /* Create a new ContentHandler and apply it to the XML-Reader*/
        SearchItemHandler searchHandler=new SearchItemHandler();
        xr.setContentHandler(searchHandler);
        xr.parse(new InputSource(url.openStream()));
               
   setListAdapter(new SearchArrayAdapter(this, R.layout.custom_list_view, searchHandler.getItems()));       
   }catch(Exception e){
   }
}

}
reza
Freshman
Freshman
 
Posts: 9
Joined: Sat Jan 22, 2011 12:29 am

Top

Re: Listview is not working

Postby urbantrad » Wed Feb 16, 2011 10:36 pm

On first look, one very suspicious thing is in the Handler: You create a single SearchItem, and then fill in the data for it from the enitre XML document? I'd expect that you create a new SearchItem whenever a new Item is started in the XML file.
If one file is an entire XML file, still i'd expect you to add the Item to the list after parsing is finished, instead of requiring the results to be requested for it to be added.
urbantrad
Senior Developer
Senior Developer
 
Posts: 104
Joined: Thu Sep 09, 2010 10:19 pm

Re: Listview is not working

Postby reza » Thu Feb 17, 2011 12:12 am

Hi.. Thanks for the reply. yes i also think it is my handler which is problematic.
My XML is
Code: Select all
<Business>
- <Local>
  <ItemName name="The Piano Bar" />
  <ItemAddress address="Norrebrogade 132" />
  <Longitude lon="52.012365" />
  <Latitude lat="25.32104" />
  </Local>
- <Local>
  <ItemName name="Reza's Bar" />
  <ItemAddress address="Norrebrogade 150" />
  <Longitude lon="52.32654" />
  <Latitude lat="25.01456" />
  </Local>
- <Local>
  <ItemName name="JD weatherspoon" />
  <ItemAddress address="Frederiksundvej 25,2 tv" />
  <Longitude lon="52.01565" />
  <Latitude lat="25.02321" />
  </Local>
  </Business>

I am creating a single object when the parser finds <Local> tag. and I am adding the object to the list when the parser finds the </Local> end tag.
and I changed my handler to as the following;
But still I can not see anything.
Code: Select all
public class SearchItemHandler extends DefaultHandler{
   private boolean in_itemName=false;
   private boolean in_itemAddress=false;
   private boolean in_itemLatitude=false;
   private boolean in_itemLongitude=false;
   private boolean in_business=false;
   private boolean in_local=false;
   
   public ArrayList<SearchItem> itemList;

   private SearchItem sItem;
   public SearchItem getParsedData(){
      return this.sItem;
   }
   
   @Override
    public void startDocument() throws SAXException {
            this.sItem = new SearchItem();
    }

    @Override
    public void endDocument() throws SAXException {
            // Nothing to do
    }
    @Override
    public void startElement(String namespaceURI,String localname,String qName,Attributes atts)throws SAXException{
       if(localname.equalsIgnoreCase("Business")){
          
          this.in_business=true;
       }else if(localname.equalsIgnoreCase("local")){
          sItem=new SearchItem();//Creating a single item.
          this.in_local=true;
       }else if(localname.equalsIgnoreCase("ItemName")){
          
          String itemname=atts.getValue("name");
          sItem.setItemName(itemname);
          
          this.in_itemName=true;
       }else if(localname.equalsIgnoreCase("ItemAddress")){
          String itemaddress=atts.getValue("address");
          sItem.setItemAddress(itemaddress);
          
          this.in_itemAddress=true;
       }else if(localname.equalsIgnoreCase("Latitude")){
          double lat=Double.parseDouble(atts.getValue("lat"));
          sItem.setLatitude(lat);
          this.in_itemLatitude=true;
       }else if(localname.equalsIgnoreCase("Longitude")){
          double lon=Double.parseDouble(atts.getValue("lon"));
          sItem.setLatitude(lon);
       
          this.in_itemLongitude=true;
    }       
}
    @Override
    public void endElement(String namespaceURI, String localname, String qName)throws SAXException {
       if(localname.equalsIgnoreCase("Business")){
          this.in_business=false;
       }else if(localname.equalsIgnoreCase("local")){
          addItem(); //adding a single item to the list.
          this.in_local=false;
       }else if(localname.equalsIgnoreCase("ItemName")){
          this.in_itemName=false;
       }else if(localname.equalsIgnoreCase("ItemAddress")){
          this.in_itemAddress=false;
       }else if(localname.equalsIgnoreCase("Latitude")){
          this.in_itemLatitude=false;
       }else if(localname.equalsIgnoreCase("Longitude")){
          this.in_itemLongitude=false;
       }   
}
    @Override
    public void characters(char ch[], int start, int length) {
                if(this.in_local){
                //currentCondition.(new String(ch, start, length));
               // sItem.equals(new String(ch,start,length));
        }
    }
    public ArrayList<SearchItem> getItems(){
      //itemList.add(sItem);
      return this.itemList;
   }
    public void addItem(){
       itemList.add(sItem);
    }
}
reza
Freshman
Freshman
 
Posts: 9
Joined: Sat Jan 22, 2011 12:29 am

Re: Listview is not working

Postby urbantrad » Thu Feb 17, 2011 3:25 pm

I can't immeditaly see anything wrong now. The only thing that looks a bit weird is that you request the XMLReader from your SAXParser, instead of just using the parse function of the parser. I think this would be cleaner, but I don't think any of the functionality will be different. You'll have to use debug tools to find out where the problem is.
urbantrad
Senior Developer
Senior Developer
 
Posts: 104
Joined: Thu Sep 09, 2010 10:19 pm

Re: Listview is not working

Postby reza » Fri Feb 18, 2011 4:42 pm

Hi urbantrad
Thanks for your effort. The problem has been solved. That was a silly mistake.
I declared this:
public ArrayList<SearchItem> itemList

But forgot the following small little important thing
public ArrayList<SearchItem> itemList=new ArrayList<SearchItem>();

:)
Thanks dude.
reza
Freshman
Freshman
 
Posts: 9
Joined: Sat Jan 22, 2011 12:29 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: MSNbot Media and 18 guests