2011-05-18 49 views
0

我已經完成xml解析並能夠在列表視圖中顯示一個數組項目。但是我想在一個列表中顯示2個數組項目...使用數組適配器...意味着我想要array_barrio在一個列表視圖我怎麼能做到這一點...我的Java類在sax xml解析後在列表視圖中顯示項目

public class XMLParsingExample extends ListActivity { 
    String name = null; 
    private String array_estado[]; 
    private String[] array_ciudad; 
    private String array_barrio[]; 
    /** Create Object For SiteList Class */ 
    SitesList sitesList = null; 
    /** Called when the activity is first created. */ 
    @SuppressWarnings("unchecked") 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.listplaceholder); 
     /** Create a new layout to display the view */ 
     LinearLayout layout = new LinearLayout(this); 
     layout.setOrientation(1); 
    try { 

      SAXParserFactory spf = SAXParserFactory.newInstance(); 
      SAXParser sp = spf.newSAXParser(); 
      XMLReader xr = sp.getXMLReader(); 

      /** Send URL to parse XML Tags */ 
      URL sourceUrl = new URL(
        "http://www.arteonline.mobi/iphone/output.php?key=Buenos"); 

      MyXMLHandler myXMLHandler = new MyXMLHandler(); 
      xr.setContentHandler(myXMLHandler); 
      xr.parse(new InputSource(sourceUrl.openStream())); 

     } catch (Exception e) { 
      System.out.println("XML Pasing Excpetion = " + e); 
     } 

     sitesList = MyXMLHandler.sitesList; 


     array_ciudad = new String[sitesList.getEstado().size()]; 
     for (int i = 0; i < sitesList.getEstado().size(); i++) 
     { 
      name = sitesList.getEstado().get(i); 
      array_ciudad[i] = name; 
      Log.i("array_spinner" + i, array_ciudad[i]); 
     } 
     array_barrio = new String[sitesList.getBarrio().size()]; 
     for (int i = 0; i < sitesList.getBarrio().size(); i++) 
     { 
      name = sitesList.getBarrio().get(i); 
      array_barrio[i] = name; 
      // Log.i("array_spinner" + i, array_ciudad[i]); 
     } 

     setListAdapter(new ArrayAdapter(this, 
        android.R.layout.simple_list_item_1, array_barrio)); 

//  setListAdapter(new ArrayAdapter(this, 
//    android.R.layout.simple_list_item_1, array_barrio)); 

} 

回答

0

您可以array_ciudad既是一個新的String []這是大到足以容納兩個,然後複製每個元素數組添加到新數組。在你的ArrayAdapter中使用新的數組。

String[] bigArray = new String[array_barrio.length + array_ciudad.length]; 
System.arraycopy(array_barrio, 0, bigArray, 0, array_barrio.length); 
System.arraycopy(array_ciudad, array_barrio.length, bigArray, array_barrio.length, array_ciudad.length); 

另一個值得考慮的事情是將剛剛創建bigArray最初,只是這兩組元素添加到大數組,而不是小的。

+0

@尼古拉斯我用上面的行,並通過大陣列以陣列適配器它ForSE召開前夕靠近.. setListAdapter(新ArrayAdapter(此, \t android.R.layout.simple_list_item_1,bigArray)); – 2011-05-18 05:36:31

+0

05-18 11:31:30.015:ERROR/AndroidRuntime(753):由於:java.lang.ArrayIndexOutOfBoundsException:src.length = 3 srcPos = 3 dst.length = 6 dstPos = 3 length = 3 – 2011-05-18 06:03:38

+0

親愛的爲什麼它是不工作?怎麼回事? – 2011-05-18 06:18:29

0

是否要合併兩個數組項並將其合併爲一個。那麼你可以按照尼古拉斯的方式。添加數組並將其作爲一個數組並將數組傳遞給ArrayAdapter。但是如果你想要在同一個列表中顯示兩個數組的數據,那麼你必須使用BaseAdapter併爲此編寫自己的邏輯。

感謝

迪帕克

+0

你好deepak ..當我結合兩個數組項目,並使其成爲一個,然後它也顯示在同一列表中的數組項目...和你說的第二個技巧,顯示兩個數組在同一個列表中的數據,也顯示數據在相同的列表中,那麼兩者之間會有什麼差別...請詳細解釋... – 2011-05-18 05:45:50

+0

意味着在將數組組合使用後,使用陣列適配器時需要作爲數組使用基本適配器時,我們可以在同一列表視圖中實現多個1 0r2項手段可以改變不。在同一列表視圖中的行? – 2011-05-18 06:24:24

0

如果你想在一個列表視圖項顯示多個項目,您可以通過擴展BaseAdapter使用自己的適配器。但首先array_ciudadarray_barrio將需要是相同的大小。

每個項目需要用下面的列表項目佈局表示。此佈局需要放置在res/layout目錄中。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 


    <!-- Location where barrio items will be displayed -->       
    <TextView android:id="@+id/barrio" 
     android:layout_height="wrap_content"   
     android:layout_width="wrap_content" /> 

    <!-- Location where ciudad items will be displayed -->       
    <TextView android:id="@+id/ciudad"     
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" />      

</LinearLayout> 

本示例自定義適配器假定array_barrio和array_ciudad長度相同。您真正需要做的就是通過膨脹佈局並在需要的地方將字符串插入到TextView中來爲列表項創建視圖。在這個例子中MyAdapter應該是一個XMLParsingExample的內部類,它可以直接訪問你定義的數組屬性。

protected class MyAdapter extends BaseAdapter { 

    private LayoutInflater inflater; 

    public MyAdapter(Context ctx) { 
     super(); 
     this.inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    }   

    @Override 
    public int getCount() { 
     return XMLParsingExample.this.array_barrio.length; 
    } 

    /* Not implemented but not really needed */ 
    @Override 
    public Object getItem(int position) { 
     return null; 
    } 

    /* Not implemented but not really needed */ 
    @Override 
    public long getItemId(int position) { 
     return 0; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View v = inflater.inflate(R.layout.listitem_layout, parent, false); 
     Course c = courses.get(position); 

     String barrio = XMLParsingExample.this.array_barrio[position]; 
     String ciudad = XMLParsingExample.this.array_ciudad[position];    

     TextView tv = (TextView) v.findViewById(R.id.barrio); 
     tv.setText(barrio); 
     tv = (TextView) v.findViewById(R.id.ciudad); 
     tv.setText(ciudad); 

     return v; 
    } 

} 
+0

正如@Deepak所說,如果你想結合兩個數組,那麼就使用@ nicholas.hauschild的方法。但是,如果您想在一個列表項中顯示兩個或更多項目/字符串,這應該會很有幫助。 – stafford 2011-05-18 05:46:17

+0

親愛的先生...首先v v感謝您使用完整答案..將XMLParsingExample.java類中的任何更改????所以不使用陣列適配器inXMLParsingExample.java ??? – 2011-05-18 05:59:47

+0

@ atul-yadav只要您的XML解析工作正常,您將需要的唯一更改就是將'MyAdapter'類添加爲'XMLParsingExample'中的內部類。那麼不要使用'ArrayAdapter'來代替使用'new MyAdapter(this)'。如果你使用'ListActivity'進行壓縮,那麼在'onCreate'結尾你需要'setListAdapter(adapter);'。 – stafford 2011-05-18 06:06:29