プロジェクトにインポートした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フォームjarが独自のオブジェクトを参照している間、ArrayListはローカルオブジェクトを参照している必要があるようです。ローカルオブジェクトの使用を避け、jarから同じCountryVOオブジェクトをインポートしてください。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加