0

Ok, Let me simplify the scenario first

Actually I want to trigger some action after completing a background thread operations.

Hence I made a thread callback interface like below.

interface OnThreadCompleted.java

package xml.parser;

public interface OnThreadCompleted {

    public void OnThreadCompleted();

}

and here is my actual XMLParserThread.java extending the Threads

public class XMLParserThread extends Thread {

ArrayList<Integer> listWeatherCode;
ArrayList<String> listWeatherDesc;
ArrayList<String> listIcon_day;
ArrayList<String> listIcon_night;

Context context;
String fileName;
XMLParser xmlParser;

OnThreadCompleted listner;

public XMLParserThread(Context context, String fileName,
        OnThreadCompleted listner) {

    this.context = context;
    this.fileName = fileName;
    this.listner = listner;
}

@Override
public void run() {

    xmlParser = new XMLParser();

    listWeatherCode = new ArrayList<Integer>();
    listWeatherDesc = new ArrayList<String>();
    listIcon_day = new ArrayList<String>();
    listIcon_night = new ArrayList<String>();

    String xmlResponse = null;
    try {
        xmlResponse = xmlParser.getXmlFromFile(context, fileName);
        Document doc = xmlParser.getDomElement(xmlResponse);
        NodeList nList = doc.getElementsByTagName("condition");

        for (int i = 0; i < nList.getLength(); i++) {

            Element element = (Element) nList.item(i);

            listWeatherCode.add(Integer.valueOf(xmlParser.getValue(element,
                    "code")));
            listWeatherDesc.add(xmlParser.getValue(element, "description"));
            listIcon_day.add(xmlParser.getValue(element, "day_icon"));
            listIcon_night.add(xmlParser.getValue(element, "night_icon"));
        }

        Log.e("listWeatherCode", listWeatherCode.toString());

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    super.run();
}
}

and Im calling my thread in my activity like

private void callXMLParserThread() {

    String fileName = "weather_conditions.xml";
    parserThread = new XMLParserThread(context, fileName,
            new OnThreadCompleted() {

                @Override
                public void OnThreadCompleted() {

                    Log.e("parserThread State", parserThread.getState()
                            + ""); // **Label 2**
                }

            });

    parserThread.start();

    Log.e("parserThread State", parserThread.getState()
            + ""); // **Label 1**

}

Experiments: I have check the log mentioned in Label 1 is returning me the state of Thread as WAITING. also I have checked my thread is also performing its all operations,

Problem

Its not calling my callback Method in my activity, Hence the Log in my activity returning nothing, as mention in Label 2

Please Guide me where I m wrong?

Thanks in advance

Qadir Hussain
  • 8,721
  • 13
  • 89
  • 124

1 Answers1

1

One problem is that, even though you have taken listener as parameter is thread, you are not invoking the listener's callback method from thread

OnThreadCompleted listner;

public XMLParserThread(Context context, String fileName,
        OnThreadCompleted listner) {

    this.context = context;
    this.fileName = fileName;
    this.listner = listner;
}

@Override
public void run() {
   //listener.OnThreadCompleted() call is missing
}

Secondly, its highly recommended to use Runnable interface instead of extending Thread class, unless you really have a strong reason

Also, if you are expecting some value back from thread, use Callable and Future objects provided by ExecutorService class. More details here.

Community
  • 1
  • 1
sanbhat
  • 17,522
  • 6
  • 48
  • 64
  • OMG, my code was not wrong, just the one line :) Thanks for the description also, How can I use this code in a runnable interface and what are the strong reasons for using the runnable interface not extending the thread class. – Qadir Hussain Sep 20 '13 at 07:35
  • Now the callback method is calling perfectly but, the Log always returning me the state of thread as RUNNABLE. when it will return me the completed or finished etc? – Qadir Hussain Sep 20 '13 at 07:38