package de.wak_sh.client.fragments;

import java.io.IOException;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import de.wak_sh.client.R;
import de.wak_sh.client.backend.MessageArrayAdapter;
import de.wak_sh.client.backend.ProgressDialogTask;
import de.wak_sh.client.backend.model.Message;
import de.wak_sh.client.backend.service.MessageService;

public class NachrichtenFragment extends Fragment {
	protected List<Message> messages;
	protected ListView listView;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_nachrichten,
				container, false);
		View header = inflater.inflate(R.layout.messages_list_item, container,
				false);

		((TextView) header.findViewById(R.id.msg_date)).setText(R.string.datum);
		((TextView) header.findViewById(R.id.msg_from)).setText(R.string.von);
		((TextView) header.findViewById(R.id.msg_subject))
				.setText(R.string.betreff);

		// listView = (ListView) rootView.findViewById(R.id.msg_listview);
		listView = (ListView) rootView;
		listView.addHeaderView(header);

		return rootView;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		if (messages == null) {
			new MessageTask(getActivity()).execute();
		}
	}

	private class MessageTask extends ProgressDialogTask<Void, Void> {
		public MessageTask(Context context) {
			super(context, context.getString(R.string.fetching_messages));
		}

		@Override
		protected Void doInBackground(Void... params) {
			MessageService service = new MessageService();
			try {
				service.fetchMessages();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}

			messages = service.getMessages();

			getActivity().runOnUiThread(new Runnable() {
				@Override
				public void run() {
					MessageArrayAdapter adapter = new MessageArrayAdapter(
							getActivity(), messages);
					// listView.setAdapter(adapter);
				}
			});

			return null;

		}

	}
}

@@ -115,40 +115,8 @@
 ty;%0A
-import android.content.Context;%0A
 impo
@@ -1214,19 +1214,16 @@
 ff);%0A%0A%09%09
-// 
 listView
@@ -1248,78 +1248,15 @@
 View
-.findViewById(R.id.msg_listview);%0A%09%09listView = (ListView) rootView
 ;%0A%09%09
+// 
 list
@@ -1434,29 +1434,24 @@
 ageTask(
-getA
+a
 ctivity
-()
 ).execut
@@ -1538,70 +1538,104 @@
 %0A%09%09p
-ublic MessageTask(Context context) %7B%0A%09%09%09super(context, context
+rivate Activity activity;%0A%0A%09%09public MessageTask(Activity activity) %7B%0A%09%09%09super(activity, activity
 .get
@@ -1671,16 +1671,45 @@
 ages));%0A
+%09%09%09this.activity = activity;%0A
 %09%09%7D%0A%0A%09%09@
@@ -2012,29 +2012,24 @@
 ();%0A%0A%09%09%09
-getA
+a
 ctivity
-()
 .runOnUi
@@ -2161,21 +2161,16 @@
 %09%09%09%09
-getA
+a
 ctivity
-()
 , me
@@ -2183,19 +2183,16 @@
 );%0A%09%09%09%09%09
-// 
 listView

Comment out code that causes a ClassCastException