de.egore911.drilog.fragments.LogviewFragment.java Source code

Java tutorial

Introduction

Here is the source code for de.egore911.drilog.fragments.LogviewFragment.java

Source

/* LogviewFragment.java
 *
 * Copyright (C) 2011-2012 Christoph Brill
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package de.egore911.drilog.fragments;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import de.egore911.drilog.R;
import de.egore911.drilog.ShowActivity;
import de.egore911.drilog.adapter.ShowDataAdapter;

import java.util.HashSet;
import java.util.List;

/**
 * @author Christoph Brill <egore911@egore911.de>
 * @since 0.3
 */
public class LogviewFragment extends ListFragment {

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View layout = inflater.inflate(R.layout.fragment_logview, container, false);
        ListView list = (ListView) layout.findViewById(android.R.id.list);
        list.setEmptyView(layout.findViewById(android.R.id.empty));
        return layout;
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        ViewGroup row = (ViewGroup) v;
        Object tag = row.getTag();

        if (tag instanceof ShowDataAdapter.CommentViewHolder) {
            // ShowDataAdapter.CommentViewHolder c = (ShowDataAdapter.CommentViewHolder) tag;
            // TODO: what to do when a user clicks a log line?

        } else if (tag instanceof ShowDataAdapter.UserViewHolder) {

            ShowDataAdapter.UserViewHolder u = (ShowDataAdapter.UserViewHolder) tag;

            List<String> current = ((ShowActivity) getActivity()).mWatchesDao.getWatches();

            String nick = u.user.nick;
            if (current.contains(nick)) {
                ((ShowActivity) getActivity()).mWatchesDao.removeWatch(nick);
                current.remove(nick);
            } else {
                ((ShowActivity) getActivity()).mWatchesDao.addWatch(nick);
                current.add(nick);
            }

            ShowDataAdapter listAdapter = (ShowDataAdapter) getListAdapter();
            listAdapter.setWatchList(new HashSet<String>(current));
            // TODO this causes the views to be recycled which causes a flicker of the images
            listAdapter.notifyDataSetChanged();
        }
    }

}