2017-05-27 72 views
0

幫我使用此代碼。我解析XML文件,將對象添加到ArrayList,然後在recyclerview中顯示信息,但只有一個對象顯示,儘管ArrayList包含5個對象。我如何顯示所有對象?謝謝。這裏是我的代碼將Xml解析爲本地文件並將其顯示爲回收站視圖

public class WorldRegionsAdapter extends RecyclerView.Adapter<WorldRegionsAdapter.WorldRegionsHolder> { 

private static final String TAG = WorldRegionsAdapter.class.getCanonicalName(); 
public List<WorldRegion> worldRegionList; 
private Context context; 

public WorldRegionsAdapter(Context context, List<WorldRegion> worldRegionList) { 
    this.context = context; 
    this.worldRegionList = worldRegionList; 
} 

@Override 
public WorldRegionsHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { 
    LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext()); 
    View view = inflater.inflate(R.layout.world_region, viewGroup, false); 
    return new WorldRegionsHolder(view); 
} 

@Override 
public void onBindViewHolder(WorldRegionsHolder worldRegionsHolder, int position) { 
    WorldRegion worldRegion = worldRegionList.get(position); 
    worldRegionsHolder.ivWorld.setImageResource(R.drawable.ic_world_globe_dark); 
    worldRegionsHolder.tvRegion.setText(worldRegion.getRegionName()); 
} 

@Override 
public int getItemCount() { 
    return worldRegionList.size(); 
} 

public static class WorldRegionsHolder extends RecyclerView.ViewHolder { 

    @BindView(R.id.iv_world_globe) 
    ImageView ivWorld; 
    @BindView(R.id.tv_region) 
    TextView tvRegion; 

    public WorldRegionsHolder(View itemView) { 
     super(itemView); 
     ButterKnife.bind(this, itemView); 
    } 
} 

}

public class WorldRegion { 

public String polyExtract; 
public String regionType; 
public String regionName; 

public WorldRegion() { 
} 

public WorldRegion(String region, String regionType, String regionName) { 
    this.polyExtract = region; 
    this.regionType = regionType; 
    this.regionName = regionName; 
} 

public String getPolyExtract() { 
    return polyExtract; 
} 

public void setPolyExtract(String polyExtract) { 
    this.polyExtract = polyExtract; 
} 

public String getRegionType() { 
    return regionType; 
} 

public void setRegionType(String regionType) { 
    this.regionType = regionType; 
} 

public String getRegionName() { 
    return regionName; 
} 

public void setRegionName(String regionName) { 
    this.regionName = regionName; 
} 

@Override 
public String toString() { 
    return "WorldRegion{" + 
      "polyExtract='" + polyExtract + '\'' + 
      ", regionType='" + regionType + '\'' + 
      ", regionName='" + regionName + '\'' + 
      '}'; 
} 

}

