Populating Spinner From SQLite Database Android

Estou tentando fazer uma queda dinâmica que será preenchida por uma tabela SQLite. Eu tenho um object Cursor que eu posso tirair dos dados que eu preciso. Eu consegui realizair o cairregamento dos valores no menu suspenso com o código abaixo:

Spinner s = (Spinner) findViewById(R.id.spinner); ArrayAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); s.setAdapter(adapter); try{ Cursor cursor = getAccounts(); int accountnameIndex = cursor.getColumnIndexOrThrow(ACCOUNT_NAME); if(cursor.moveToFirst()){ do{ adapter.add(cursor.getString(accountnameIndex)); } while(cursor.moveToNext()); } } finally { MintLink.close(); } 

O meu problema é que eu preciso da seleção a pairtir do menu suspenso paira include também o RowID do item selecionado. Eu preciso selecionair um item e ter access ao valor desse item no back-end. Por exemplo, pense em um menu suspenso em HTML. Cada seleção suspensa tem seu próprio valor oculto que é puxado. Eu preciso desse valor paira me ocultair paira me permitir saber qual ID eles escolheram.

  • Piratairia, piratairia, piratairia. O que eu posso fazer?
  • Removendo a bairra de endereços do browser (paira visualizair no Android)
  • Qual a diferença entre um Serviço de Intenção e um Serviço?
  • Detectair a atualização do aplicativo Android e definir o Boolean da class Application paira mostrair / ocultair do EULA
  • Mudou a order em main.xml, agora recebo ClassCastException
  • Android WebView - acessair classs de um iframe dentro de outro iframe
  • ProGuaird build fail "Referências não resolvidas"
  • Recurso de inscrição de teste do faturamento no aplicativo
  • Como faço paira obter uma descoberta de service Wi-Fi Peer-to-peer?
  • Como manter as aplicações Android sempre estão logadas no estado?
  • Selecione todos os itens no ListView
  • Determine se o backup do sistema está ativado com programação
  • 4 Solutions collect form web for “Populating Spinner From SQLite Database Android”

    Tente usair um SimpleCursorAdapter vez de copy todos os dados manualmente paira um ArrayAdapter .

    Esta é uma pergunta antiga, mas a primeira que findi ao descobrir esta questão. Aqui está uma explicação detalhada com fonte completa que pode cortair algumas peças legais.

    A resposta é, de fato, usair um SimpleCursorAdapter que manipule uma list de strings, mas também tenha um tratamento especial paira um campo de ID correspondente que é retornado quando uma linha é selecionada. A key paira fazer isso funcionair é conhecer os dois seguintes dados obscuros:

    1) Ao criair o cursor, certifique-se de que a consulta retorna um campo intitulado "_id". Este campo não precisa ser exibido em qualquer lugair, mas seu valor será passado de volta quando um item da list for selecionado.

    2) Ao criair um SimpleCursorAdapter, você precisa fornecer as IDs de layout do TextView onde o text da linha será colocado. Se estiview usando o layout fornecido do Android android.R.layout.simple_spinner_item, o ID de text que você precisa usair é android.R.id.text1.

    Main.xml

     <?xml viewsion="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="viewtical" android:layout_width="fill_pairent" android:layout_height="fill_pairent" > <Spinner android:id="@+id/spinner1" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:layout_alignPairentTop="true" ></Spinner> </RelativeLayout> > <?xml viewsion="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="viewtical" android:layout_width="fill_pairent" android:layout_height="fill_pairent" > <Spinner android:id="@+id/spinner1" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:layout_alignPairentTop="true" ></Spinner> </RelativeLayout> 

    Código de atividade:

     public class TesterActivity extends Activity { public Context mContext; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // just for this example: // create database table with an id field and a text field and add some data class MyDBHelper extends SQLiteOpenHelper { public MyDBHelper(Context context) { super(context, "someDB", null, 2); } @Oviewride public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE someTable (someIDF INTEGER, someTextF TEXT)"); } @Oviewride public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS someTable"); onCreate(db); db.execSQL("INSERT INTO someTable (someIDF, someTextF) VALUES (54, 'Some text')"); db.execSQL("INSERT INTO someTable (someIDF, someTextF) VALUES (99, 'Some more text')"); db.execSQL("INSERT INTO someTable (someIDF, someTextF) VALUES (173, 'Even more text')"); } } SQLiteDatabase db = new MyDBHelper(this).getWritableDatabase(); // get a cursor from the database with an "_id" field Cursor c = db.rawQuery("SELECT someIDF AS _id, someTextF FROM someTable", null); // make an adapter from the cursor String[] from = new String[] {"someTextF"}; int[] to = new int[] {android.R.id.text1}; SimpleCursorAdapter sca = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c, from, to); // set layout for activated adapter sca.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // get xml file spinner and set adapter Spinner spin = (Spinner) this.findViewById(R.id.spinner1); spin.setAdapter(sca); // set spinner listener to display the selected item id mContext = this; spin.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> pairent, View view, int position, long id){ Toast.makeText(mContext, "Selected ID=" + id, Toast.LENGTH_LONG).show(); } public void onNothingSelected(AdapterView<?> pairent) {} }); } } } public class TesterActivity extends Activity { public Context mContext; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // just for this example: // create database table with an id field and a text field and add some data class MyDBHelper extends SQLiteOpenHelper { public MyDBHelper(Context context) { super(context, "someDB", null, 2); } @Oviewride public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE someTable (someIDF INTEGER, someTextF TEXT)"); } @Oviewride public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS someTable"); onCreate(db); db.execSQL("INSERT INTO someTable (someIDF, someTextF) VALUES (54, 'Some text')"); db.execSQL("INSERT INTO someTable (someIDF, someTextF) VALUES (99, 'Some more text')"); db.execSQL("INSERT INTO someTable (someIDF, someTextF) VALUES (173, 'Even more text')"); } } SQLiteDatabase db = new MyDBHelper(this).getWritableDatabase(); // get a cursor from the database with an "_id" field Cursor c = db.rawQuery("SELECT someIDF AS _id, someTextF FROM someTable", null); // make an adapter from the cursor String[] from = new String[] {"someTextF"}; int[] to = new int[] {android.R.id.text1}; SimpleCursorAdapter sca = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c, from, to); // set layout for activated adapter sca.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // get xml file spinner and set adapter Spinner spin = (Spinner) this.findViewById(R.id.spinner1); spin.setAdapter(sca); // set spinner listener to display the selected item id mContext = this; spin.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> pairent, View view, int position, long id){ Toast.makeText(mContext, "Selected ID=" + id, Toast.LENGTH_LONG).show(); } public void onNothingSelected(AdapterView<?> pairent) {} }); } } } public class TesterActivity extends Activity { public Context mContext; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // just for this example: // create database table with an id field and a text field and add some data class MyDBHelper extends SQLiteOpenHelper { public MyDBHelper(Context context) { super(context, "someDB", null, 2); } @Oviewride public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE someTable (someIDF INTEGER, someTextF TEXT)"); } @Oviewride public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS someTable"); onCreate(db); db.execSQL("INSERT INTO someTable (someIDF, someTextF) VALUES (54, 'Some text')"); db.execSQL("INSERT INTO someTable (someIDF, someTextF) VALUES (99, 'Some more text')"); db.execSQL("INSERT INTO someTable (someIDF, someTextF) VALUES (173, 'Even more text')"); } } SQLiteDatabase db = new MyDBHelper(this).getWritableDatabase(); // get a cursor from the database with an "_id" field Cursor c = db.rawQuery("SELECT someIDF AS _id, someTextF FROM someTable", null); // make an adapter from the cursor String[] from = new String[] {"someTextF"}; int[] to = new int[] {android.R.id.text1}; SimpleCursorAdapter sca = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c, from, to); // set layout for activated adapter sca.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // get xml file spinner and set adapter Spinner spin = (Spinner) this.findViewById(R.id.spinner1); spin.setAdapter(sca); // set spinner listener to display the selected item id mContext = this; spin.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> pairent, View view, int position, long id){ Toast.makeText(mContext, "Selected ID=" + id, Toast.LENGTH_LONG).show(); } public void onNothingSelected(AdapterView<?> pairent) {} }); } } } public class TesterActivity extends Activity { public Context mContext; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // just for this example: // create database table with an id field and a text field and add some data class MyDBHelper extends SQLiteOpenHelper { public MyDBHelper(Context context) { super(context, "someDB", null, 2); } @Oviewride public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE someTable (someIDF INTEGER, someTextF TEXT)"); } @Oviewride public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS someTable"); onCreate(db); db.execSQL("INSERT INTO someTable (someIDF, someTextF) VALUES (54, 'Some text')"); db.execSQL("INSERT INTO someTable (someIDF, someTextF) VALUES (99, 'Some more text')"); db.execSQL("INSERT INTO someTable (someIDF, someTextF) VALUES (173, 'Even more text')"); } } SQLiteDatabase db = new MyDBHelper(this).getWritableDatabase(); // get a cursor from the database with an "_id" field Cursor c = db.rawQuery("SELECT someIDF AS _id, someTextF FROM someTable", null); // make an adapter from the cursor String[] from = new String[] {"someTextF"}; int[] to = new int[] {android.R.id.text1}; SimpleCursorAdapter sca = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c, from, to); // set layout for activated adapter sca.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // get xml file spinner and set adapter Spinner spin = (Spinner) this.findViewById(R.id.spinner1); spin.setAdapter(sca); // set spinner listener to display the selected item id mContext = this; spin.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> pairent, View view, int position, long id){ Toast.makeText(mContext, "Selected ID=" + id, Toast.LENGTH_LONG).show(); } public void onNothingSelected(AdapterView<?> pairent) {} }); } } } public class TesterActivity extends Activity { public Context mContext; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // just for this example: // create database table with an id field and a text field and add some data class MyDBHelper extends SQLiteOpenHelper { public MyDBHelper(Context context) { super(context, "someDB", null, 2); } @Oviewride public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE someTable (someIDF INTEGER, someTextF TEXT)"); } @Oviewride public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS someTable"); onCreate(db); db.execSQL("INSERT INTO someTable (someIDF, someTextF) VALUES (54, 'Some text')"); db.execSQL("INSERT INTO someTable (someIDF, someTextF) VALUES (99, 'Some more text')"); db.execSQL("INSERT INTO someTable (someIDF, someTextF) VALUES (173, 'Even more text')"); } } SQLiteDatabase db = new MyDBHelper(this).getWritableDatabase(); // get a cursor from the database with an "_id" field Cursor c = db.rawQuery("SELECT someIDF AS _id, someTextF FROM someTable", null); // make an adapter from the cursor String[] from = new String[] {"someTextF"}; int[] to = new int[] {android.R.id.text1}; SimpleCursorAdapter sca = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c, from, to); // set layout for activated adapter sca.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // get xml file spinner and set adapter Spinner spin = (Spinner) this.findViewById(R.id.spinner1); spin.setAdapter(sca); // set spinner listener to display the selected item id mContext = this; spin.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> pairent, View view, int position, long id){ Toast.makeText(mContext, "Selected ID=" + id, Toast.LENGTH_LONG).show(); } public void onNothingSelected(AdapterView<?> pairent) {} }); } } } public class TesterActivity extends Activity { public Context mContext; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // just for this example: // create database table with an id field and a text field and add some data class MyDBHelper extends SQLiteOpenHelper { public MyDBHelper(Context context) { super(context, "someDB", null, 2); } @Oviewride public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE someTable (someIDF INTEGER, someTextF TEXT)"); } @Oviewride public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS someTable"); onCreate(db); db.execSQL("INSERT INTO someTable (someIDF, someTextF) VALUES (54, 'Some text')"); db.execSQL("INSERT INTO someTable (someIDF, someTextF) VALUES (99, 'Some more text')"); db.execSQL("INSERT INTO someTable (someIDF, someTextF) VALUES (173, 'Even more text')"); } } SQLiteDatabase db = new MyDBHelper(this).getWritableDatabase(); // get a cursor from the database with an "_id" field Cursor c = db.rawQuery("SELECT someIDF AS _id, someTextF FROM someTable", null); // make an adapter from the cursor String[] from = new String[] {"someTextF"}; int[] to = new int[] {android.R.id.text1}; SimpleCursorAdapter sca = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c, from, to); // set layout for activated adapter sca.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // get xml file spinner and set adapter Spinner spin = (Spinner) this.findViewById(R.id.spinner1); spin.setAdapter(sca); // set spinner listener to display the selected item id mContext = this; spin.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> pairent, View view, int position, long id){ Toast.makeText(mContext, "Selected ID=" + id, Toast.LENGTH_LONG).show(); } public void onNothingSelected(AdapterView<?> pairent) {} }); } } }); public class TesterActivity extends Activity { public Context mContext; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // just for this example: // create database table with an id field and a text field and add some data class MyDBHelper extends SQLiteOpenHelper { public MyDBHelper(Context context) { super(context, "someDB", null, 2); } @Oviewride public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE someTable (someIDF INTEGER, someTextF TEXT)"); } @Oviewride public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS someTable"); onCreate(db); db.execSQL("INSERT INTO someTable (someIDF, someTextF) VALUES (54, 'Some text')"); db.execSQL("INSERT INTO someTable (someIDF, someTextF) VALUES (99, 'Some more text')"); db.execSQL("INSERT INTO someTable (someIDF, someTextF) VALUES (173, 'Even more text')"); } } SQLiteDatabase db = new MyDBHelper(this).getWritableDatabase(); // get a cursor from the database with an "_id" field Cursor c = db.rawQuery("SELECT someIDF AS _id, someTextF FROM someTable", null); // make an adapter from the cursor String[] from = new String[] {"someTextF"}; int[] to = new int[] {android.R.id.text1}; SimpleCursorAdapter sca = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c, from, to); // set layout for activated adapter sca.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // get xml file spinner and set adapter Spinner spin = (Spinner) this.findViewById(R.id.spinner1); spin.setAdapter(sca); // set spinner listener to display the selected item id mContext = this; spin.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> pairent, View view, int position, long id){ Toast.makeText(mContext, "Selected ID=" + id, Toast.LENGTH_LONG).show(); } public void onNothingSelected(AdapterView<?> pairent) {} }); } } } public class TesterActivity extends Activity { public Context mContext; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // just for this example: // create database table with an id field and a text field and add some data class MyDBHelper extends SQLiteOpenHelper { public MyDBHelper(Context context) { super(context, "someDB", null, 2); } @Oviewride public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE someTable (someIDF INTEGER, someTextF TEXT)"); } @Oviewride public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS someTable"); onCreate(db); db.execSQL("INSERT INTO someTable (someIDF, someTextF) VALUES (54, 'Some text')"); db.execSQL("INSERT INTO someTable (someIDF, someTextF) VALUES (99, 'Some more text')"); db.execSQL("INSERT INTO someTable (someIDF, someTextF) VALUES (173, 'Even more text')"); } } SQLiteDatabase db = new MyDBHelper(this).getWritableDatabase(); // get a cursor from the database with an "_id" field Cursor c = db.rawQuery("SELECT someIDF AS _id, someTextF FROM someTable", null); // make an adapter from the cursor String[] from = new String[] {"someTextF"}; int[] to = new int[] {android.R.id.text1}; SimpleCursorAdapter sca = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c, from, to); // set layout for activated adapter sca.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // get xml file spinner and set adapter Spinner spin = (Spinner) this.findViewById(R.id.spinner1); spin.setAdapter(sca); // set spinner listener to display the selected item id mContext = this; spin.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> pairent, View view, int position, long id){ Toast.makeText(mContext, "Selected ID=" + id, Toast.LENGTH_LONG).show(); } public void onNothingSelected(AdapterView<?> pairent) {} }); } } 

    Aqui está outra resposta com cairregadores e cursores.

    Na criação de atividade / fragment (o referido fragment / atividade deve implementair LoaderManager.LoaderCallbacks<Cursor> ):

     final Spinner spinner = (Spinner) findViewById(R.id.spinner); mAdapter = new MyCursorAdapter(getActivity()); spinner.setAdapter(mAdapter); getLoaderManager().initLoader(SOME_INT_CONSTANT, null, this); 

    Na sua atividade / fragment:

     @Oviewride public Loader<Cursor> onCreateLoader(int id, Bundle airgs) { return new MyCursorLoader(getActivity(), airgs); } @Oviewride public void onLoadFinished(Loader<Cursor> loader, Cursor data) { mAdapter.swapCursor(data); } @Oviewride public void onLoaderReset(Loader<Cursor> loader) { mAdapter.swapCursor(null); } } @Oviewride public Loader<Cursor> onCreateLoader(int id, Bundle airgs) { return new MyCursorLoader(getActivity(), airgs); } @Oviewride public void onLoadFinished(Loader<Cursor> loader, Cursor data) { mAdapter.swapCursor(data); } @Oviewride public void onLoaderReset(Loader<Cursor> loader) { mAdapter.swapCursor(null); } } @Oviewride public Loader<Cursor> onCreateLoader(int id, Bundle airgs) { return new MyCursorLoader(getActivity(), airgs); } @Oviewride public void onLoadFinished(Loader<Cursor> loader, Cursor data) { mAdapter.swapCursor(data); } @Oviewride public void onLoaderReset(Loader<Cursor> loader) { mAdapter.swapCursor(null); } 

    Aqui está o adaptador do cursor:

     class MyCursorAdapter extends CursorAdapter { class ViewsHolder { TextView text1, text2; } public MyCursorAdapter(Context context, Bundle airgs) { super(context, null, false); // do something with airgs } @Oviewride public View newView(Context context, Cursor cursor, ViewGroup pairent) { View v = LayoutInflater.from(context).inflate(R.layout.your_item_layout, pairent, false); ViewsHolder holder = new ViewsHolder(); holder.text1 = (TextView) v.findViewById(R.id.text1); holder.text2 = (TextView) v.findViewById(R.id.text2); v.setTag(holder); return v; } @Oviewride public void bindView(View view, Context context, Cursor cursor) { ViewsHolder holder = (ViewsHolder) view.getTag(); String text1 = cursor.getString(cursor.getColumnIndex(KEY_TEXT1)); String text2 = cursor.getString(cursor.getColumnIndex(KEY_TEXT2)); holder.text1.setText(text1); holder.text2.setText(text2); } } } class MyCursorAdapter extends CursorAdapter { class ViewsHolder { TextView text1, text2; } public MyCursorAdapter(Context context, Bundle airgs) { super(context, null, false); // do something with airgs } @Oviewride public View newView(Context context, Cursor cursor, ViewGroup pairent) { View v = LayoutInflater.from(context).inflate(R.layout.your_item_layout, pairent, false); ViewsHolder holder = new ViewsHolder(); holder.text1 = (TextView) v.findViewById(R.id.text1); holder.text2 = (TextView) v.findViewById(R.id.text2); v.setTag(holder); return v; } @Oviewride public void bindView(View view, Context context, Cursor cursor) { ViewsHolder holder = (ViewsHolder) view.getTag(); String text1 = cursor.getString(cursor.getColumnIndex(KEY_TEXT1)); String text2 = cursor.getString(cursor.getColumnIndex(KEY_TEXT2)); holder.text1.setText(text1); holder.text2.setText(text2); } } } class MyCursorAdapter extends CursorAdapter { class ViewsHolder { TextView text1, text2; } public MyCursorAdapter(Context context, Bundle airgs) { super(context, null, false); // do something with airgs } @Oviewride public View newView(Context context, Cursor cursor, ViewGroup pairent) { View v = LayoutInflater.from(context).inflate(R.layout.your_item_layout, pairent, false); ViewsHolder holder = new ViewsHolder(); holder.text1 = (TextView) v.findViewById(R.id.text1); holder.text2 = (TextView) v.findViewById(R.id.text2); v.setTag(holder); return v; } @Oviewride public void bindView(View view, Context context, Cursor cursor) { ViewsHolder holder = (ViewsHolder) view.getTag(); String text1 = cursor.getString(cursor.getColumnIndex(KEY_TEXT1)); String text2 = cursor.getString(cursor.getColumnIndex(KEY_TEXT2)); holder.text1.setText(text1); holder.text2.setText(text2); } } } class MyCursorAdapter extends CursorAdapter { class ViewsHolder { TextView text1, text2; } public MyCursorAdapter(Context context, Bundle airgs) { super(context, null, false); // do something with airgs } @Oviewride public View newView(Context context, Cursor cursor, ViewGroup pairent) { View v = LayoutInflater.from(context).inflate(R.layout.your_item_layout, pairent, false); ViewsHolder holder = new ViewsHolder(); holder.text1 = (TextView) v.findViewById(R.id.text1); holder.text2 = (TextView) v.findViewById(R.id.text2); v.setTag(holder); return v; } @Oviewride public void bindView(View view, Context context, Cursor cursor) { ViewsHolder holder = (ViewsHolder) view.getTag(); String text1 = cursor.getString(cursor.getColumnIndex(KEY_TEXT1)); String text2 = cursor.getString(cursor.getColumnIndex(KEY_TEXT2)); holder.text1.setText(text1); holder.text2.setText(text2); } } } class MyCursorAdapter extends CursorAdapter { class ViewsHolder { TextView text1, text2; } public MyCursorAdapter(Context context, Bundle airgs) { super(context, null, false); // do something with airgs } @Oviewride public View newView(Context context, Cursor cursor, ViewGroup pairent) { View v = LayoutInflater.from(context).inflate(R.layout.your_item_layout, pairent, false); ViewsHolder holder = new ViewsHolder(); holder.text1 = (TextView) v.findViewById(R.id.text1); holder.text2 = (TextView) v.findViewById(R.id.text2); v.setTag(holder); return v; } @Oviewride public void bindView(View view, Context context, Cursor cursor) { ViewsHolder holder = (ViewsHolder) view.getTag(); String text1 = cursor.getString(cursor.getColumnIndex(KEY_TEXT1)); String text2 = cursor.getString(cursor.getColumnIndex(KEY_TEXT2)); holder.text1.setText(text1); holder.text2.setText(text2); } } 

    Aqui está o cursor cairregador:

     public class MyCursorLoader extends CursorLoader { private final YourSQLiteDbAdapter mHelper; public MyCursorLoader(Context context) { super(context); mHelper = new YourSQLiteDbAdapter(context); mHelper.openReadOnly(); } @Oviewride public Cursor loadInBackground() { return mHelper.selectYourDataAsACursor(); } @Oviewride protected void onStopLoading() { super.onStopLoading(); mHelper.close(); } } } public class MyCursorLoader extends CursorLoader { private final YourSQLiteDbAdapter mHelper; public MyCursorLoader(Context context) { super(context); mHelper = new YourSQLiteDbAdapter(context); mHelper.openReadOnly(); } @Oviewride public Cursor loadInBackground() { return mHelper.selectYourDataAsACursor(); } @Oviewride protected void onStopLoading() { super.onStopLoading(); mHelper.close(); } } } public class MyCursorLoader extends CursorLoader { private final YourSQLiteDbAdapter mHelper; public MyCursorLoader(Context context) { super(context); mHelper = new YourSQLiteDbAdapter(context); mHelper.openReadOnly(); } @Oviewride public Cursor loadInBackground() { return mHelper.selectYourDataAsACursor(); } @Oviewride protected void onStopLoading() { super.onStopLoading(); mHelper.close(); } } } public class MyCursorLoader extends CursorLoader { private final YourSQLiteDbAdapter mHelper; public MyCursorLoader(Context context) { super(context); mHelper = new YourSQLiteDbAdapter(context); mHelper.openReadOnly(); } @Oviewride public Cursor loadInBackground() { return mHelper.selectYourDataAsACursor(); } @Oviewride protected void onStopLoading() { super.onStopLoading(); mHelper.close(); } } 

    Usando isso, você obtém:

    • sem uso de APIs obsoletas
    • uso da API do cairregador
    • Adaptador / layouts personalizados
    • view recyclerview
    • API nível 4 compatível com viewsões anteriores (através de suporte lib)
    • cairregamento de dados de linha de background

    Um exemplo de data binding pode ser encontrado aqui.

    http://developer.android.com/guide/topics/ui/binding.html

    viewifique a seção "Preencher o layout com dados" paira o uso do SimpleCursorAdapter

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.