Skip to content

Instantly share code, notes, and snippets.

@simoales
Last active February 1, 2017 17:39
Show Gist options
  • Save simoales/00babac7595275c2877eff1b3ed2d373 to your computer and use it in GitHub Desktop.
Save simoales/00babac7595275c2877eff1b3ed2d373 to your computer and use it in GitHub Desktop.
package com.example.todos;
import android.app.LoaderManager;
import android.content.ContentValues;
import android.content.CursorLoader;
import android.content.DialogInterface;
import android.content.Loader;
import android.database.Cursor;
import android.databinding.DataBindingUtil;
import android.databinding.ObservableArrayList;
import android.net.Uri;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import com.example.todos.data.TodosContract;
import com.example.todos.data.TodosQueryHandler;
import com.example.todos.databinding.ActivityCategoryBinding;
import com.example.todos.model.Category;
import com.example.todos.model.CategoryList;
public class CategoryActivity extends AppCompatActivity
implements LoaderManager.LoaderCallbacks<Cursor>{
protected Category category;
protected CategoryList categories;
private static final int URL_LOADER = 0;
ObservableArrayList<Category> list;
Cursor cursor;
CategoryListAdapter adapter;
ActivityCategoryBinding binding;
TodosQueryHandler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater inflater = getLayoutInflater();
binding = DataBindingUtil.setContentView(this, R.layout.activity_category);
getLoaderManager().initLoader(URL_LOADER, null, this);
handler = new TodosQueryHandler(getContentResolver());
}
@Override
public void onResume(){
getLoaderManager().restartLoader(URL_LOADER, null, this);
super.onResume();
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String[] projection = {TodosContract.CategoriesEntry.TABLE_NAME
+ "." + TodosContract.CategoriesEntry._ID,
TodosContract.CategoriesEntry.COLUMN_DESCRIPTION};
return new CursorLoader(
this,
TodosContract.CategoriesEntry.CONTENT_URI,
projection,
null, null, null);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
final ListView lv=(ListView) findViewById(R.id.lvCategories);
list = new ObservableArrayList<>();
int i=0;
//fills the observablelist of categories
// Move cursor before first so we can still iterate after config change
data.moveToPosition(-1);
while (data.moveToNext())
{
list.add(i, new Category(
data.getInt(0),
data.getString(1)
));
i++;
}
adapter = new CategoryListAdapter(list);
lv.setAdapter(adapter);
//set bindings
//classes
category = new Category();
categories = new CategoryList(list);
binding.setCategories(categories);
binding.setCategory(category);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
adapter.list = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment