如何读取 JAR 文件中的方法返回的对象类型的数组列表
How to read an arraylist of object type which a method in a JAR file is returning
我在我的项目中导入了一个 JAR 文件,其中一个方法 returns 一个 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
}
}
我的问题
- 如何从 JAR 文件中检索数据并分配给应用程序中的对象。
- 在我的例子中,JAR 返回一个 ArrayList 值,我必须在我的应用程序中访问它。
请帮助我,因为我被困在这一点上。谢谢
看起来你的 ArrayList 必须引用你的本地对象,而 arraylist 表单 jar 引用它自己的对象避免使用你的本地对象并从你的 jar 导入相同的 CountryVO 对象
我在我的项目中导入了一个 JAR 文件,其中一个方法 returns 一个 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
}
}
我的问题
- 如何从 JAR 文件中检索数据并分配给应用程序中的对象。
- 在我的例子中,JAR 返回一个 ArrayList 值,我必须在我的应用程序中访问它。
请帮助我,因为我被困在这一点上。谢谢
看起来你的 ArrayList 必须引用你的本地对象,而 arraylist 表单 jar 引用它自己的对象避免使用你的本地对象并从你的 jar 导入相同的 CountryVO 对象