我创建了一个方法来循环一个包含4个项目的文件并生成类obj 资源模板如下:
<?xml version="1.0" encoding="UTF-8"?>
<part1prayer>
<item>
<title> </title>
<arabictext> </arabictext>
<transliteration> </transliteration>
<tanslanguage> </tanslanguage>
<translation> </translation>
</item>
</part1prayer>
由于某种原因,此方法不断返回最后一个项目[3]四次而不是item[0]
,item[1]
和item[2]
但是,如果我在此行 nNode = nList.item(temp);
手动传递值,即将temp
替换为2,则会将项目2返回四次。
public ArrayList<PrayerDataProperties> readAssetFile2(String fileName) {
ArrayList<PrayerDataProperties> listOfPrayers = null;
try {
listOfPrayers = new ArrayList<>();
InputStream inputstream = null;
Node nNode = null;
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
inputstream = context.getAssets().open(fileName);
Document doc = dBuilder.parse(inputstream);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName(ITEM);
for (int temp = 0; temp < nList.getLength(); temp++) {
nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
PrayerDataProperties prayerData = new PrayerDataProperties();
prayerData.setArabicTxt(eElement.getElementsByTagName(ARABIC_TEXT)
.item(0).getTextContent());
prayerData.setTransliterationTxt(eElement
.getElementsByTagName(TRANSLITERATION).item(0).getTextContent());
prayerData.setTransLangTxt(eElement
.getElementsByTagName(TRANS_LANGUAGE).item(0).getTextContent());
prayerData.setTranslationTxt(eElement
.getElementsByTagName(TRANSLATION).item(0).getTextContent());
listOfPrayers.add(prayerData);
}// End if
}// End for
}// End try
catch (Exception e) {
e.printStackTrace();
}// End catch
return listOfPrayers;
}// End of readAssetFile2
public class PrayerListCustomAdapter extends BaseAdapter
{
private Context context;
private ArrayList<PrayerDataProperties> listsOfPrayer;
LayoutInflater layoutInflater;
private PrayerDataProperties prayerDataProperties;
// Empty Constructor
public PrayerListCustomAdapter()
{
}// End Empty Constructor
public PrayerListCustomAdapter(Context acontext, ArrayList<PrayerDataProperties> prayerLists)
{
this.context = acontext;
this.listsOfPrayer = prayerLists;
layoutInflater = LayoutInflater.from(acontext);
}//End of Constructor
@Override
public int getCount() {
return listsOfPrayer.size();
}
@Override
public Object getItem(int position) {
return listsOfPrayer.get(position);
}
@Override
public long getItemId(int position)
{
return listsOfPrayer.indexOf(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder = null;
View customView = convertView;
// Inflate the View if Empty
if (customView == null)
{
customView = layoutInflater.inflate(R.layout.prayer_list_template, null);
// Attach View to View inflated and ViewGroup
holder = new ViewHolder();
// Instantiate Views
holder.arabicTxt = (TextView)customView.findViewById(R.id.tv_arabic_txt);
holder.transliterationTxt = (TextView)customView.findViewById(R.id.tv_transliteration_txt);
holder.transLanguageTxt = (TextView)customView.findViewById(R.id.tv_translang_txt);
holder.translationTxt = (TextView)customView.findViewById(R.id.tv_translation_txt);
customView.setTag(holder);
}//End if
else
{
// Call The View already Inflated
holder = (ViewHolder)customView.getTag();
}//End else
// Pass Data to Each View
prayerDataProperties = (PrayerDataProperties)getItem(position);
holder.arabicTxt.setText(prayerDataProperties.getArabicTxt());
holder.transliterationTxt.setText(prayerDataProperties.getTransliterationTxt());
holder.transLanguageTxt.setText(prayerDataProperties.getTransLangTxt());
holder.translationTxt.setText(prayerDataProperties.getTranslationTxt());
return customView;
}//End of getView
static class ViewHolder
{
TextView arabicTxt, transliterationTxt, translationTxt, transLanguageTxt;
}
}// End of PrayerListCustomAdapter
答案 0 :(得分:1)
请确保PrayerDataProperties
中的字段不是静态的。如果是,则所有实例都具有相同的值,并且当您在XML中读取时它们会被覆盖。
如果这些字段确实包含static
关键字,请将其删除,这样可以解决您的问题。