![](/img/trans.png)
[英]Java method to read text file and return ArrayList type object
[英]How to read an arraylist of object type which a method in a JAR file is returning
我有一個導入到項目中的JAR文件,其中一個方法返回ArrayList。 我在項目中定義了與JAR文件中相同的定義的對象(此處出現類型不匹配錯誤)。
所以我的問題是,如何將JAR文件中的ArrayList分配給我項目中具有相同定義的ArrayList。
JAR文件中的代碼
public final class XMLReaderClass implements XMLReader {
private static ArrayList<CountryVO> details;
@Override
public ArrayList<CountryVO> read(InputStream fIn) {
// TODO Auto-generated method stub
if(details == null){
details = new ArrayList<CountryVO>();
try{
Document doc=parseXml(ReadFromfile(fIn));
NodeList n = doc.getElementsByTagName("Country");
for(int i=0; i < n.getLength(); i++){
CountryVO countryVO = new CountryVO();
Element e = (Element)n.item(i);
//Read individual elements
Element countryNameEl = (Element) e.getElementsByTagName("CountryName").item(0);
Element countryCodeEl = (Element) e.getElementsByTagName("CountryCode").item(0);
String countryName = countryNameEl.getLastChild().getNodeValue();
String countryCode = countryCodeEl.getLastChild().getNodeValue();
countryVO.setCountryName(countryName);
countryVO.setCountryCode(countryCode);
details.add(countryVO);
}
}catch(Exception e){
e.printStackTrace();
}
}
return details; // The data in this ArrayList should be accessed in the project where jar is imported
}
活動中的代碼
public class MyApp extends Activity {
private ListView ls;
private ArrayList<CountryVO> list;
Context context;
InputStream fIn = null;
InputSource inputSource = null;
FirstClass fclass = new FirstClass(); // Creating an instance of a class inside the JAR file
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_myApp);
try {
fIn = context.getResources().getAssets().open("DataFile.xml");
inputSource = new InputSource(fIn);
} catch (Exception e) {
e.printStackTrace();
}
XMLReader xmlr = fclass.read("xml");
list = xmlr.read(fIn); // Here I am getting Type Mismatch error because list is created in the activity(MyApp) and arraylist which method is returning is of other type
}
}
我的問題
敬請幫助,因為我此時仍無法解決。 謝謝
看起來您的ArrayList必須引用您的本地對象,而arraylist表單jar引用其自己的對象時,請避免使用本地對象,並從jar中導入相同的CountryVO對象
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.