de.fahrgemeinschaft.RideListFragment.java Source code

Java tutorial

Introduction

Here is the source code for de.fahrgemeinschaft.RideListFragment.java

Source

/**
 * Ridesharing Android App
 *
 * Copyright (c) 2013 by it's authors.
 * Some rights reserved. See LICENSE.. 
 *
 */

package de.fahrgemeinschaft;

import com.actionbarsherlock.app.SherlockListFragment;

import de.fahrgemeinschaft.RideListFragment.ListItemClicker;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.CursorAdapter;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class RideListFragment extends SherlockListFragment implements LoaderCallbacks<Cursor> {

    private static final String TAG = "Fahrgemeinschaft";
    private ListItemClicker callback;

    @Override
    public View onCreateView(final LayoutInflater lI, ViewGroup p, Bundle b) {
        return lI.inflate(R.layout.fragment_ride_list, p, false);
    }

    @Override
    public void onViewCreated(View layout, Bundle savedInstanceState) {
        super.onViewCreated(layout, savedInstanceState);

        setListAdapter(new BaseAdapter() {

            @Override
            public int getCount() {
                return 42;
            }

            @Override
            public View getView(int position, View v, ViewGroup parent) {
                v = getLayoutInflater(null).inflate(R.layout.view_ride_list_entry, parent, false);
                // we have default green background
                if (position % 2 == 0) {
                    v.setBackgroundColor(getResources().getColor(R.color.mediumGreen));
                }
                return v;
            }

            @Override
            public long getItemId(int position) {
                return 0;
            }

            @Override
            public Object getItem(int position) {
                return null;
            }
        });

    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle arg1) {
        Uri uri = Uri.parse("content://...");
        return new CursorLoader(getActivity(), uri, null, null, null, null);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> arg0, Cursor msges) {
        ((CursorAdapter) getListAdapter()).swapCursor(msges);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> arg0) {
        Log.d(TAG, "onLoaderReset");
    }

    public interface ListItemClicker {
        public void onListItemClick(String id);
    }

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

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        callback.onListItemClick("foo");
    }

    static class RideView extends RelativeLayout {

        TextView from;

        public RideView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

        @Override
        protected void onFinishInflate() {
            super.onFinishInflate();
            //            text = (TextView) findViewById(R.id.msg_text);
        }
    }
}