Java tutorial
/* * * Copyright 2015 University of Wisconsin - Parkside * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * */ package rss; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.os.ResultReceiver; import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ListView; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; import java.util.List; import android.os.Handler; import u.ready_wisc.MenuActivity; import u.ready_wisc.R; import u.ready_wisc.RssActivity; /** * Created by piela_000 on 3/1/2015. */ public class RssFragment extends Fragment implements AdapterView.OnItemClickListener { private ProgressBar progressBar; private ListView listView; private View view; public static String weatherDesc; public static String weatherLink; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRetainInstance(true); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (view == null) { view = inflater.inflate(R.layout.fragment_layout, container, false); progressBar = (ProgressBar) view.findViewById(R.id.progressBar); listView = (ListView) view.findViewById(R.id.listView); listView.setOnItemClickListener(this); startService(); } else { // If we are returning from a configuration change: // "view" is still attached to the previous view hierarchy // so we need to remove it and re-attach it to the current one ViewGroup parent = (ViewGroup) view.getParent(); parent.removeView(view); } return view; } private void startService() { Intent intent = new Intent(getActivity(), RssService.class); intent.putExtra(RssService.RECEIVER, resultReceiver); getActivity().startService(intent); } /** * Once the {@link RssService} finishes its task, the result is sent to this * ResultReceiver. */ private final ResultReceiver resultReceiver = new ResultReceiver(new Handler()) { @SuppressWarnings("unchecked") @Override protected void onReceiveResult(int resultCode, Bundle resultData) { progressBar.setVisibility(View.GONE); List<RssItem> items = (List<RssItem>) resultData.getSerializable(RssService.ITEMS); if (items != null) { RssAdapter adapter = new RssAdapter(getActivity(), items); listView.setAdapter(adapter); } else { Toast.makeText(getActivity(), "An error occurred while downloading the rss feed.", Toast.LENGTH_LONG).show(); } listView.setVisibility(View.VISIBLE); }; }; @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { RssAdapter adapter = (RssAdapter) parent.getAdapter(); RssItem item = (RssItem) adapter.getItem(position); weatherLink = item.getLink(); weatherDesc = item.getDesc(); // Uri uri = Uri.parse(item.getLink()); // Intent intent = new Intent(Intent.ACTION_VIEW, uri); // startActivity(intent); Intent intent = new Intent(getActivity(), RssActivity.class); startActivity(intent); } }