Search code examples
javaandroidarraylistexecutable-jarobject-type

How to read an arraylist of object type which a method in a JAR file is returning


I have a JAR file which I have imported in my project, in that a method returns an ArrayList. I have defined the object with same definition as in JAR file in my project (Here I get type mismatch error).

So my question is that how can the ArrayList in the JAR file can be assigned to an ArrayList in my project with same definition.

Code in the JAR file

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
}

Code in the Activity

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
   }
}

My Question

  1. How to retrieve data from a JAR file and assign to a object in the app.
  2. Here in my case the JAR is returning an ArrayList of values which I have to access in my app.

Kindly help me as I am stuck at this point. Thank you


Solution

  • Looks like your ArrayList must be referring to your local object while the arraylist form jar is referring to its own object avoid using your local object and import the same CountryVO object from your jar