public class DownloadMapsActivity extends AppCompatActivity { 

private static final String FREE_DEVICE_MEMORY = "Free "; 
private WorldRegionsAdapter adapter; 
private List<WorldRegion> worldRegionList; 

// All static variables 
static final String KEY_URL = "https://github.com/osmandapp/OsmAnd-resources/blob/master/countries-info/regions.xml"; 
// XML node keys 
static final String KEY_TYPE = "type"; // parent node 
static final String KEY_NAME = "name"; 
static final String KEY_POLY_EXTRACT = "poly_extract"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_download_maps); 

    TextView tvMemory = (TextView) findViewById(R.id.tv_free_memory); 
    String devMemory = DeviceMemory.getTotalInternalMemorySize(); 
    tvMemory.setText(FREE_DEVICE_MEMORY + devMemory); 

    ProgressBar pbMemory = (ProgressBar) findViewById(R.id.pb_memory); 
    pbMemory.setScaleY(3f); 
    int numDevMemory = Integer.parseInt(devMemory.replaceAll("[\\D]", "")); 
    pbMemory.setProgress(numDevMemory); 

    Uri uri = Uri.parse("https://github.com/osmandapp/OsmAnd-resources/blob/master/countries-info/regions.xml"); 
    File getFileName = new File("" + uri); 
    getFileName.getName(); 

    worldRegionList = new ArrayList<>(); 
    WorldRegion worldRegion = new WorldRegion(); 

    RecyclerView rvWorldRegions = (RecyclerView) findViewById(R.id.rv_world_regions); 

    rvWorldRegions.setLayoutManager(new LinearLayoutManager(this)); 

    WorldRegionsAdapter adapter = new WorldRegionsAdapter(this, worldRegionList); 
    rvWorldRegions.setAdapter(adapter); 
    try { 
     XmlPullParser parser = getResources().getXml(R.xml.contacts); 
     String name = null; 
     while (parser.getEventType() != XmlPullParser.END_DOCUMENT) { 
      if (parser.getEventType() == XmlPullParser.START_TAG 
        && parser.getName().equals("region")) { 
       if (parser.getAttributeValue(null, "type").equalsIgnoreCase("continent")) { 
       name = parser.getAttributeValue(null, "name"); 
       worldRegion.setRegionName(name); 
       worldRegionList.add(worldRegion); 
       } 
      } 
      parser.next(); 
      adapter.notifyDataSetChanged(); 
     } 
    } catch (Throwable t) { 
    } 
} 

}

和我的XML文件

<?xml version="1.0" encoding="utf-8"?> 

