How to connect two cursors through the cursorloader? So far I have the following

public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor> { private AutoCompleteTextView txtSearch; private Spinner spinner; private ListView list; private ImageButton btnClear; DBHeler db; private MyCursorAdapter cursorAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); db = new DBHeler(this); try { db.createDataBase(); db.openDataBase(); } catch (IOException ex) { ex.printStackTrace(); } spinner = (Spinner) findViewById(R.id.spinner); // НастраиваСм Π°Π΄Π°ΠΏΡ‚Π΅Ρ€ ArrayAdapter<?> adapter = ArrayAdapter.createFromResource(this, R.array.types, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // Π’Ρ‹Π·Ρ‹Π²Π°Π΅ΠΌ Π°Π΄Π°ΠΏΡ‚Π΅Ρ€ spinner.setAdapter(adapter); txtSearch = (AutoCompleteTextView) findViewById(R.id.txtSearch); list = (ListView) findViewById(R.id.list); btnClear = (ImageButton) findViewById(R.id.btnClear); /*Π’Ρ‹Π²ΠΎΠ΄ΠΈΠΌ Π² список всС слова, ΠΏΡ€ΠΈ запускС ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΡ‹*/ String selectedItem = spinner.getSelectedItem().toString(); if (selectedItem.equals("Π‘ русского Π½Π° английский")) { cursorAdapter = new MyCursorAdapter(this, R.layout.item, null, 0); list.setAdapter(cursorAdapter); } else if (selectedItem.equals("Π‘ английского Π½Π° русский")) { cursorAdapter = new MyCursorAdapter(this, R.layout.item, null, 0); list.setAdapter(cursorAdapter); } } @Override protected void onResume() { super.onResume(); txtSearch.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View view, boolean b) { if (b && txtSearch.getText().toString().length() > 0) btnClear.setVisibility(View.VISIBLE); else btnClear.setVisibility(View.INVISIBLE); } }); txtSearch.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { if(charSequence.length() != 0){ btnClear.setVisibility(View.VISIBLE); }else{ btnClear.setVisibility(View.INVISIBLE); } } @Override public void afterTextChanged(Editable editable) { } }); list.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { Intent intent = new Intent(MainActivity.this, SlovoActivity.class); CharSequence strCharSequence = ((TextView)view.findViewById(R.id.txtSlovo)).getText(); String str = strCharSequence.toString().toLowerCase().trim(); String selectedItem = spinner.getSelectedItem().toString(); if (selectedItem.equals("Π‘ русского Π½Π° английский")) { intent.putExtra("slovo", str); intent.putExtra("type", "RU"); startActivity(intent); } else if (selectedItem.equals("Π‘ английского Π½Π° русский")) { intent.putExtra("slovo", str); intent.putExtra("type", "EN"); startActivity(intent); } } }); spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { txtSearch.setText(""); String str = adapterView.getItemAtPosition(i).toString(); if (str.equals("Π‘ русского Π½Π° английский")) { getSupportLoaderManager().initLoader(0, null, MainActivity.this); } else if (str.equals("Π‘ английского Π½Π° русский")) { } } @Override public void onNothingSelected(AdapterView<?> adapterView) { } }); } protected void onDestroy() { super.onDestroy(); // Π·Π°ΠΊΡ€Ρ‹Π²Π°Π΅ΠΌ ΠΏΠΎΠ΄ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ ΠΏΡ€ΠΈ Π²Ρ‹Ρ…ΠΎΠ΄Π΅ db.close(); } @Override public Loader onCreateLoader(int id, Bundle args) { return new MyCursorLoader(this, db); } @Override public void onLoadFinished(Loader<Cursor> loader, Cursor data) { cursorAdapter.swapCursor(data); } @Override public void onLoaderReset(Loader<Cursor> loader) { } static class MyCursorLoader extends CursorLoader { DBHeler db; public MyCursorLoader(Context context, DBHeler db) { super(context); this.db = db; } @Override public Cursor loadInBackground() { Cursor cursor = db.getRuWords(); return cursor; } } } 

I need to select the data from the db.getRuWords () cursor in the listView when selecting the value "From Russian to English" in the listView, and if I select the value "From English to Russian", to display the data from the db.getEnWords cursor (). how should the code look like I don’t understand everything?

MADE SO. BUT DOES NOT WORK anyway

 public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor> { private AutoCompleteTextView txtSearch; private Spinner spinner; private ListView list; private ImageButton btnClear; private final int RULOADER = 1; private final int ENLOADER = 1; DBHeler db; private MyCursorAdapter cursorAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); db = new DBHeler(this); try { db.createDataBase(); db.openDataBase(); } catch (IOException ex) { ex.printStackTrace(); } spinner = (Spinner) findViewById(R.id.spinner); // НастраиваСм Π°Π΄Π°ΠΏΡ‚Π΅Ρ€ ArrayAdapter<?> adapter = ArrayAdapter.createFromResource(this, R.array.types, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // Π’Ρ‹Π·Ρ‹Π²Π°Π΅ΠΌ Π°Π΄Π°ΠΏΡ‚Π΅Ρ€ spinner.setAdapter(adapter); txtSearch = (AutoCompleteTextView) findViewById(R.id.txtSearch); list = (ListView) findViewById(R.id.list); btnClear = (ImageButton) findViewById(R.id.btnClear); /*Π’Ρ‹Π²ΠΎΠ΄ΠΈΠΌ Π² список всС слова, ΠΏΡ€ΠΈ запускС ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΡ‹*/ String selectedItem = spinner.getSelectedItem().toString(); if (selectedItem.equals("Π‘ русского Π½Π° английский")) { cursorAdapter = new MyCursorAdapter(this, R.layout.item, null, 0); list.setAdapter(cursorAdapter); } else if (selectedItem.equals("Π‘ английского Π½Π° русский")) { cursorAdapter = new MyCursorAdapter(this, R.layout.item, null, 0); list.setAdapter(cursorAdapter); } } @Override protected void onResume() { super.onResume(); txtSearch.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View view, boolean b) { if (b && txtSearch.getText().toString().length() > 0) btnClear.setVisibility(View.VISIBLE); else btnClear.setVisibility(View.INVISIBLE); } }); txtSearch.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { if(charSequence.length() != 0){ btnClear.setVisibility(View.VISIBLE); }else{ btnClear.setVisibility(View.INVISIBLE); } } @Override public void afterTextChanged(Editable editable) { } }); list.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { Intent intent = new Intent(MainActivity.this, SlovoActivity.class); CharSequence strCharSequence = ((TextView)view.findViewById(R.id.txtSlovo)).getText(); String str = strCharSequence.toString().toLowerCase().trim(); String selectedItem = spinner.getSelectedItem().toString(); if (selectedItem.equals("Π‘ русского Π½Π° английский")) { intent.putExtra("slovo", str); intent.putExtra("type", "RU"); startActivity(intent); } else if (selectedItem.equals("Π‘ английского Π½Π° русский")) { intent.putExtra("slovo", str); intent.putExtra("type", "EN"); startActivity(intent); } } }); spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { txtSearch.setText(""); String str = adapterView.getItemAtPosition(i).toString(); if (str.equals("Π‘ русского Π½Π° английский")) { getSupportLoaderManager().initLoader(RULOADER, null, MainActivity.this); list.setAdapter(cursorAdapter); } else if (str.equals("Π‘ английского Π½Π° русский")) { getSupportLoaderManager().initLoader(ENLOADER, null, MainActivity.this); list.setAdapter(cursorAdapter); } } @Override public void onNothingSelected(AdapterView<?> adapterView) { } }); } protected void onDestroy() { super.onDestroy(); // Π·Π°ΠΊΡ€Ρ‹Π²Π°Π΅ΠΌ ΠΏΠΎΠ΄ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ ΠΏΡ€ΠΈ Π²Ρ‹Ρ…ΠΎΠ΄Π΅ db.close(); } @Override public Loader onCreateLoader(int id, Bundle args) { if (id == RULOADER) { return new RuWords(MainActivity.this, db); } else { return new EnWords(MainActivity.this, db); } } @Override public void onLoadFinished(Loader<Cursor> loader, Cursor data) { //cursorAdapter.swapCursor(data); if(loader instanceof RuWords) { cursorAdapter.swapCursor(data); } else { cursorAdapter.swapCursor(data); } } @Override public void onLoaderReset(Loader<Cursor> loader) { } static class RuWords extends CursorLoader { DBHeler db; public RuWords(Context context, DBHeler db) { super(context); this.db = db; } @Override public Cursor loadInBackground() { Cursor cursor = db.getRuWords(); return cursor; } } static class EnWords extends CursorLoader { DBHeler db; public EnWords(Context context, DBHeler db) { super(context); this.db = db; } @Override public Cursor loadInBackground() { Cursor cursor = db.getEnWords(); return cursor; } } } 

What's wrong?

  • two classes of the successor from CursorLoader to create something? - Veronica
  • This question needs to be removed, since it is basically the same. - pavlofff

1 answer 1

Something like this code, only the important parts of the activation are left (I have nothing to test, unfortunately, so minor flaws are possible). The ID in the methods and arguments indicates which of the loaders to use at the moment.

 public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor> { private Spinner spinner; private ListView list; static final int LOADER_RUS = 0; static final int LOADER_ENG = 1; DBHeler db; private MyCursorAdapter cursorAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); db = new DBHeler(this); spinner = (Spinner) findViewById(R.id.spinner); list = (ListView) findViewById(R.id.list); from = new String[] {Contract.Entry.COLUMN_SLOVO, Contract.Entry.COLUMN_SLOVO, Contract.Entry.COLUMN_IZBRANNOE}; to = new int[] {R.id.txtBukva, R.id.txtSlovo, R.id.btnIzbrannoe}; cursorAdapter = new MyCursorAdapter(this, R.layout.item, null, from, to, 0); list.setAdapter(cursorAdapter); // ΠΈΠ½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·ΠΈΡ€ΡƒΠ΅ΠΌ ΠΎΠ±Π° Π·Π°Π³Ρ€ΡƒΠ·Ρ‡ΠΈΠΊΠ° getSupportLoaderManager().initLoader(LOADER_RUS, null, this); getSupportLoaderManager().initLoader(LOADER_ENG, null, this); spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> adapterView, View view, int loadID, long l) { // обновляСм Π΄Π°Π½Π½Ρ‹Π΅ Π² курсорС getSupportLoaderManager().getLoader(loadID).forceLoad(); } @Override public void onNothingSelected(AdapterView<?> adapterView) { } }); } protected void onDestroy() { super.onDestroy(); db.close(); } @Override public Loader onCreateLoader(int id, Bundle args) { return new MyCursorLoader(this, db, id); } @Override public void onLoadFinished(Loader<Cursor> loader, Cursor data) { cursorAdapter.swapCursor(data); } @Override public void onLoaderReset(Loader<Cursor> loader) { cursorAdapter.swapCursor(null); } static class MyCursorLoader extends CursorLoader { Cursor cursor; DBHeler db; final int LoaderID; public MyCursorLoader(Context context, DBHeler db, int id) { super(context); this.db = db; LoaderID = id; } @Override public Cursor loadInBackground() { switch (LoaderID) { case LOADER_RUS: cursor = db.getRuWords(); break; case LOADER_ENG: cursor = db.getEnWords(); break; } return cursor; } } } 

MyCursorLoader takes the third argument ID, which of the cursors to connect.

When selecting an item in the spinner, the loader is updated by the pressed position (and not by the contents of the list item) - the third argument of the callback with the name loadID in this code passes the position of the selected item. The position can be 0 or 1, the ID of our loaders is also 0 or 1 for the Russian and English set, respectively.

  • in the case where LOADER_RUS and LOADER_ENG scolds writes Non-static field 'LOADER_RUS' cannot be referenced from a static context - Veronica
  • @ Veronica make these constants static . They are generally for convenience of perception, instead you can specify the number 0 and 1 - pavlofff
  • Yes, already done. But it seems to me that this is from = new String [] {Contract.Entry.COLUMN_SLOVO, Contract.Entry.COLUMN_SLOVO, Contract.Entry.COLUMN_IZBRANNOE}; to = new int [] {R.id.txtBukva, R.id.txtSlovo, R.id.btnIzbrannoe}; cursorAdapter = new MyCursorAdapter (this, R.layout.item, null, from, to, 0); too much. In the adapter class, I have so made holder.txtBukva.setText (bukva); holder.txtSlovo.setText (slovo); bukva, slovo I get from the cursor. and the adapter is inherited from the cursoradapter. or is your option better in terms of performance, etc.? - Veronica
  • @ Veronica, if your adapter receives data in another way, then initialize it and transfer the data provided by your adapter. This is an example. - pavlofff