Last active
September 11, 2015 14:29
-
-
Save anolivetree/23fdb807246a8269b5af to your computer and use it in GitHub Desktop.
Custom view with CursorLoader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.myapplication; | |
import android.content.Context; | |
import android.database.Cursor; | |
import android.net.Uri; | |
import android.support.v4.content.CursorLoader; | |
import android.support.v4.content.Loader; | |
import android.support.v4.widget.CursorAdapter; | |
import android.util.AttributeSet; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.LinearLayout; | |
import android.widget.ListView; | |
import android.widget.TextView; | |
public class QueryView extends LinearLayout implements Loader.OnLoadCompleteListener<Cursor> { | |
public QueryView(Context context) { | |
super(context); | |
init(); | |
} | |
public QueryView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(); | |
} | |
private void init() { | |
mLoader = new CursorLoader(getContext(), Uri.parse("content://com.example.viewloader"), null, null, null, null); | |
} | |
private ListView mListView; | |
private CursorLoader mLoader; | |
private MyAdapter mAdapter; | |
private boolean mIsRegistered; | |
@Override | |
protected void onFinishInflate() { | |
super.onFinishInflate(); | |
mListView = (ListView) findViewById(R.id.list); | |
mAdapter = new MyAdapter(getContext()); | |
mListView.setAdapter(mAdapter); | |
} | |
@Override | |
protected void onWindowVisibilityChanged(int visibility) { | |
super.onWindowVisibilityChanged(visibility); | |
if (visibility == View.VISIBLE) { | |
if (!mIsRegistered) { | |
mLoader.registerListener(0, this); | |
mLoader.startLoading(); | |
mIsRegistered = true; | |
} | |
} else { | |
if (mIsRegistered) { | |
mLoader.unregisterListener(this); | |
mLoader.stopLoading(); | |
mIsRegistered = false; | |
} | |
} | |
} | |
@Override | |
public void onLoadComplete(Loader<Cursor> loader, Cursor data) { | |
mAdapter.swapCursor(data); | |
} | |
static class MyAdapter extends CursorAdapter { | |
public MyAdapter(Context context) { | |
super(context, null, 0); | |
} | |
@Override | |
public View newView(Context context, Cursor cursor, ViewGroup parent) { | |
View v = LayoutInflater.from(context).inflate(R.layout.row, parent, false); | |
return v; | |
} | |
@Override | |
public void bindView(View view, Context context, Cursor cursor) { | |
TextView vName = (TextView) view.findViewById(R.id.name); | |
TextView vAge = (TextView) view.findViewById(R.id.age); | |
String name = cursor.getString(cursor.getColumnIndexOrThrow("name")); | |
int age = cursor.getInt(cursor.getColumnIndexOrThrow("age")); | |
vName.setText(name); | |
vAge.setText(String.valueOf(age)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment