android - 添加到新 Activity 的收藏夹

标签 android favorites

我正在开发一个报价应用程序..它在卡片 View (回收者 View )中包含名言和人名..每个卡片 View 都包含一个复选框..我想做的是..当用户点击特定报价卡的复选框..我需要显示 toast ..保存到收藏夹..并将复选框背景更改为另一张图片(newimg)..当用户再次点击要显示 toast 的复选框。作为已删除的收藏夹 ...并且复选框背景图像应该是默认值。。因此,如何在单独的 Activity 中显示用户标记的所有收藏夹引号..我是android的新手..我没有找到任何引用资料用于我的目的。 .

MainActivity.java

public class MainActivity extends AppCompatActivity {

    //recyclerview objects
    private RecyclerView recyclerView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String[] AuthorNames = new String[]{"Navagio Beach", "Anse Source d'Argent Beach", "As Catedrais Beach",
                "La Concha Beach", "Bondi Beach", "Nissi Beach"};

        String[] QuotesGuide = new String[]{"https://www.tripadvisor.com.my/Attraction_Review-g7777607-" +
                "d671779-Reviews-Navagio_Beach_Shipwreck_Beach-Anafonitria_Zakynthos_Ionian_Islands.html",
                "https://www.tripadvisor.com.my/Attraction_Review-g477968-d637885-Reviews-Anse_Source_D_Argent" +
                        "-La_Digue_Island.html",
                "https://www.tripadvisor.com.my/Attraction_Review-g609028-d1547522-Reviews-As_Catedrais_Beach-Ribadeo_" +
                        "Province_of_Lugo_Galicia.html",
                "https://www.tripadvisor.com.my/Attraction_Review-g187457-d675885-Reviews-La_Concha_Beach-San_Sebastian" +
                        "_Donostia_Province_of_Guipuzcoa_Basque_Country.html",
                "https://www.tripadvisor.com.my/Attraction_Review-g255060-d257354-Reviews-Bondi_Beach-Sydney_" +
                        "New_South_Wales.html",
                "https://www.tripadvisor.com.my/Attraction_Review-g262055-d1519581-Reviews-Nissi_Beach-Ayia_" +
                        "Napa_Famagusta_District.html"};

        RecyclerView myrv = findViewById(R.id.recyclerView);
        MyRecycleViewAdapter myAdapter = new MyRecycleViewAdapter( AuthorNames , QuotesGuide , MainActivity.this);
        myrv.setLayoutManager(new LinearLayoutManager(this));
        myrv.setAdapter(myAdapter);

        }}

MyQuote.java

public class MyQuote {
private String author;
private String quotedesc;
private int isLiked = 0;
//constructor initializing values
public MyQuote(String author, String quotedesc) {
    this.quotedesc = quotedesc;
    this.author = author;
}
//getters
public String getAuthor() {
    return author;
}
public int getIsLiked(){return isLiked;}
public String getQuotedesc() {
    return quotedesc;
}

public void setIsLiked(int isLiked) {
    this.isLiked = isLiked;
}

MyReclerViewadapter.java

public class MyRecycleViewAdapter extends RecyclerView.Adapter<MyRecycleViewAdapter.ViewHolder>{
private MyQuote myQuote;
    private String[] AuthorNames;
    private String[] QuotesGuide;
    private Context mCtx;

    public MyRecycleViewAdapter(String[] authorNames, String[] quotesGuide, Context mCtx) {
        AuthorNames = authorNames;
        QuotesGuide = quotesGuide;
        this.mCtx = mCtx;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.sample_quotecards, parent, false);


        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(final MyRecycleViewAdapter.ViewHolder myholder, final int position) {
        myholder.tv_author.setText(AuthorNames[position]);
        myholder.tv_quote.setText(QuotesGuide[position]);

            myholder.im_favlike.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    //Show "Saved to favourite" toast
                    Toast.makeText(mCtx, " quote saved to favorites",
                            Toast.LENGTH_LONG).show();
                } else {
                    //Show "Removed from favourite"

                    Toast.makeText(mCtx, " quote removed from favorites",
                            Toast.LENGTH_LONG).show();
                }


            }


        });


        // share button of a recycler cardview
        myholder.buttonViewOption.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.putExtra(Intent.EXTRA_TEXT, "share this quote"
                        + AuthorNames[myholder.getAdapterPosition()] +
                        "\nHere is the link to the full review: " + QuotesGuide[myholder.
                        getAdapterPosition()]);
                intent.setType("text/plain");
                mCtx.startActivity(Intent.createChooser(intent, "share this quote"));


            }
        });
    }


    @Override
    public int getItemCount() {
        return AuthorNames.length;
    }


    public class ViewHolder extends RecyclerView.ViewHolder {

        public TextView tv_author;
        public  CheckBox im_favlike;
        public TextView tv_quote;
        public ImageButton buttonViewOption;

        public ViewHolder(View itemView) {
            super(itemView);

            im_favlike = itemView.findViewById(R.id.likeimg);
            tv_author= itemView.findViewById(R.id.author_title);
            tv_quote= itemView.findViewById(R.id.quote_text);
            buttonViewOption = itemView.findViewById(R.id.imageViewOptions);
        }
    }
}

what i want to do is :

  1. whenever i click the (chekbox)favorite..the checkbox image is changing..and on back click it comes to default(unchecked)..and it works fine.. but the problem..is i dont understand how to save these checkbox values...when the user exits the app and open the app again i need to maintain the favorites..checkbox values..

  2. how to store all the favorites(quotes cards)... marked(checked) by the user in a separate activity..

i am new to android.. i dont know about shared preferences..can anyone explain in detail step wise ..what should i do.. it helps me lot..

enter image description here

最佳答案

检查这个示例项目

https://github.com/saini2sandeep/Favourite.git

用于显示 toast“已保存到收藏夹”和“已从收藏夹中删除” 你可以这样做:

//假设 likeButtonCB 是您的复选框,您必须在其上设置一个监听器,如下面的代码所示:

 likeButtonCB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (isChecked) {
                           //Show "Saved to favourite" toast
                        } else {
                           //Show "Removed from favourite" toast
                        }
            }
        });

现在要更改点赞按钮的图像,您必须制作一个像这样的可绘制文件: 你可以根据你的名字命名它,我将它命名为“like_button_background” 这里“ic_like_heart_button_color”是可绘制的喜欢的按钮图像,“ic_like_heart_button_empty”是不同的图像。

  <?xml version="1.0" encoding="utf-8"?>
   <selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:drawable="@drawable/ic_like_heart_button_color" 
   android:state_checked="true" />
   <item android:drawable="@drawable/ic_like_heart_button_empty" />

在 xml 代码中将此文件添加到您的复选框背景中,如下所示:

<CheckBox
        android:id="@+id/like_button_cb"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_marginStart="@dimen/margin_left_gen_16"
        android:background="@drawable/like_button_background"
        android:button="@null"
        android:gravity="center"
        android:padding="@dimen/padding_gen_4"
        android:textSize="@dimen/tv_gen_16"
        android:theme="@style/checkBoxStyle"
        android:visibility="visible" />

这将解决您的前两个问题。 要为单个卡片保存点赞,您必须在模型类“int isLiked = 0;”中再维护一个字段。在模型类中,根据它,您可以在填充 UI 时更新点赞按钮的状态。

你可以这样做: 例如,这里的故事是您的模型,然后在填充卡片数据时在您的适配器中执行此代码。

 if (story.getIsLiked() == 1) {
            likeButtonCB.setChecked(true);
        } else {
            likeButtonCB.setChecked(false);
        }

关于android - 添加到新 Activity 的收藏夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52928491/

相关文章:

android - PropertyChangedEventHandler 导致内存泄漏

android - 单击 ImageView 时的语音气泡类型选项 View ?

java - 检查字符串是否为空文本或数字的方法不起作用

java - 拦截后退按钮,如果按下一次则返回上一页,如果按下两次则退出

ios - 实现收藏按钮 Swift

android - 自定义 android map 位置?

java - 如何使用 android studio 创建两个 android 按钮

c# - 由于域文件夹重定向,在 Environment.SpecialFolder.Favorites 上调用 Directory.GetDirectories 时出现 DirectoryNotFoundException

javascript - Wordpress 收藏夹插件 - 从 cookie 中读取一系列帖子

ios - 使用indexPath.row保存收藏夹按钮状态