Hello! Tell me how you can implement adding favorites and deleting?

There are two activites. In the main MainActivity , MainActivity , the ListView MainActivity ; when you click on a list item, the second full-text MainActivity opens. So in the second activation, I added FloatingActionButton , when I clicked on it, the article will be added to my favorites. But how to implement it, I do not quite understand. Maybe someone knows, at least tell me the direction in which to move.

Full code:

MainActivity list MainActivity

 public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); // ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ экзСмпляр элСмСнта ListView ListView listView = (ListView)findViewById(R.id.listView); TextView textV = (TextView) findViewById(R.id.textV); // опрСдСляСм массив Ρ‚ΠΈΠΏΠ° String // Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΊΠΈ Π² спискС final String[] catnames = getResources().getStringArray(R.array.cat_names); // тСкст Π²Ρ‹Π²ΠΎΠ΄ΠΈΡ‚ΡŒΡΡ Π²ΠΎ Π²Ρ‚ΠΎΡ€ΠΎΠΌ Π°ΠΊΡ‚ΠΈΠ²ΠΈΡ‚ΠΈ final String[] catnames2 = getResources().getStringArray(R.array.cat_names2); // Π²Ρ‚ΠΎΡ€ΠΎΠΉ тСкст для Π²Ρ‚ΠΎΡ€ΠΎΠ³ΠΎ Π°ΠΊΡ‚ΠΈΠ²ΠΈΡ‚ΠΈ final String[] catnames3 = getResources().getStringArray(R.array.cat_names3); // ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠ΅ΠΌ Π°Π΄Π°ΠΏΡ‚Π΅Ρ€ Π΄Π°Π½Π½Ρ‹Ρ… final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.item, catnames); listView.setAdapter(adapter); // массив количСство элСмСнтов final int[] mice = new int [catnames.length]; listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View itemClicked, int position,long id) { TextView textView = (TextView) itemClicked; String strText = textView.getText().toString(); // ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ тСкст Π½Π°ΠΆΠ°Ρ‚ΠΎΠ³ΠΎ элСмСнта Toast.makeText(getApplicationContext(), ((TextView) itemClicked).getText(), Toast.LENGTH_SHORT).show(); for(int x=0;x<catnames.length;x++){ mice[x]=x; // заполняСм массив System.out.println(mice[x]); // Π²Ρ‹Π²ΠΎΠ΄ΠΈΠΌ массив Π² Ρ†ΠΈΠΊΠ»Π΅ if (strText == catnames[x]) { // ЗапускаСм Π°ΠΊΡ‚ΠΈΠ²Π½ΠΎΡΡ‚ΡŒ, ΡΠ²ΡΠ·Π°Π½Π½ΡƒΡŽ с ΠΎΠΏΡ€Π΅Π΄Π΅Π»Π΅Π½Π½Ρ‹ΠΌ ΠΈΠΌΠ΅Π½Π΅ΠΌ ΠΊΠΎΡ‚Π° Intent intent = new Intent(MainActivity.this, LastActivity.class); // для ΠΎΡ‚ΠΏΡ€Π°Π²ΠΊΠΈ Π² Π΄Ρ€ΡƒΠ³ΠΎΠΉ класс. ΠšΠ»ΡŽΡ‡ ΠΈ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅(Ρ‚ΠΎ Ρ‡Ρ‚ΠΎ Π² ΠΏΠΎΠ»Π΅ editText) intent.putExtra("name1", catnames[x]); intent.putExtra("name2", catnames2[x]); intent.putExtra("name3", catnames3[x]); startActivity(intent); } } } }); } } 

Second activation for the full text of LastActivity with the FloatingActionButton button for adding to favorites

 public class LastActivity extends AppCompatActivity { TextView textView; TextView textView2; TextView textView3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_last); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); // Π½Π°Ρ…ΠΎΠ΄ΠΈΠΌ Π² ΠΊΠΎΠ΄Π΅ textView = (TextView) findViewById(R.id.textView); textView2 = (TextView) findViewById(R.id.textView2); textView3 = (TextView) findViewById(R.id.textView3); // ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ Intent ΠΈΠ·Π²Π»ΠΈΠΊΠ°Π΅ΠΌ ΠΈΠ· Π½Π΅Π³ΠΎ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ Intent intent = getIntent(); // ΠΈΠ·Π²Π»Π΅ΠΊΠ°Π΅ΠΌ ΠΈΠ· Π½Π΅Π³ΠΎ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ String name1 = intent.getStringExtra("name1"); String name2 = intent.getStringExtra("name2"); String name3 = intent.getStringExtra("name3"); // Π²Ρ‹Π²ΠΎΠ΄ΠΈΠΌ ΠΏΠΎΠ»ΡƒΡ‡Π΅Π½Π½Ρ‹Π΅ Π΄Π°Π½Π½Ρ‹Π΅ textView.setText(name1); textView2.setText(name2); textView3.setText(name3); // ΠΊΠ½ΠΎΠΏΠΊΠ° Π΄ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ Π² ΠΈΠ·Π±Ρ€Π°Π½Π½ΠΎΠ΅ FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Π”ΠΎΠ±Π°Π²Π»Π΅Π½Π° Π² ΠΈΠ·Π±Ρ€Π°Π½Π½ΠΎΠ΅", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); } } 

    1 answer 1

    You can in different ways.

    For example, save to -l identifier in sharedPreferences. After that, in the list, check its availability and display the status in the list as required.

    • Do not quite understand. For example, I saved some items in sharedPreferences, and how to get them out ?? Is there any filter in ListView? For example, if you click on a menu item from sharedPreferences, these numbers would be added to the filter, and not the entire ListView would be displayed, but only with these numbers ??? - Artsait
    • @Artsait, in general there is a ListView filter, but it is much easier to create a list of data on a given condition on your own - YuriSPb ♦
    • It still does not reach. Do you mean adding items using ListView.add? Okay, I'll try to ask a new question, maybe it will be more clear that I can't do it. Thanks anyway)) - Artsait