Android Open Source - NexusData Main Activity






From Project

Back to project page NexusData.

License

The source code is released under:

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCT...

If you think the Android project NexusData 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 org.example.todo;
/*w  w  w.  j a  v  a2s  .  c o  m*/
import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.view.*;
import android.widget.*;
import com.github.dkharrat.nexusdata.core.FetchRequest;
import com.github.dkharrat.nexusdata.core.ObjectContext;

import java.util.Arrays;
import java.util.List;

public class MainActivity extends Activity {

    private enum DisplayMode {
        TODO,
        COMPLETED
    }

    private ListView listView;
    private TaskListAdapter listAdapter;

    private static final int ADD_TASK = 1;

    private DisplayMode displayMode = DisplayMode.TODO;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        getActionBar().setDisplayShowTitleEnabled(false);
        getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
        getActionBar().setListNavigationCallbacks(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Arrays.asList("Todo", "Completed")), new ActionBar.OnNavigationListener() {
            @Override
            public boolean onNavigationItemSelected(int itemPosition, long itemId) {
                if (itemPosition == 0) {
                    displayMode = DisplayMode.TODO;
                } else {
                    displayMode = DisplayMode.COMPLETED;
                }
                refreshUI();
                return true;
            }
        });

        listView = (ListView)findViewById(R.id.todo_tasks_list);

        refreshUI();
    }

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

        refreshUI();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(Menu.NONE, ADD_TASK, Menu.NONE, "Add")
                .setIcon(android.R.drawable.ic_menu_add)
                .setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)  {
        boolean result = super.onOptionsItemSelected(item);

        switch (item.getItemId()) {
            case ADD_TASK: {
                Intent intent = new Intent(this, NewTaskActivity.class);
                startActivity(intent);

                break;
            }
        }

        return result;
    }

    private void refreshUI() {
        ObjectContext ctx = TodoApp.getMainObjectContext();

        boolean displayCompleted = (displayMode == DisplayMode.COMPLETED);
        FetchRequest<Task> fetchRequest = ctx.newFetchRequestBuilder(Task.class)
                .predicate("completed == "+displayCompleted)
                .build();
        List<Task> tasks = ctx.executeFetchOperation(fetchRequest);

        if (listAdapter == null) {
            listAdapter = new TaskListAdapter(this, tasks);
            listView.setAdapter(listAdapter);
        } else {
            listAdapter.clear();
            listAdapter.addAll(tasks);
            listAdapter.notifyDataSetChanged();
        }
    }

    public class TaskListAdapter extends ArrayAdapter<Task> {

        Handler handler = new Handler();

        public TaskListAdapter(Context context, List<Task> entries) {
            super(context, 0, entries);
        }

        private void setStrikeThroughView(TextView textView, boolean strikeThrough) {
            if (strikeThrough) {
                textView.setPaintFlags(textView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
            } else {
                textView.setPaintFlags(textView.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG);
            }
        }

        @Override
        public View getView(final int position, final View convertView, final ViewGroup parent) {
            final Task task = getItem(position);

            final LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            final View view = inflater.inflate(R.layout.list_item_with_detail, null);

            final TextView titleView = (TextView)view.findViewById(R.id.list_item_title);
            final TextView detailsView = (TextView)view.findViewById(R.id.list_item_detail);
            final CheckBox completedCheckbox = (CheckBox)view.findViewById(R.id.checkbox_completed);
            completedCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    task.setCompleted(isChecked);
                    if (task.isCompleted()) {
                        setStrikeThroughView(titleView, true);
                        setStrikeThroughView(detailsView, true);
                    } else {
                        setStrikeThroughView(titleView, false);
                        setStrikeThroughView(detailsView, false);
                    }
                    TodoApp.getMainObjectContext().save();

                    if (displayMode == DisplayMode.COMPLETED && !task.isCompleted() ||
                            displayMode == DisplayMode.TODO && task.isCompleted()) {
                        handler.postDelayed(new Runnable() {
                            public void run() {
                                listAdapter.remove(task);
                                listAdapter.notifyDataSetChanged();
                            }
                        }, 1500);
                    }
                }
            });

            titleView.setText(task.getTitle());
            detailsView.setText(task.getNotes());
            completedCheckbox.setChecked(task.isCompleted());

            if (task.isCompleted()) {
                setStrikeThroughView(titleView, true);
                setStrikeThroughView(detailsView, true);
            }

            return view;
        }
    }
}




Java Source Code List

