Java tutorial
/* * Copyright (c) 2014 lxb<lxbzmy@gmail.com> * * 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 cn.devit.app.ip_messenger; import java.util.Map; import android.app.Activity; import android.os.Bundle; import android.support.v4.app.ListFragment; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.SimpleAdapter; import cn.devit.app.ip_messenger.dummy.DummyContent1; import cn.devit.app.ip_messenger.pigeon.UserData; /** * A fragment representing a list of Items. * <p /> * <p /> * Activities containing this fragment MUST implement the {@link Callbacks} * interface. */ public class MessageHistorykFragment extends ListFragment { // TODO: Rename parameter arguments, choose names that match // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER private static final String ARG_PARAM1 = "param1"; private static final String ARG_PARAM2 = "param2"; // TODO: Rename and change types of parameters private String mParam1; private String mParam2; private OnFragmentInteractionListener mListener; // TODO: Rename and change types of parameters public static MessageHistorykFragment newInstance(String param1, String param2) { MessageHistorykFragment fragment = new MessageHistorykFragment(); Bundle args = new Bundle(); args.putString(ARG_PARAM1, param1); args.putString(ARG_PARAM2, param2); fragment.setArguments(args); return fragment; } /** * Mandatory empty constructor for the fragment manager to instantiate the * fragment (e.g. upon screen orientation changes). */ public MessageHistorykFragment() { } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mParam1 = getArguments().getString(ARG_PARAM1); mParam2 = getArguments().getString(ARG_PARAM2); } // TODO: Change Adapter to display your content // setListAdapter(new // ArrayAdapter<DummyContent1.DummyItem>(getActivity(), // android.R.layout.two_line_list_item, new String[] {} // android.R.id.text1, // DummyContent1.ITEMS)); setListAdapter(new SimpleAdapter(getActivity(), DummyContent1.ITEMS, R.layout.message_list_view, new String[] { "text1", "text2", "timestamp" }, new int[] { R.id.text1, R.id.text2, R.id.text_date })); } @Override public void onAttach(Activity activity) { super.onAttach(activity); // try { // mListener = (OnFragmentInteractionListener) activity; // } catch (ClassCastException e) { // throw new ClassCastException(activity.toString() // + " must implement OnFragmentInteractionListener"); // } } @Override public void onDetach() { super.onDetach(); mListener = null; } @Override public void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); if (null != mListener) { // Notify the active callbacks interface (the activity, if the // fragment is attached to one) that an item has been selected. mListener.onFragmentInteraction(DummyContent1.ITEMS.get(position).get("id")); } } public void addMessage(UserData[] user, String message) { } /** * This interface must be implemented by activities that contain this * fragment to allow an interaction in this fragment to be communicated to * the activity and potentially other fragments contained in that activity. * <p> * See the Android Training lesson <a href= * "http://developer.android.com/training/basics/fragments/communicating.html" * >Communicating with Other Fragments</a> for more information. */ public interface OnFragmentInteractionListener { // TODO: Update argument type and name public void onFragmentInteraction(String id); } }