I try to transfer the data to the Activity method from this response . In the myStr array, the last element of the ListView now always appears.
My adapter code:
public class MyCursorAdapter extends CursorAdapter { private int layout; //Π½ΡΠΆΠ΅Π½ Π΄Π»Ρ ΡΠΎΠ·Π΄Π°Π½ΠΈΡ ΠΎΠ±ΡΠ΅ΠΊΡΠΎΠ² ΠΊΠ»Π°ΡΡΠ° View String[] myStr = new String[3]; //OnFavoriteListener callback; public MyCursorAdapter(Context context, int layout, Cursor c, int flags) { super(context, c, flags); this.layout = layout; } public static class ViewHolder { public TextView txtBukva; public TextView txtSlovo; public ImageButton btnIzbrannoe; public void setBtnIzbrannoe(ImageButton btnIzbrannoe) { this.btnIzbrannoe = btnIzbrannoe; } } @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(layout, parent, false); return view; } @Override public void bindView(View view, Context context, final Cursor cursor) { final ViewHolder holder = new ViewHolder(); String bukva = cursor.getString(cursor.getColumnIndex(Contract.Entry.COLUMN_SLOVO)).substring(0, 1).toUpperCase(); final String slovo = cursor.getString(cursor.getColumnIndex(Contract.Entry.COLUMN_SLOVO)); final String izbrannoe = cursor.getString(cursor.getColumnIndex(Contract.Entry.COLUMN_IZBRANNOE)); int _ID = cursor.getInt(cursor.getColumnIndex(Contract.Entry._ID)); holder.txtBukva = (TextView) view.findViewById(R.id.txtBukva); holder.txtSlovo = (TextView) view.findViewById(R.id.txtSlovo); holder.btnIzbrannoe = (ImageButton) view.findViewById(R.id.btnIzbrannoe); holder.txtBukva.setText(bukva); holder.txtSlovo.setText(slovo); holder.btnIzbrannoe.setFocusable(false); if (izbrannoe.equals("1")) { holder.btnIzbrannoe.setImageResource(R.drawable.icon_star_yellow); } else if (izbrannoe.equals("0")) { holder.btnIzbrannoe.setImageResource(R.drawable.icon_star_outline_black); } myStr[0] = _ID + ""; myStr[1] = slovo; myStr[2] = izbrannoe; holder.btnIzbrannoe.setTag(myStr); } } How to transfer the data corresponding to the clicked item?
This is the code that turned out in MainActivity.
public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor> { private AutoCompleteTextView txtSearch; private Spinner spinner; private ListView list; private ImageButton btnClear; DBHeler db; private MySimpleCursorAdapter cursorAdapter; final private static int LOADER_RUS = 0; //0 - ΠΏΠΎΡΠΎΠΌΡ ΡΡΠΎ Π² spinner ΠΏΠ΅ΡΠ²ΠΎΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ ΠΈΠΌΠ΅Π΅Ρ id 0 final private static int LOADER_ENG = 1; //1 - ΠΏΠΎΡΠΎΠΌΡ ΡΡΠΎ Π² spinner ΠΏΠ΅ΡΠ²ΠΎΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ ΠΈΠΌΠ΅Π΅Ρ id 1 @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); txtSearch = (AutoCompleteTextView) findViewById(R.id.txtSearch); list = (ListView) findViewById(R.id.list); btnClear = (ImageButton) findViewById(R.id.btnClear); 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); String[] from = new String[] {Contract.Entry.COLUMN_SLOVO, Contract.Entry.COLUMN_SLOVO, Contract.Entry.COLUMN_PEREVOD, Contract.Entry.COLUMN_IZBRANNOE}; int[] to = new int[] {R.id.txtBukva, R.id.txtSlovo, R.id.txtPerevod, R.id.btnIzbrannoe}; cursorAdapter = new MySimpleCursorAdapter(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() { /*ΠΡΠΈ Π²ΡΠ±ΠΎΡΠ΅ ΠΏΡΠ½ΠΊΡΠ° Π² ΡΠΏΠΈΠ½Π΅ΡΠ΅ ΠΎΠ±Π½ΠΎΠ²Π»Π΅Π½ΠΈΠ΅ Π·Π°Π³ΡΡΠ·ΡΠΈΠΊΠ° ΠΏΡΠΎΠΈΡΡ
ΠΎΠ΄ΠΈΡ ΠΏΠΎ Π½Π°ΠΆΠ°ΡΠΎΠΉ ΠΏΠΎΠ·ΠΈΡΠΈΠΈ (Π° Π½Π΅ ΠΏΠΎ ΡΠΎΠ΄Π΅ΡΠΆΠΈΠΌΠΎΠΌΡ ΠΏΡΠ½ΠΊΡΠ° ΡΠΏΠΈΡΠΊΠ°) - ΡΡΠ΅ΡΠΈΠΉ Π°ΡΠ³ΡΠΌΠ΅Π½Ρ ΠΊΠΎΠ»Π±ΡΠΊΠ° Ρ ΠΈΠΌΠ΅Π½Π΅ΠΌ loadID Π² Π΄Π°Π½Π½ΠΎΠΌ ΠΊΠΎΠ΄Π΅ ΠΏΠ΅ΡΠ΅Π΄Π°Π΅Ρ ΠΏΠΎΠ·ΠΈΡΠΈΡ Π²ΡΠ±ΡΠ°Π½Π½ΠΎΠ³ΠΎ ΠΏΡΠ½ΠΊΡΠ°. ΠΠΎΠ·ΠΈΡΠΈΡ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ 0 ΠΈΠ»ΠΈ 1, ID Π½Π°ΡΠΈΡ
Π·Π°Π³ΡΡΠ·ΡΠΈΠΊΠΎΠ² ΡΠ°ΠΊ ΠΆΠ΅ 0 ΠΈΠ»ΠΈ 1 Π΄Π»Ρ ΡΡΡΡΠΊΠΎΠ³ΠΎ ΠΈ Π°Π½Π³Π»ΠΈΠΉΡΠΊΠΎΠ³ΠΎ Π½Π°Π±ΠΎΡΠ° ΡΠΎΠΎΡΠ²Π΅ΡΡΡΠ²Π΅Π½Π½ΠΎ.*/ @Override public void onItemSelected(AdapterView<?> adapterView, View view, int loadID, long l) { getSupportLoaderManager().getLoader(loadID).forceLoad(); // ΠΎΠ±Π½ΠΎΠ²Π»ΡΠ΅ΠΌ Π΄Π°Π½Π½ΡΠ΅ Π² ΠΊΡΡΡΠΎΡΠ΅ } @Override public void onNothingSelected(AdapterView<?> adapterView) { } }); } @Override protected void onResume() { super.onResume(); 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); } } }); 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) { String[] from = new String[] {Contract.Entry.COLUMN_SLOVO, Contract.Entry.COLUMN_SLOVO, Contract.Entry.COLUMN_PEREVOD, Contract.Entry.COLUMN_IZBRANNOE}; int[] to = new int[] {R.id.txtBukva, R.id.txtSlovo, R.id.txtPerevod, R.id.btnIzbrannoe}; cursorAdapter = new MySimpleCursorAdapter(MainActivity.this, R.layout.item, null, from, to, 0); list.setAdapter(cursorAdapter); } @Override public void afterTextChanged(Editable editable) { } }); } public void onFavoriteClick(View view) { Slovo data = (Slovo)view.getTag(); long id = data.id; String slovo = data.word; String perevod = data.perevod; String bukva = data.simbol; boolean izbrannoe = data.favorite; //System.out.println(id + " " + slovo + " " + perevod + " " + bukva + " " + izbrannoe); if (izbrannoe == false) { ContentValues values = new ContentValues(); values.put(Contract.Entry.COLUMN_IZBRANNOE, "1"); // ΠΡΡΠ°Π²Π»ΡΠ΅ΠΌ Π½ΠΎΠ²ΡΠΉ ΡΡΠ΄ Π² Π±Π°Π·Ρ Π΄Π°Π½Π½ΡΡ
ΠΈ Π·Π°ΠΏΠΎΠΌΠΈΠ½Π°Π΅ΠΌ Π΅Π³ΠΎ ΠΈΠ΄Π΅Π½ΡΠΈΡΠΈΠΊΠ°ΡΠΎΡ long newRowId = db.database.update(Contract.Entry.TABLE_RUEN, values, Contract.Entry.COLUMN_SLOVO + "= ?", new String[]{slovo}); izbrannoe = true; // ΠΡΠ²ΠΎΠ΄ΠΈΠΌ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠ΅ Π² ΡΡΠΏΠ΅ΡΠ½ΠΎΠΌ ΡΠ»ΡΡΠ°Π΅ ΠΈΠ»ΠΈ ΠΏΡΠΈ ΠΎΡΠΈΠ±ΠΊΠ΅ if (newRowId == -1) { // ΠΡΠ»ΠΈ ID -1, Π·Π½Π°ΡΠΈΡ ΠΏΡΠΎΠΈΠ·ΠΎΡΠ»Π° ΠΎΡΠΈΠ±ΠΊΠ° Toast.makeText(this, "ΠΡΠΈΠ±ΠΊΠ°", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "ΠΠΎΠ±Π°Π²Π»Π΅Π½ΠΎ Π² ΠΈΠ·Π±ΡΠ°Π½Π½ΠΎΠ΅", Toast.LENGTH_SHORT).show(); getSupportLoaderManager().getLoader(LOADER_RUS).forceLoad(); } } else if (izbrannoe == true) { ContentValues values = new ContentValues(); values.put(Contract.Entry.COLUMN_IZBRANNOE, "0"); // ΠΡΡΠ°Π²Π»ΡΠ΅ΠΌ Π½ΠΎΠ²ΡΠΉ ΡΡΠ΄ Π² Π±Π°Π·Ρ Π΄Π°Π½Π½ΡΡ
ΠΈ Π·Π°ΠΏΠΎΠΌΠΈΠ½Π°Π΅ΠΌ Π΅Π³ΠΎ ΠΈΠ΄Π΅Π½ΡΠΈΡΠΈΠΊΠ°ΡΠΎΡ long newRowId = db.database.update(Contract.Entry.TABLE_RUEN, values, Contract.Entry.COLUMN_SLOVO + "= ?", new String[]{slovo}); izbrannoe = true; // ΠΡΠ²ΠΎΠ΄ΠΈΠΌ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠ΅ Π² ΡΡΠΏΠ΅ΡΠ½ΠΎΠΌ ΡΠ»ΡΡΠ°Π΅ ΠΈΠ»ΠΈ ΠΏΡΠΈ ΠΎΡΠΈΠ±ΠΊΠ΅ if (newRowId == -1) { // ΠΡΠ»ΠΈ ID -1, Π·Π½Π°ΡΠΈΡ ΠΏΡΠΎΠΈΠ·ΠΎΡΠ»Π° ΠΎΡΠΈΠ±ΠΊΠ° Toast.makeText(this, "ΠΡΠΈΠ±ΠΊΠ°", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "Π£Π΄Π°Π»Π΅Π½ΠΎ ΠΈΠ· ΠΈΠ·Π±ΡΠ°Π½Π½ΠΎΠ³ΠΎ", Toast.LENGTH_SHORT).show(); getSupportLoaderManager().getLoader(LOADER_RUS).forceLoad(); } } } public void onClearBtnClick(View view) { txtSearch.setText(""); btnClear.setVisibility(View.INVISIBLE); } protected void onDestroy() { super.onDestroy(); db.close(); } static class MyCursorLoader extends CursorLoader { Cursor cursor; DBHeler dbHeler; final int loaderID; public MyCursorLoader(Context context, DBHeler dbHeler, int id) { super(context); this.dbHeler = dbHeler; loaderID = id; } @Override protected Cursor onLoadInBackground() { switch (loaderID) { case LOADER_RUS: cursor = dbHeler.getRuWords(); break; case LOADER_ENG: cursor = dbHeler.getEnWords(); break; } return cursor; } } @Override public Loader<Cursor> 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); } }
TextView, and in themyStrarray they also appear from the last item? - pavlofff