LittleTin
LittleTin

Reputation: 27

How to setText or send a arraylist from the thread in android

I'm trying to make a news board, I have been googled it a lot and trying to use "Handler", but always crash

This is original code:

public void onViewCreated(final View view, Bundle savedInstanceState) {
    final TextView t1 = (TextView) view.findViewById(R.id.txt1);
    final TextView t2 = (TextView) view.findViewById(R.id.txt2);
    final TextView t3 = (TextView) view.findViewById(R.id.txt3);
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                final ArrayList arrayList = XMLPullParser.parse("http://localhost/news.xml", "news", 1, 2, 3);
                System.out.println(arrayList.size() + "return from arraylist");
                /*
                t1.setText(arrayList.get(0).toString());
                t2.setText(arrayList.get(1).toString());
                t3.setText(arrayList.get(2).toString());
                */
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
        }
    }).start();
}

And I edit like this:

public void onViewCreated(final View view, Bundle savedInstanceState) {
    final Handler handler = new Handler();
    final TextView t1 = (TextView) view.findViewById(R.id.txt1);
    final TextView t2 = (TextView) view.findViewById(R.id.txt2);
    final TextView t3 = (TextView) view.findViewById(R.id.txt3);
    new Thread(new Runnable() {
        @Override
        public void run() {
            handler.post(new Runnable() {
                public void run() {
                    try {
                        final ArrayList arrayList = XMLPullParser.parse("http://localhost/news.xml", "news", 1, 2, 3);
                        System.out.println(arrayList.size() + "return from arraylist");
                        t1.setText(arrayList.get(0).toString());
                        t2.setText(arrayList.get(1).toString());
                        t3.setText(arrayList.get(2).toString());
                    } catch (URISyntaxException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }).start();
}

The original code is susses show arraylist details and String, but the problem is how to setText.

Upvotes: 1

Views: 311

Answers (1)

Mr.Me
Mr.Me

Reputation: 9276

try this instead

   public void onViewCreated(final View view, Bundle savedInstanceState) {
       // main thread
        final Handler handler = new Handler();
        final TextView t1 = (TextView) view.findViewById(R.id.txt1);
        final TextView t2 = (TextView) view.findViewById(R.id.txt2);
        final TextView t3 = (TextView) view.findViewById(R.id.txt3);
        new Thread(new Runnable() {
            @Override
            public void run() {
                      // background thread
                final ArrayList arrayList = XMLPullParser.parse("http://localhost/news.xml", "news", 1, 2, 3);
                System.out.println(arrayList.size() + "return from arraylist");
                handler.post(new Runnable() {
                    public void run() {
                        try {
                          // main thread 
                            t1.setText(arrayList.get(0).toString());
                            t2.setText(arrayList.get(1).toString());
                            t3.setText(arrayList.get(2).toString());
                        } catch (URISyntaxException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        }).start();
    }

To explain what is going on, You created a Handler object in the main thread, since onCreateView is called there.

Then you created another thread for networking, that thread does the XML parsing, and deliver a Runnable to the UI/Main thread using the Handler.

The handler takes the massage/Runnable, and call it on the main thread (the only thread allowed to touch the UI).

Upvotes: 2

Related Questions