Java tutorial
/* * Copyright 2012 - 2013 Anton Tananaev (anton.tananaev@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 com.sun.utils; import android.os.Bundle; import android.support.v4.app.ListFragment; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ListView; import com.sun.personalconnect.R; import java.text.DateFormat; import java.util.ArrayList; import java.util.Collection; import java.util.Date; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Set; public class StatusFragment extends ListFragment { private static final int LIMIT = 20; private final LinkedList<String> messages = new LinkedList<String>(); private final Set<ArrayAdapter<String>> adapters = new HashSet<ArrayAdapter<String>>(); private void notifyAdapters() { for (ArrayAdapter<String> adapter : adapters) { adapter.notifyDataSetChanged(); } } public void addMessage(String message) { DateFormat format = DateFormat.getTimeInstance(DateFormat.SHORT); message = format.format(new Date()) + " - " + message; messages.add(message); while (messages.size() > LIMIT) { messages.removeFirst(); } notifyAdapters(); } public void setMessages(Collection<String> msgs) { messages.clear(); messages.addAll(msgs); while (messages.size() > LIMIT) { messages.removeFirst(); } notifyAdapters(); } public void clearMessages() { messages.clear(); notifyAdapters(); } public List<String> getMessages() { ArrayList<String> list = new ArrayList<>(); list.addAll(messages); return list; } private ArrayAdapter<String> adapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // return super.onCreateView(inflater, container, savedInstanceState); return inflater.inflate(R.layout.activity_status, container, false); } @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1, messages); setListAdapter(adapter); adapters.add(adapter); getListView().setOnItemClickListener(listener); } @Override public void onDestroy() { adapters.remove(adapter); super.onDestroy(); } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { super.onCreateOptionsMenu(menu, inflater); inflater.inflate(R.menu.status, menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == R.id.clear) { clearMessages(); return true; } return super.onOptionsItemSelected(item); } ListView.OnItemClickListener listener; public void setOnItemclickListener(ListView.OnItemClickListener l) { listener = l; } }