com.github.dkharrat.nexusdata.core.AtomicStore.java
com.github.dkharrat.nexusdata.core.ChangedObjectsSet.java
com.github.dkharrat.nexusdata.core.FaultingSet.java
com.github.dkharrat.nexusdata.core.FetchRequest.java
com.github.dkharrat.nexusdata.core.IncrementalStore.java
com.github.dkharrat.nexusdata.core.ManagedObject.java
com.github.dkharrat.nexusdata.core.NoSuchPropertyException.java
com.github.dkharrat.nexusdata.core.ObjectContextNotifier.java
com.github.dkharrat.nexusdata.core.ObjectContext.java
com.github.dkharrat.nexusdata.core.ObjectID.java
com.github.dkharrat.nexusdata.core.ObjectsChangedNotification.java
com.github.dkharrat.nexusdata.core.PersistentStoreCoordinator.java
com.github.dkharrat.nexusdata.core.PersistentStoreRequest.java
com.github.dkharrat.nexusdata.core.PersistentStore.java
com.github.dkharrat.nexusdata.core.SaveChangesRequest.java
com.github.dkharrat.nexusdata.core.SortDescriptor.java
com.github.dkharrat.nexusdata.core.StoreCacheNode.java
com.github.dkharrat.nexusdata.metamodel.Attribute.java
com.github.dkharrat.nexusdata.metamodel.Entity.java
com.github.dkharrat.nexusdata.metamodel.ObjectModelJsonParser.java
com.github.dkharrat.nexusdata.metamodel.ObjectModel.java
com.github.dkharrat.nexusdata.metamodel.Property.java
com.github.dkharrat.nexusdata.metamodel.Relationship.java
com.github.dkharrat.nexusdata.modelgen.ModelGenerator.java
com.github.dkharrat.nexusdata.modelgen.Startup.java
com.github.dkharrat.nexusdata.modelgen.metamodel.Attribute.java
com.github.dkharrat.nexusdata.modelgen.metamodel.Entity.java
com.github.dkharrat.nexusdata.modelgen.metamodel.EnumProperty.java
com.github.dkharrat.nexusdata.modelgen.metamodel.ModelWrapper.java
com.github.dkharrat.nexusdata.modelgen.metamodel.Model.java
com.github.dkharrat.nexusdata.modelgen.metamodel.Property.java
com.github.dkharrat.nexusdata.modelgen.metamodel.Relationship.java
com.github.dkharrat.nexusdata.predicate.ComparisonPredicate.java
com.github.dkharrat.nexusdata.predicate.CompoundPredicate.java
com.github.dkharrat.nexusdata.predicate.ConstantExpression.java
com.github.dkharrat.nexusdata.predicate.ExpressionBuilder.java
com.github.dkharrat.nexusdata.predicate.ExpressionVisitor.java
com.github.dkharrat.nexusdata.predicate.Expression.java
com.github.dkharrat.nexusdata.predicate.FieldPathExpression.java
com.github.dkharrat.nexusdata.predicate.NotPredicate.java
com.github.dkharrat.nexusdata.predicate.PredicateBuilder.java
com.github.dkharrat.nexusdata.predicate.Predicate.java
com.github.dkharrat.nexusdata.predicate.ThisExpression.java
com.github.dkharrat.nexusdata.predicate.parser.ComparisonParselet.java
com.github.dkharrat.nexusdata.predicate.parser.ConstantParselet.java
com.github.dkharrat.nexusdata.predicate.parser.ExpressionNode.java
com.github.dkharrat.nexusdata.predicate.parser.GroupParselet.java
com.github.dkharrat.nexusdata.predicate.parser.InfixParselet.java
com.github.dkharrat.nexusdata.predicate.parser.LexerGrammar.java
com.github.dkharrat.nexusdata.predicate.parser.Lexer.java
com.github.dkharrat.nexusdata.predicate.parser.LogicalParselet.java
com.github.dkharrat.nexusdata.predicate.parser.NameParselet.java
com.github.dkharrat.nexusdata.predicate.parser.ParseException.java
com.github.dkharrat.nexusdata.predicate.parser.Parser.java
com.github.dkharrat.nexusdata.predicate.parser.PredicateParser.java
com.github.dkharrat.nexusdata.predicate.parser.PrefixParselet.java
com.github.dkharrat.nexusdata.predicate.parser.Token.java
com.github.dkharrat.nexusdata.store.AndroidSqlPersistentStore.java
com.github.dkharrat.nexusdata.store.DatabaseHelper.java
com.github.dkharrat.nexusdata.store.InMemoryPersistentStore.java
com.github.dkharrat.nexusdata.store.PredicateToSQL.java
com.github.dkharrat.nexusdata.utils.DateUtil.java
com.github.dkharrat.nexusdata.utils.ObjectUtil.java
com.github.dkharrat.nexusdata.utils.SqlTableBuilder.java
com.github.dkharrat.nexusdata.utils.StreamUtil.java
com.github.dkharrat.nexusdata.utils.StringUtil.java
com.github.dkharrat.nexusdata.utils.android.CursorUtil.java
com.github.dkharrat.nexusdata.utils.android.SQLiteDatabaseHelper.java
org.example.todo.MainActivity.java
org.example.todo.NewTaskActivity.java
org.example.todo.Task.java
org.example.todo.TodoApp.java
org.example.todo.User.java
org.example.todo._Task.java
org.example.todo._User.java