<!-- north-europe --> 
    <region name="denmark" lang="da" poly_extract="north-europe"/> 
    <region name="estonia" lang="et" poly_extract="north-europe"/> 
    <region name="iceland" lang="is" poly_extract="north-europe"/> 
    <region name="faroe-islands" translate="Faroe Islands;entity=node" lang="fo" poly_extract="north-europe"/> 
    <region name="finland" inner_download_prefix="$name" srtm="no" lang="fi,sv" poly_extract="north-europe"> 
     <region map="no" name="aland" translate="name:fi=Ahvenanmaa;admin_level=3;entity=relation"/> 
     <region type="srtm" name="eastern-finland" translate="Eastern Finland;entity=relation"/> 
     <region name="northern-finland" map="no" srtm="no" hillshade="no" wiki="no" translate="name:en=Northern Finland;entity=relation" boundary="no"> 
      <region type="srtm" name="lapland" translate="Lapland;entity=relation"/> 
      <region type="srtm" name="northern-ostrobothnia" translate="name:en=Northern Ostrobothnia;entity=relation"/> 
     </region> 
     <region type="srtm" name="southern-finland" translate="Southern Finland;entity=relation"/> 
     <region type="srtm" name="western-finland" translate="Western Finland;entity=relation"/> 
    </region> 
    <region name="latvia" lang="lv" poly_extract="north-europe"/> 
    <region name="lithuania" lang="lt" poly_extract="north-europe"/> 
    <region name="norway" srtm="no" inner_download_prefix="$name" lang="nb,nn" poly_extract="north-europe" join_map_files="yes"> 
     <region srtm="yes" hillshade="no" wiki="no" name="akershus"/> 
     <region srtm="yes" hillshade="no" wiki="no" name="astfold" translate="Østfold;entity=node"/> 
     <region srtm="yes" hillshade="no" wiki="no" name="aust-agder"/> 
     <region srtm="yes" hillshade="no" wiki="no" name="buskerud"/> 
     <region srtm="yes" hillshade="no" wiki="no" name="finnmark"/> 
     <region srtm="yes" hillshade="no" wiki="no" name="hedmark"/> 
     <region srtm="yes" hillshade="no" wiki="no" name="hordaland"/> 
     <region srtm="yes" hillshade="no" wiki="no" name="more-og-romsdal" translate="Møre og Romsdal;entity=node"/> 
     <region srtm="yes" hillshade="no" wiki="no" name="nordland"/> 
     <region srtm="yes" hillshade="no" wiki="no" name="nord-trondelag" translate="Nord-Trøndelag;entity=node"/> 
     <region srtm="yes" hillshade="no" wiki="no" name="oppland"/> 
     <region srtm="yes" hillshade="no" wiki="no" name="oslo"/> 
     <region srtm="yes" hillshade="no" wiki="no" name="rogaland"/> 
     <region srtm="yes" hillshade="no" wiki="no" name="sogn-og-fjordane" translate="Sogn og Fjordane;entity=node"/> 
     <region srtm="yes" hillshade="no" wiki="no" name="sor-trondelag" translate="Sør-Trøndelag;entity=node"/> 
     <region srtm="yes" hillshade="no" wiki="no" name="telemark"/> 
     <region srtm="yes" hillshade="no" wiki="no" name="troms"/> 
     <region srtm="yes" hillshade="no" wiki="no" name="vest-agder"/> 
     <region srtm="yes" hillshade="no" wiki="no" name="vestfold"/> 
     <region name="svalbard-and-jan-mayen" translate="Svalbard;entity=relation"/> 
    </region> 
    <region name="sweden" inner_download_prefix="$name" srtm="no" lang="sv" poly_extract="north-europe"> 
     <region map="yes" srtm="yes" hillshade="no" wiki="no" name="blekinge" translate="Blekinge län;entity=relation"/> 
     <region map="yes" srtm="yes" hillshade="no" wiki="no" name="dalarna" translate="Dalecarlia;entity=relation"/> 
     <region map="yes" srtm="yes" hillshade="no" wiki="no" name="gavleborg" translate="Gävleborgs län;entity=relation"/> 
     <region map="yes" srtm="yes" hillshade="no" wiki="no" name="gotland" translate="Gotlands län;entity=relation"/> 
     <region map="yes" srtm="yes" hillshade="no" wiki="no" name="halland" translate="Hallands län;entity=relation"/> 
     <region map="yes" srtm="yes" hillshade="no" wiki="no" name="jamtland" translate="Jämtlands län;entity=relation"/> 
     <region map="yes" srtm="yes" hillshade="no" wiki="no" name="jonkoping" translate="Jönköpings län;entity=relation"/> 
     <region map="yes" srtm="yes" hillshade="no" wiki="no" name="kalmar" translate="Kalmar län;entity=relation"/> 
     <region map="yes" srtm="yes" hillshade="no" wiki="no" name="kronoberg" translate="Kronobergs län;entity=relation"/> 
     <region map="yes" srtm="yes" hillshade="no" wiki="no" name="norrbotten" translate="Norrbottens län;entity=relation"/> 
     <region map="yes" srtm="yes" hillshade="no" wiki="no" name="orebro" translate="Örebro län;entity=relation"/> 
     <region map="yes" srtm="yes" hillshade="no" wiki="no" name="ostergotland" translate="Östergötlands län;entity=relation"/> 
     <region map="yes" srtm="yes" hillshade="no" wiki="no" name="skane" translate="Skåne län;entity=relation"/> 
     <region map="yes" srtm="yes" hillshade="no" wiki="no" name="sodermanland" translate="Södermanlands län;entity=relation"/> 
     <region map="yes" srtm="yes" hillshade="no" wiki="no" name="stockholm" translate="Stockholms län;entity=relation"/> 
     <region map="yes" srtm="yes" hillshade="no" wiki="no" name="uppsala" translate="Uppsala län;entity=relation"/> 
     <region map="yes" srtm="yes" hillshade="no" wiki="no" name="varmland" translate="Värmlands län;entity=relation"/> 
     <region map="yes" srtm="yes" hillshade="no" wiki="no" name="vasterbotten" translate="Västerbottens län;entity=relation"/> 
     <region map="yes" srtm="yes" hillshade="no" wiki="no" name="vasternorrland" translate="Västernorrlands län;entity=relation"/> 
     <region map="yes" srtm="yes" hillshade="no" wiki="no" name="vastmanland" translate="Västmanlands län;entity=relation"/> 
     <region map="yes" srtm="yes" hillshade="no" wiki="no" name="vastra-gotaland" translate="Västra Götalands län;entity=relation"/> 
    </region> 

    <!-- east-europe --> 
    <region name="belarus" lang="be,ru" poly_extract="east-europe"/> 
    <region name="bulgaria" lang="bg" poly_extract="east-europe"/> 
    <region name="czech-republic" inner_download_prefix="$name" translate="Czech Republic;entity=node" lang="cs,sk" poly_extract="east-europe" join_map_files="yes"> 
     <region name="jihovychod" srtm="no" hillshade="no" wiki="no" translate="name:en=Southeast;ref=CZ06;entity=relation"/> 
     <region name="jihozapad" srtm="no" hillshade="no" wiki="no" translate="name:en=Southwest;ref=CZ03;entity=relation"/> 
     <region name="moravskoslezsko" srtm="no" hillshade="no" wiki="no" translate="name:en=Moravia-Silesia;entity=relation"/> 
     <region name="praha" srtm="no" hillshade="no" wiki="no" translate="name:en=Prague;entity=relation"/> 
     <region name="severovychod" srtm="no" hillshade="no" wiki="no" translate="name:en=Northeast;ref=CZ05;entity=relation"/> 
     <region name="severozapad" srtm="no" hillshade="no" wiki="no" translate="name:en=Northwest;ref=CZ04;entity=relation"/> 
     <region name="stredni-cechy" srtm="no" hillshade="no" wiki="no" translate="name:en=Central Bohemia;entity=relation"/> 
     <region name="stredni-morava" srtm="no" hillshade="no" wiki="no" translate="name:en=Central Moravia;entity=relation"/> 
    </region> 
    <region name="hungary" lang="hu" poly_extract="east-europe"/> 
    <region name="moldova" lang="ro" poly_extract="east-europe"/> 
    <region name="poland" inner_download_prefix="$name" lang="pl" poly_extract="east-europe" join_map_files="yes"> 
     <region name="greater-poland" srtm="no" hillshade="no" wiki="no" translate="Greater Poland;entity=node"/> 
     <region name="kuyavian-pomeranian" srtm="no" hillshade="no" wiki="no" translate="Kuyavian-Pomerania;entity=node"/> 
     <region name="lesser-poland" srtm="no" hillshade="no" wiki="no" translate="Lesser Poland;entity=node"/> 
     <region name="lodz" srtm="no" hillshade="no" wiki="no" translate="Łódź;entity=node"/> 
     <region name="lower-silesian" srtm="no" hillshade="no" wiki="no" translate="Lower Silesia;entity=node"/> 
     <region name="lublin" srtm="no" hillshade="no" wiki="no" translate="name:en=Lublin Voivodeship;entity=relation"/> 
     <region name="lubusz" srtm="no" hillshade="no" wiki="no"/> 
     <region name="masovian" srtm="no" hillshade="no" wiki="no" translate="Masovia;entity=node"/> 
     <region name="opole" srtm="no" hillshade="no" wiki="no" translate="Opole Voivodeship;entity=node"/> 
     <region name="podlachian" srtm="no" hillshade="no" wiki="no" translate="Podlachia;entity=node"/> 
     <region name="pomeranian" srtm="no" hillshade="no" wiki="no" translate="Pomerania;entity=node"/> 
     <region name="silesian" srtm="no" hillshade="no" wiki="no" translate="Silesia;entity=node"/> 
     <region name="subcarpathian" srtm="no" hillshade="no" wiki="no" translate="Subcarpathia;entity=node"/> 
     <region name="swietokrzyskie" srtm="no" hillshade="no" wiki="no" translate="Holy Cross;entity=node"/> 
     <region name="warmian-masurian" srtm="no" hillshade="no" wiki="no" translate="Warmian-Masurian Voivodeship;entity=node"/> 
     <region name="west-pomeranian" srtm="no" hillshade="no" wiki="no" translate="West Pomerania;entity=node"/> 
    </region> 
    <region name="romania" lang="ro" poly_extract="east-europe"/> 
    <region name="slovakia" lang="sk" poly_extract="east-europe"/> 
    <region name="transnistria" hillshade="no" lang="ru,uk,ro" poly_extract="east-europe"/> 
    <region name="ukraine" inner_download_prefix="$name" lang="uk,ru" poly_extract="east-europe"> 
     <region name="cherkasy" hillshade="no" srtm="no" wiki="no" translate="name:en=Cherkasy Oblast;entity=relation"/> 
     <region name="chernihiv" hillshade="no" srtm="no" wiki="no" translate="name:en=Chernihiv Oblast;entity=relation"/> 
     <region name="chernivtsi" hillshade="no" srtm="no" wiki="no" translate="name:en=Chernivtsi Oblast;entity=relation"/> 
     <region name="crimea" hillshade="no" srtm="no" wiki="no" boundary="ukraine/crimea" translate="name:en=Autonomous Republic of Crimea;entity=relation"/> 
     <region name="dnipropetrovsk" hillshade="no" srtm="no" wiki="no" translate="name:en=Dnipropetrovsk Oblast;entity=relation"/> 
     <region name="donetsk" hillshade="no" srtm="no" wiki="no" translate="name:en=Donetsk Oblast;entity=relation"/> 
     <region name="ivano-frankivsk" hillshade="no" srtm="no" wiki="no" translate="name:en=Ivano-Frankivsk Oblast;entity=relation"/> 
     <region name="kharkiv" hillshade="no" srtm="no" wiki="no" translate="name:en=Kharkiv Oblast;entity=relation"/> 
     <region name="kherson" hillshade="no" srtm="no" wiki="no" translate="name:en=Kherson Oblast;entity=relation"/> 
     <region name="khmelnytskyy" hillshade="no" srtm="no" wiki="no" translate="name:en=Khmelnytskyi Oblast;entity=relation"/> 
     <region name="kiev" hillshade="no" srtm="no" wiki="no" translate="name:en=Kyiv Oblast;entity=relation"/> 
     <region name="kiev-city" hillshade="no" srtm="no" wiki="no" translate="name:en=Kyiv;entity=relation"/> 
     <region name="kirovohrad" hillshade="no" srtm="no" wiki="no" translate="name:en=Kirovohrad Oblast;entity=relation"/> 
     <region name="luhansk" hillshade="no" srtm="no" wiki="no" translate="name:en=Luhansk Oblast;entity=relation"/> 
     <region name="lviv" hillshade="no" srtm="no" wiki="no" translate="name:en=Lviv Oblast;entity=relation"/> 
     <region name="mykolayiv" hillshade="no" srtm="no" wiki="no" translate="name:en=Mykolaiv Oblast;entity=relation"/> 
     <region name="odessa" hillshade="no" srtm="no" wiki="no" translate="name:en=Odessa Oblast;entity=relation"/> 
     <region name="poltava" hillshade="no" srtm="no" wiki="no" translate="name:en=Poltava Oblast;entity=relation"/> 
     <region name="rivne" hillshade="no" srtm="no" wiki="no" translate="name:en=Rivne Oblast;entity=relation"/> 
     <region name="sumy" hillshade="no" srtm="no" wiki="no" translate="name:en=Sumy Oblast;entity=relation"/> 
     <region name="ternopil" hillshade="no" srtm="no" wiki="no" translate="name:en=Ternopil Oblast;entity=relation"/> 
     <region name="transcarpathia" hillshade="no" srtm="no" wiki="no" translate="name:en=Zakarpattia Oblast;entity=relation"/> 
     <region name="vinnytsya" hillshade="no" srtm="no" wiki="no" translate="name:en=Vinnytsia Oblast;entity=relation"/> 
     <region name="volyn" hillshade="no" srtm="no" wiki="no" translate="name:en=Volyn Oblast;entity=relation"/> 
     <region name="zaporizhzhya" hillshade="no" srtm="no" wiki="no" translate="name:en=Zaporizhia Oblast;entity=relation"/> 
     <region name="zhytomyr" hillshade="no" srtm="no" wiki="no" translate="name:en=Zhytomyr Oblast;entity=relation"/> 
    </region> 

    <!-- south-europe --> 
    <region name="albania" lang="sq" poly_extract="south-europe"/> 
    <region name="andorra" lang="ca" poly_extract="south-europe"/> 
    <region name="azores" translate="Azores;entity=relation" hillshade="no" lang="pt" poly_extract="south-europe"/> 
    <region name="bosnia-herzegovina" translate="Bosnia and Herzegovina;entity=node" lang="bs,hr,sr" poly_extract="south-europe"/> 
    <region name="croatia" lang="hr" poly_extract="south-europe"/> 
    <region name="cyprus" translate="name:en=Cyprus;entity=node" lang="el,tr" left_hand_navigation="yes" poly_extract="south-europe"/> 
    <region name="greece" lang="el" poly_extract="south-europe"/> 
    <region name="italy" map="no" inner_download_prefix="$name" lang="it" poly_extract="south-europe" join_road_files="yes"> 
     <region name="abruzzo" hillshade="no"/> 
     <region name="basilicata" hillshade="no"/> 
     <region name="calabria" hillshade="no"/> 
     <region name="campania" hillshade="no"/> 
     <region name="emilia-romagna" hillshade="no"/> 
     <region name="friuli-venezia-giulia" translate="Friuli Venezia Giulia;entity=node" hillshade="no"/> 
     <region name="lazio" hillshade="no"/> 
     <region name="liguria" hillshade="no"/> 
     <region name="lombardia" hillshade="no"/> 
     <region name="marche" hillshade="no"/> 
     <region name="molise" hillshade="no"/> 
     <region name="piemonte" hillshade="no"/> 
     <region name="puglia" hillshade="no"/> 
     <region name="sardegna" hillshade="no"/> 
     <region name="sicilia" hillshade="no" /> 
     <region name="toscana" hillshade="no"/> 
     <region name="trentino-alto-adige" translate="Trentino-Alto Adige/Südtirol;entity=node" hillshade="no"/> 
     <region name="umbria" hillshade="no"/> 
     <region name="valle-aosta" translate="Valle d'Aosta;entity=node" hillshade="no"/> 
     <region name="veneto" hillshade="no"/> 
    </region> 

+0

請調試代碼在while循環,看看它的正常工作 –

+0

我調試後設置適配器。計算的尺寸,但沒有顯示在回收站的視圖。 –

回答

0

試試這個。把對象數組列表中

try { 
     XmlPullParser parser = getResources().getXml(R.xml.contacts); 
     String name = null; 
     while (parser.getEventType() != XmlPullParser.END_DOCUMENT) { 
      if (parser.getEventType() == XmlPullParser.START_TAG 
        && parser.getName().equals("region")) { 
       if (parser.getAttributeValue(null, "type").equalsIgnoreCase("continent")) { 
       name = parser.getAttributeValue(null, "name"); 
       worldRegion.setRegionName(name); 
       worldRegionList.add(worldRegion); 
       } 
      } 
      parser.next(); 
      WorldRegionsAdapter adapter = new 
      WorldRegionsAdapter(this,worldRegionList); 
      rvWorldRegions.setAdapter(adapter); 
     } 
    } catch (Throwable t) { 
    } 
+0

它沒有幫助。 recyclerview中的適配器僅顯示一個元素。 –

+0

[輸入圖片描述] [1] [1]:https://i.stack.imgur.com/ahZAo.pnga –

相關問題