Android Open Source - ArchPad C Notes Fragment






From Project

Back to project page ArchPad.

License

The source code is released under:

GNU General Public License

If you think the Android project ArchPad listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.Andryyo.ArchPad.note;
//  w  w w.  j  av  a 2s  . c om


import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.view.*;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.SimpleCursorTreeAdapter;
import android.widget.TextView;
import com.Andryyo.ArchPad.R;
import com.Andryyo.ArchPad.database.CSQLiteOpenHelper;

import java.util.Calendar;

/**
 * Created with IntelliJ IDEA.
 * User: ???????
 * Date: 11.05.13
 * Time: 11:39
 * To change this template use File | Settings | File Templates.
 */
public class CNotesFragment extends Fragment implements View.OnClickListener {

    CMySimpleCursorTreeAdapter adapter;
    ExpandableListView expandableListView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)  {
        setHasOptionsMenu(false);
        expandableListView = new ExpandableListView(getActivity());
        adapter = new CMySimpleCursorTreeAdapter(getActivity(),
                            getLoaderManager(),
                            R.layout.expand_list_group,
                            new String[]{"timemark"},
                            new int[]{R.id.text1},
                            R.layout.expand_list_child,
                            new String[]{"text"},
                            new int[]{R.id.text1});
        View footerView = inflater.inflate(R.layout.add_note_button, null);
        footerView.setOnClickListener(this);
        expandableListView.
                addFooterView(footerView);
        expandableListView.setAdapter(adapter);
        registerForContextMenu(expandableListView);
        return expandableListView;
    }

    public void update()    {
        if (adapter!=null)
            adapter.update();
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
                                    ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        MenuInflater inflater = getActivity().getMenuInflater();
        inflater.inflate(R.menu.notes_context_menu, menu);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        if (item.getItemId()==R.id.delete_note)
        {
            ExpandableListView.ExpandableListContextMenuInfo info =
                    (ExpandableListView.ExpandableListContextMenuInfo)item.getMenuInfo();
            adapter.deleteRecord(info.id);
        }
        return true;
    }

    @Override
    public void onResume()  {
        super.onResume();
        update();
    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent(getActivity(), CNoteCreateActivity.class);
        startActivity(intent);
    }

    class CMySimpleCursorTreeAdapter extends SimpleCursorTreeAdapter implements LoaderManager.LoaderCallbacks<Cursor>{

        private static final String GROUP_POS = "groupPos";

        Context context;
        LoaderManager loaderManager;

        public CMySimpleCursorTreeAdapter(Context context, LoaderManager lm, int groupLayout, String[] groupFrom, int[] groupTo, int childLayout, String[] childFrom, int[] childTo) {
            super(context, null, groupLayout, groupFrom, groupTo, childLayout, childFrom, childTo);
            this.setViewBinder(new CMyViewBinder());
            this.context = context;
            this.loaderManager = lm;
            update();
        }

        public void deleteRecord(long _id)  {
            CSQLiteOpenHelper helper = CSQLiteOpenHelper.getHelper(context);
            helper.delete(CSQLiteOpenHelper.TABLE_NOTES,_id);
            update();
        }

        @Override
        protected Cursor getChildrenCursor(Cursor groupCursor) {
            Bundle bundle = new Bundle();
            bundle.putInt(GROUP_POS, groupCursor.getPosition());
            loaderManager.restartLoader((int) groupCursor.getLong(groupCursor.getColumnIndex("_id")), bundle, this);
            return null;
        }

        public void update()    {
            loaderManager.restartLoader(0, null, this);
        }

        @Override
        public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
            if (i==0)
                return CSQLiteOpenHelper.getCursorLoader(context, CSQLiteOpenHelper.TABLE_NOTES);
            else
                return CSQLiteOpenHelper.getCursorLoader(context, CSQLiteOpenHelper.TABLE_NOTES, i, bundle);

        }

        @Override
        public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
            if (cursorLoader.getId()==0)
            {
                changeCursor(cursor);
                if (getGroupCount()!=0)
                    expandableListView.setSelection(getGroupCount()-1);
            }
            else
            try {
                setChildrenCursor(((CSQLiteOpenHelper.CSQLiteCursorLoader)cursorLoader)
                        .getData().getInt(GROUP_POS), cursor);
            } catch (NullPointerException e)
                {e.printStackTrace();}
        }

        @Override
        public void onLoaderReset(Loader<Cursor> cursorLoader) {
            changeCursor(null);
        }


        class CMyViewBinder implements ViewBinder {

            @Override
            public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
                if (columnIndex==cursor.getColumnIndex("text"))
                    return false;
                if (columnIndex==cursor.getColumnIndex("timemark"))
                {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTimeInMillis(cursor.getLong(cursor.getColumnIndex("timemark")));
                    ((TextView)view).setText(
                            calendar.get(Calendar.DATE) + ":" +
                                    calendar.get(Calendar.MONTH) + ":" +
                                    calendar.get(Calendar.YEAR) + " " +
                                    calendar.get(Calendar.HOUR_OF_DAY) + ":" +
                                    calendar.get(Calendar.MINUTE)+" "+
                                    cursor.getString(cursor.getColumnIndex("name")));
                    return true;
                }
                return false;
            }
        }
    }

}




Java Source Code List

com.Andryyo.ArchPad.CArrow.java
com.Andryyo.ArchPad.CShot.java
com.Andryyo.ArchPad.MainActivity.java
com.Andryyo.ArchPad.archeryFragment.CArcheryFragment.java
com.Andryyo.ArchPad.archeryFragment.CCurrentEndView.java
com.Andryyo.ArchPad.archeryFragment.CDistanceTemplate.java
com.Andryyo.ArchPad.archeryFragment.CDistance.java
com.Andryyo.ArchPad.archeryFragment.CRoundTemplate.java
com.Andryyo.ArchPad.archeryFragment.CRound.java
com.Andryyo.ArchPad.archeryFragment.IOnShotAddListener.java
com.Andryyo.ArchPad.archeryFragment.?EndsCounterView.java
com.Andryyo.ArchPad.database.CSQLiteOpenHelper.java
com.Andryyo.ArchPad.note.CNoteCreateActivity.java
com.Andryyo.ArchPad.note.CNotesFragment.java
com.Andryyo.ArchPad.sight.CSightSelectFragment.java
com.Andryyo.ArchPad.sight.?SightPropertiesFragment.java
com.Andryyo.ArchPad.start.CArrowSelectView.java
com.Andryyo.ArchPad.start.CColorSelectView.java
com.Andryyo.ArchPad.start.CRoundSelectView.java
com.Andryyo.ArchPad.start.CStartFragment.java
com.Andryyo.ArchPad.start.CTargetSelectView.java
com.Andryyo.ArchPad.start.IOnFragmentSwapRequiredListener.java
com.Andryyo.ArchPad.statistics.CBorderedTextView.java
com.Andryyo.ArchPad.statistics.CRecordViewFragment.java
com.Andryyo.ArchPad.statistics.CStatisticsFragment.java
com.Andryyo.ArchPad.statistics.IOnUpdateListener.java
com.Andryyo.ArchPad.target.CEditableTargetView.java
com.Andryyo.ArchPad.target.CRing.java
com.Andryyo.ArchPad.target.CTargetView.java
com.Andryyo.ArchPad.target.CTarget.java
com.Andryyo.ArchPad.target.CZoomableTargetView.java