text 9到做名单
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了text 9到做名单相关的知识,希望对你有一定的参考价值。
<?xml version="1.0" encoding="utf-8"?>
<!--
This linear layout contains a single item in the list of tasks displayed in the MainActivity
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context="com.example.android.todolist.MainActivity">
<!--
This horizontal linear layout displays the description and priority of a task
in a single horizontal line
-->
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="24dp"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingBottom="8dp">
<!-- TextView that displays the description of a task -->
<TextView
android:id="@+id/taskDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/primary_text_light"
style="@style/TextAppearance.AppCompat.Medium" />
<!-- An empty view for spacing out the description and priority views -->
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<!-- A priority circle that is colored in programmatically in the adapter code -->
<TextView
android:id="@+id/priorityTextView"
android:layout_width="22dp"
android:layout_height="22dp"
android:layout_gravity="center"
android:gravity="center"
android:textAlignment="center"
style="@style/TextAppearance.AppCompat.Small"
android:textColor="@android:color/primary_text_light"
android:background="@drawable/priority_circle" />
</LinearLayout>
<!-- A gray divider line at the bottom of each task -->
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<!--
This layout defines the main activity screen and displays a list of tasks (if they exist),
and a floating action button that launches another activity.
-->
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- RecyclerView that is responsible for holding a list of task data -->
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerViewTasks"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="8dp"/>
<!-- Floating action button that launches AddTaskActivity when clicked -->
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_input_add"
android:tint="@android:color/white"/>
</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<!--
This vertical linear layout contains all the views in the AddTaskActivity code.
This includes an EditText to add a task description, and buttons to select a priority
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingEnd="@dimen/activity_horizontal_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingStart="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<!-- EditText for getting task description input -->
<EditText
android:id="@+id/editTextTaskDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="start"
android:hint="@string/edit_task_description"
android:paddingBottom ="@dimen/activity_horizontal_margin"/>
<!-- TextView and FrameLayout (which contains buttons) for Priority Selection -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:textColor="@android:color/primary_text_light"
style="@style/TextAppearance.AppCompat.Medium"
android:text="@string/priority_string"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_vertical_margin">
<!-- Linear layout that contains all three priority buttons (high, medium, low)
in a single line -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:weightSum="3">
<Button
style="?android:attr/buttonBarButtonStyle"
android:textColor="@android:color/primary_text_light"
android:id="@+id/buttonP1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/materialRed"
android:text="@string/high_priority"
android:layout_weight="1"
android:onClick="onPrioritySelected"/>
<Button
style="?android:attr/buttonBarButtonStyle"
android:textColor="@android:color/primary_text_light"
android:id="@+id/buttonP2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/materialOrange"
android:text="@string/med_priority"
android:layout_weight="1"
android:onClick="onPrioritySelected"/>
<Button
style="?android:attr/buttonBarButtonStyle"
android:textColor="@android:color/primary_text_light"
android:id="@+id/buttonP3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/materialYellow"
android:text="@string/low_priority"
android:layout_weight="1"
android:onClick="onPrioritySelected"/>
</LinearLayout>
<!-- RadioGroup for visible selection of a priority -->
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:weightSum="3">
<RadioButton
android:id="@+id/radButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onPrioritySelected"
android:layout_weight="1"
android:theme="@style/WhiteRadioButton"/>
<RadioButton
android:id="@+id/radButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onPrioritySelected"
android:layout_weight="1"
android:theme="@style/WhiteRadioButton"/>
<RadioButton
android:id="@+id/radButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onPrioritySelected"
android:layout_weight="1"
android:theme="@style/WhiteRadioButton"/>
</RadioGroup>
</FrameLayout>
<!-- Add button -->
<Button
android:id="@+id/addButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/add_button"
style="@style/TextAppearance.AppCompat.Large"
android:textColor="@android:color/primary_text_dark"
android:background="@color/colorPrimary"
android:onClick="onClickAddTask"/>
</LinearLayout>
/*
* Copyright (C) 2016 The Android Open Source Project
*
* 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.example.android.todolist.data;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.example.android.todolist.data.TaskContract.TaskEntry;
public class TaskDbHelper extends SQLiteOpenHelper {
// The name of the database
private static final String DATABASE_NAME = "tasksDb.db";
// If you change the database schema, you must increment the database version
private static final int VERSION = 1;
// Constructor
TaskDbHelper(Context context) {
super(context, DATABASE_NAME, null, VERSION);
}
/**
* Called when the tasks database is created for the first time.
*/
@Override
public void onCreate(SQLiteDatabase db) {
// Create tasks table (careful to follow SQL formatting rules)
final String CREATE_TABLE = "CREATE TABLE " + TaskEntry.TABLE_NAME + " (" +
TaskEntry._ID + " INTEGER PRIMARY KEY, " +
TaskEntry.COLUMN_DESCRIPTION + " TEXT NOT NULL, " +
TaskEntry.COLUMN_PRIORITY + " INTEGER NOT NULL);";
db.execSQL(CREATE_TABLE);
}
/**
* This method discards the old table of data and calls onCreate to recreate a new one.
* This only occurs when the version number for this database (DATABASE_VERSION) is incremented.
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TaskEntry.TABLE_NAME);
onCreate(db);
}
}
/*
* Copyright (C) 2016 The Android Open Source Project
*
* 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.example.android.todolist.data;
import android.net.Uri;
import android.provider.BaseColumns;
public class TaskContract {
/* Add content provider constants to the Contract
Clients need to know how to access the task data, and it's your job to provide
these content URI's for the path to that data:
1) Content authority,
2) Base content URI,
3) Path(s) to the tasks directory
4) Content URI for data in the TaskEntry class
*/
// The authority, which is how your code knows which Content Provider to access
public static final String AUTHORITY = "com.example.android.todolist";
// The base content URI = "content://" + <authority>
public static final Uri BASE_CONTENT_URI = Uri.parse("content://" + AUTHORITY);
// Define the possible paths for accessing data in this contract
// This is the path for the "tasks" directory
public static final String PATH_TASKS = "tasks";
/* TaskEntry is an inner class that defines the contents of the task table */
public static final class TaskEntry implements BaseColumns {
// TaskEntry content URI = base content URI + path
public static final Uri CONTENT_URI =
BASE_CONTENT_URI.buildUpon().appendPath(PATH_TASKS).build();
// Task table and column names
public static final String TABLE_NAME = "tasks";
// Since TaskEntry implements the interface "BaseColumns", it has an automatically produced
// "_ID" column in addition to the two below
public static final String COLUMN_DESCRIPTION = "description";
public static final String COLUMN_PRIORITY = "priority";
/*
The above table structure looks something like the sample table below.
With the name of the table and columns on top, and potential contents in rows
Note: Because this implements BaseColumns, the _id column is generated automatically
tasks
- - - - - - - - - - - - - - - - - - - - - -
| _id | description | priority |
- - - - - - - - - - - - - - - - - - - - - -
| 1 | Complete lesson | 1 |
- - - - - - - - - - - - - - - - - - - - - -
| 2 | Go shopping | 3 |
- - - - - - - - - - - - - - - - - - - - - -
.
.
.
- - - - - - - - - - - - - - - - - - - - - -
| 43 | Learn guitar | 2 |
- - - - - - - - - - - - - - - - - - - - - -
*/
}
}
/*
* Copyright (C) 2016 The Android Open Source Project
*
* 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.example.android.todolist.data;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.annotation.NonNull;
import static com.example.android.todolist.data.TaskContract.TaskEntry.TABLE_NAME;
// Verify that TaskContentProvider extends from ContentProvider and implements required methods
public class TaskContentProvider extends ContentProvider {
// Define final integer constants for the directory of tasks and a single item.
// It's convention to use 100, 200, 300, etc for directories,
// and related ints (101, 102, ..) for items in that directory.
public static final int TASKS = 100;
public static final int TASK_WITH_ID = 101;
// CDeclare a static variable for the Uri matcher that you construct
private static final UriMatcher sUriMatcher = buildUriMatcher();
// Define a static buildUriMatcher method that associates URI's with their int match
/**
Initialize a new matcher object without any matches,
then use .addURI(String authority, String path, int match) to add matches
*/
public static UriMatcher buildUriMatcher() {
// Initialize a UriMatcher with no matches by passing in NO_MATCH to the constructor
UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
/*
All paths added to the UriMatcher have a corresponding int.
For each kind of uri you may want to access, add the corresponding match with addURI.
The two calls below add matches for the task directory and a single item by ID.
*/
uriMatcher.addURI(TaskContract.AUTHORITY, TaskContract.PATH_TASKS, TASKS);
uriMatcher.addURI(TaskContract.AUTHORITY, TaskContract.PATH_TASKS + "/#", TASK_WITH_ID);
return uriMatcher;
}
// Member variable for a TaskDbHelper that's initialized in the onCreate() method
private TaskDbHelper mTaskDbHelper;
/* onCreate() is where you should initialize anything you’ll need to setup
your underlying data source.
In this case, you’re working with a SQLite database, so you’ll need to
initialize a DbHelper to gain access to it.
*/
@Override
public boolean onCreate() {
// Complete onCreate() and initialize a TaskDbhelper on startup
// [Hint] Declare the DbHelper as a global variable
Context context = getContext();
mTaskDbHelper = new TaskDbHelper(context);
return true;
}
// Implement insert to handle requests to insert a single new row of data
@Override
public Uri insert(@NonNull Uri uri, ContentValues values) {
// Get access to the task database (to write new data to)
final SQLiteDatabase db = mTaskDbHelper.getWritableDatabase();
// Write URI matching code to identify the match for the tasks directory
int match = sUriMatcher.match(uri);
Uri returnUri; // URI to be returned
switch (match) {
case TASKS:
// Insert new values into the database
// Inserting values into tasks table
long id = db.insert(TABLE_NAME, null, values);
if ( id > 0 ) {
returnUri = ContentUris.withAppendedId(TaskContract.TaskEntry.CONTENT_URI, id);
} else {
throw new android.database.SQLException("Failed to insert row into " + uri);
}
break;
// Set the value for the returnedUri and write the default case for unknown URI's
// Default case throws an UnsupportedOperationException
default:
throw new UnsupportedOperationException("Unknown uri: " + uri);
}
// Notify the resolver if the uri has been changed, and return the newly inserted URI
getContext().getContentResolver().notifyChange(uri, null);
// Return constructed uri (this points to the newly inserted row of data)
return returnUri;
}
// Implement query to handle requests for data by URI
@Override
public Cursor query(@NonNull Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
// Get access to underlying database (read-only for query)
final SQLiteDatabase db = mTaskDbHelper.getReadableDatabase();
// Write URI match code and set a variable to return a Cursor
int match = sUriMatcher.match(uri);
Cursor retCursor;
// Query for the tasks directory and write a default case
switch (match) {
// Query for the tasks directory
case TASKS:
retCursor = db.query(TABLE_NAME,
projection,
selection,
selectionArgs,
null,
null,
sortOrder);
break;
case TASKS_WITH_ID:
// Using Selection and SelectionArgs
// URI: content://<content_authority>/tasks/#
String id = uri.getPathSegments().get(1);
//uri.getPathSegments().get(0) will return "tasks"
// Selection is the _id = ?, and SelctionArgs is the row id from the URI
String mSelection = "_id=?";
String[] mSelectionArgs = new String[]{id};
retCursor = db.query(TABLE_NAME,
projection,
mSelection,
mSelectionArgs,
null,
null,
sortOrder);
break;
// Default exception
default:
throw new UnsupportedOperationException("Unknown uri: " + uri);
}
// Set a notification URI on the Cursor and return that Cursor
retCursor.setNotificationUri(getContext().getContentResolver(), uri);
// Return the desired Cursor
return retCursor;
}
// Implement delete to delete a single row of data
@Override
public int delete(@NonNull Uri uri, String selection, String[] selectionArgs) {
// Get access to the database and write URI matching code to recognize a single item
final SQLiteDatabase db = mTaskDbHelper.getWritableDatabase();
int match = sUriMatcher.match(uri);
// Keep track of the number of deleted tasks
int tasksDeleted; // starts as 0
// Write the code to delete a single row of data
// [Hint] Use selections to delete an item by its row ID
switch (match) {
// Handle the single item case, recognized by the ID included in the URI path
case TASK_WITH_ID:
// Get the task ID from the URI path
String id = uri.getPathSegments().get(1);
// Use selections/selectionArgs to filter for this ID
tasksDeleted = db.delete(TABLE_NAME, "_id=?", new String[]{id});
break;
default:
throw new UnsupportedOperationException("Unknown uri: " + uri);
}
// Notify the resolver of a change and return the number of items deleted
if (tasksDeleted != 0) {
// A task was deleted, set notification
getContext().getContentResolver().notifyChange(uri, null);
}
// Return the number of tasks deleted
return tasksDeleted;
}
@Override
public int update(@NonNull Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
//Keep track of if an update occurs
int tasksUpdated;
// match code
int match = sUriMatcher.match(uri);
switch (match) {
case TASK_WITH_ID:
//update a single task by getting the id
String id = uri.getPathSegments().get(1);
//using selections
tasksUpdated = mTaskDbHelper.getWritableDatabase().update(TABLE_NAME, values, "_id=?", new String[]{id});
break;
default:
throw new UnsupportedOperationException("Unknown uri: " + uri);
}
if (tasksUpdated != 0) {
//set notifications if a task was updated
getContext().getContentResolver().notifyChange(uri, null);
}
// return number of tasks updated
return tasksUpdated;
}
/* getType() handles requests for the MIME type of data
We are working with two types of data:
1) a directory and 2) a single row of data.
This method will not be used in our app, but gives a way to standardize the data formats
that your provider accesses, and this can be useful for data organization.
For now, this method will not be used but will be provided for completeness.
*/
@Override
public String getType(@NonNull Uri uri) {
int match = sUriMatcher.match(uri);
switch (match) {
case TASKS:
// directory
return "vnd.android.cursor.dir" + "/" + TaskContract.AUTHORITY + "/" + TaskContract.PATH_TASKS;
case TASK_WITH_ID:
// single item type
return "vnd.android.cursor.item" + "/" + TaskContract.AUTHORITY + "/" + TaskContract.PATH_TASKS;
default:
throw new UnsupportedOperationException("Unknown uri: " + uri);
}
}
}
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion '25.0.2'
defaultConfig {
applicationId "com.example.android.todolist"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
}
}
dataBinding.enabled = true
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:25.1.0'
compile 'com.android.support.constraint:constraint-layout:1.0.0-beta4'
//add Recycler view dependencies; must match SDK version
compile 'com.android.support:recyclerview-v7:25.1.0'
//FAB dependencies
compile 'com.android.support:design:25.1.0'
//Testing
// Instrumentation dependencies use androidTestCompile
// (as opposed to testCompile for local unit tests run in the JVM)
androidTestCompile 'junit:junit:4.12'
androidTestCompile 'com.android.support:support-annotations:25.1.0'
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:rules:0.5'
}
/*
* Copyright (C) 2016 The Android Open Source Project
*
* 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.example.android.todolist;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.AsyncTaskLoader;
import android.support.v4.content.Loader;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;
import android.view.View;
import com.example.android.todolist.data.TaskContract;
public class MainActivity extends AppCompatActivity implements
LoaderManager.LoaderCallbacks<Cursor> {
// Constants for logging and referring to a unique loader
private static final String TAG = MainActivity.class.getSimpleName();
private static final int TASK_LOADER_ID = 0;
// Member variables for the adapter and RecyclerView
private CustomCursorAdapter mAdapter;
RecyclerView mRecyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set the RecyclerView to its corresponding view
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerViewTasks);
// Set the layout for the RecyclerView to be a linear layout, which measures and
// positions items within a RecyclerView into a linear list
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
// Initialize the adapter and attach it to the RecyclerView
mAdapter = new CustomCursorAdapter(this);
mRecyclerView.setAdapter(mAdapter);
/*
Add a touch helper to the RecyclerView to recognize when a user swipes to delete an item.
An ItemTouchHelper enables touch behavior (like swipe and move) on each ViewHolder,
and uses callbacks to signal when a user is performing these actions.
*/
new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}
// Called when a user swipes left or right on a ViewHolder
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {
// Here is where you'll implement swipe to delete
// COMPLETED (1) Construct the URI for the item to delete
//[Hint] Use getTag (from the adapter code) to get the id of the swiped item
// Retrieve the id of the task to delete
int id = (int) viewHolder.itemView.getTag();
// Build appropriate uri with String row id appended
String stringId = Integer.toString(id);
Uri uri = TaskContract.TaskEntry.CONTENT_URI;
uri = uri.buildUpon().appendPath(stringId).build();
// COMPLETED (2) Delete a single row of data using a ContentResolver
getContentResolver().delete(uri, null, null);
// COMPLETED (3) Restart the loader to re-query for all tasks after a deletion
getSupportLoaderManager().restartLoader(TASK_LOADER_ID, null, MainActivity.this);
}
}).attachToRecyclerView(mRecyclerView);
/*
Set the Floating Action Button (FAB) to its corresponding View.
Attach an OnClickListener to it, so that when it's clicked, a new intent will be created
to launch the AddTaskActivity.
*/
FloatingActionButton fabButton = (FloatingActionButton) findViewById(R.id.fab);
fabButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Create a new intent to start an AddTaskActivity
Intent addTaskIntent = new Intent(MainActivity.this, AddTaskActivity.class);
startActivity(addTaskIntent);
}
});
/*
Ensure a loader is initialized and active. If the loader doesn't already exist, one is
created, otherwise the last created loader is re-used.
*/
getSupportLoaderManager().initLoader(TASK_LOADER_ID, null, this);
}
/**
* This method is called after this activity has been paused or restarted.
* Often, this is after new data has been inserted through an AddTaskActivity,
* so this restarts the loader to re-query the underlying data for any changes.
*/
@Override
protected void onResume() {
super.onResume();
// re-queries for all tasks
getSupportLoaderManager().restartLoader(TASK_LOADER_ID, null, this);
}
/**
* Instantiates and returns a new AsyncTaskLoader with the given ID.
* This loader will return task data as a Cursor or null if an error occurs.
*
* Implements the required callbacks to take care of loading data at all stages of loading.
*/
@Override
public Loader<Cursor> onCreateLoader(int id, final Bundle loaderArgs) {
return new AsyncTaskLoader<Cursor>(this) {
// Initialize a Cursor, this will hold all the task data
Cursor mTaskData = null;
// onStartLoading() is called when a loader first starts loading data
@Override
protected void onStartLoading() {
if (mTaskData != null) {
// Delivers any previously loaded data immediately
deliverResult(mTaskData);
} else {
// Force a new load
forceLoad();
}
}
// loadInBackground() performs asynchronous loading of data
@Override
public Cursor loadInBackground() {
// Will implement to load data
// Query and load all task data in the background; sort by priority
// [Hint] use a try/catch block to catch any errors in loading data
try {
return getContentResolver().query(TaskContract.TaskEntry.CONTENT_URI,
null,
null,
null,
TaskContract.TaskEntry.COLUMN_PRIORITY);
} catch (Exception e) {
Log.e(TAG, "Failed to asynchronously load data.");
e.printStackTrace();
return null;
}
}
// deliverResult sends the result of the load, a Cursor, to the registered listener
public void deliverResult(Cursor data) {
mTaskData = data;
super.deliverResult(data);
}
};
}
/**
* Called when a previously created loader has finished its load.
*
* @param loader The Loader that has finished.
* @param data The data generated by the Loader.
*/
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
// Update the data that the adapter uses to create ViewHolders
mAdapter.swapCursor(data);
}
/**
* Called when a previously created loader is being reset, and thus
* making its data unavailable.
* onLoaderReset removes any references this activity had to the loader's data.
*
* @param loader The Loader that is being reset.
*/
@Override
public void onLoaderReset(Loader<Cursor> loader) {
mAdapter.swapCursor(null);
}
}
/*
* Copyright (C) 2016 The Android Open Source Project
*
* 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.example.android.todolist;
import android.content.Context;
import android.database.Cursor;
import android.graphics.drawable.GradientDrawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.android.todolist.data.TaskContract;
/**
* This CustomCursorAdapter creates and binds ViewHolders, that hold the description and priority of a task,
* to a RecyclerView to efficiently display data.
*/
public class CustomCursorAdapter extends RecyclerView.Adapter<CustomCursorAdapter.TaskViewHolder> {
// Class variables for the Cursor that holds task data and the Context
private Cursor mCursor;
private Context mContext;
/**
* Constructor for the CustomCursorAdapter that initializes the Context.
*
* @param mContext the current Context
*/
public CustomCursorAdapter(Context mContext) {
this.mContext = mContext;
}
/**
* Called when ViewHolders are created to fill a RecyclerView.
*
* @return A new TaskViewHolder that holds the view for each task
*/
@Override
public TaskViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// Inflate the task_layout to a view
View view = LayoutInflater.from(mContext)
.inflate(R.layout.task_layout, parent, false);
return new TaskViewHolder(view);
}
/**
* Called by the RecyclerView to display data at a specified position in the Cursor.
*
* @param holder The ViewHolder to bind Cursor data to
* @param position The position of the data in the Cursor
*/
@Override
public void onBindViewHolder(TaskViewHolder holder, int position) {
// Indices for the _id, description, and priority columns
int idIndex = mCursor.getColumnIndex(TaskContract.TaskEntry._ID);
int descriptionIndex = mCursor.getColumnIndex(TaskContract.TaskEntry.COLUMN_DESCRIPTION);
int priorityIndex = mCursor.getColumnIndex(TaskContract.TaskEntry.COLUMN_PRIORITY);
mCursor.moveToPosition(position); // get to the right location in the cursor
// Determine the values of the wanted data
final int id = mCursor.getInt(idIndex);
String description = mCursor.getString(descriptionIndex);
int priority = mCursor.getInt(priorityIndex);
//Set values
holder.itemView.setTag(id);
holder.taskDescriptionView.setText(description);
// Programmatically set the text and color for the priority TextView
String priorityString = "" + priority; // converts int to String
holder.priorityView.setText(priorityString);
GradientDrawable priorityCircle = (GradientDrawable) holder.priorityView.getBackground();
// Get the appropriate background color based on the priority
int priorityColor = getPriorityColor(priority);
priorityCircle.setColor(priorityColor);
}
/*
Helper method for selecting the correct priority circle color.
P1 = red, P2 = orange, P3 = yellow
*/
private int getPriorityColor(int priority) {
int priorityColor = 0;
switch(priority) {
case 1: priorityColor = ContextCompat.getColor(mContext, R.color.materialRed);
break;
case 2: priorityColor = ContextCompat.getColor(mContext, R.color.materialOrange);
break;
case 3: priorityColor = ContextCompat.getColor(mContext, R.color.materialYellow);
break;
default: break;
}
return priorityColor;
}
/**
* Returns the number of items to display.
*/
@Override
public int getItemCount() {
if (mCursor == null) {
return 0;
}
return mCursor.getCount();
}
/**
* When data changes and a re-query occurs, this function swaps the old Cursor
* with a newly updated Cursor (Cursor c) that is passed in.
*/
public Cursor swapCursor(Cursor c) {
// check if this cursor is the same as the previous cursor (mCursor)
if (mCursor == c) {
return null; // bc nothing has changed
}
Cursor temp = mCursor;
this.mCursor = c; // new cursor value assigned
//check if this is a valid cursor, then update the cursor
if (c != null) {
this.notifyDataSetChanged();
}
return temp;
}
// Inner class for creating ViewHolders
class TaskViewHolder extends RecyclerView.ViewHolder {
// Class variables for the task description and priority TextViews
TextView taskDescriptionView;
TextView priorityView;
/**
* Constructor for the TaskViewHolders.
*
* @param itemView The view inflated in onCreateViewHolder
*/
public TaskViewHolder(View itemView) {
super(itemView);
taskDescriptionView = (TextView) itemView.findViewById(R.id.taskDescription);
priorityView = (TextView) itemView.findViewById(R.id.priorityTextView);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.android.todolist">
<application
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<!-- The manifest entry for the MainActivity -->
<activity android:name="com.example.android.todolist.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- AddTaskActivity -->
<activity
android:name="com.example.android.todolist.AddTaskActivity"
android:label="@string/add_task_activity_name" />
<!-- Register the TaskContentProvider -->
<!-- Set name, authorities, and exported attributes -->
<!-- exported = false limits access to this ContentProvider to only this app -->
<provider
android:name="com.example.android.todolist.data.TaskContentProvider"
android:authorities="com.example.android.todolist"
android:exported="false"/>
</application>
</manifest>
/*
* Copyright (C) 2016 The Android Open Source Project
*
* 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.example.android.todolist;
import android.content.ContentValues;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
import com.example.android.todolist.data.TaskContract;
public class AddTaskActivity extends AppCompatActivity {
// Declare a member variable to keep track of a task's selected mPriority
private int mPriority;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_task);
// Initialize to highest mPriority by default (mPriority = 1)
((RadioButton) findViewById(R.id.radButton1)).setChecked(true);
mPriority = 1;
}
/**
* onClickAddTask is called when the "ADD" button is clicked.
* It retrieves user input and inserts that new task data into the underlying database.
*/
public void onClickAddTask(View view) {
// Not yet implemented
// Check if EditText is empty, if not retrieve input and store it in a ContentValues object
// If the EditText input is empty -> don't create an entry
String input = ((EditText) findViewById(R.id.editTextTaskDescription)).getText().toString();
if (input.length() == 0) {
return;
}
// Insert new task data via a ContentResolver
// Create new empty ContentValues object
ContentValues contentValues = new ContentValues();
// Put the task description and selected mPriority into the ContentValues
contentValues.put(TaskContract.TaskEntry.COLUMN_DESCRIPTION, input);
contentValues.put(TaskContract.TaskEntry.COLUMN_PRIORITY, mPriority);
// Insert the content values via a ContentResolver
Uri uri = getContentResolver().insert(TaskContract.TaskEntry.CONTENT_URI, contentValues);
// Display the URI that's returned with a Toast
// [Hint] Don't forget to call finish() to return to MainActivity after this insert is complete
if(uri != null) {
Toast.makeText(getBaseContext(), uri.toString(), Toast.LENGTH_LONG).show();
}
// Finish activity (this returns back to MainActivity)
finish();
}
/**
* onPrioritySelected is called whenever a priority button is clicked.
* It changes the value of mPriority based on the selected button.
*/
public void onPrioritySelected(View view) {
if (((RadioButton) findViewById(R.id.radButton1)).isChecked()) {
mPriority = 1;
} else if (((RadioButton) findViewById(R.id.radButton2)).isChecked()) {
mPriority = 2;
} else if (((RadioButton) findViewById(R.id.radButton3)).isChecked()) {
mPriority = 3;
}
}
}
Content Providers
Steps:
1. Create a class that extends from Content Providers
2. Register your provider in the Manifest File
3. Define URI's
4. Add the URI's to the Contract Class
5. Build a URI Matcher to match URI patterns to int.
6. Implement the REquired CRUD methods inside the provider class
https://developer.android.com/guide/topics/providers/content-providers.html
以上是关于text 9到做名单的主要内容,如果未能解决你的问题,请参考以下文章