添加到收藏夹到新 activity

add to favorites to a new activity

我正在开发一个报价应用程序..它在卡片视图(回收者视图)中包含名言和人名..每个卡片视图都包含一个复选框..我想做的是..当用户单击特定报价卡的复选框..我需要显示祝酒词..保存到收藏夹..并将复选框背景更改为另一张图片(newimg)。复选框背景图像应为 default.。因此如何显示用户在单独activity中标记的所有收藏夹引号。我是[=51=的新手] ..我没有找到任何参考文献..

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..

查看此示例项目

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

用于吐司"Saved to favourite"和"Removed from favourites" 你可以这样做:

// 假设 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" />

这将解决您的前两个问题。 要为单个卡片保存点赞,您必须在模型 class "int isLiked = 0;" 中再维护一个字段 class 并且根据该字段,您可以在填充 [= 时更新点赞按钮的状态=39=].

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

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

据我了解,如果选中 favorite,您想将数据保存在共享首选项中。为此,您应该了解 SharedPrefernces。

按照此 link 在共享首选项中保存数据。 Android Shared preferences example

现在,根据您的要求,您想要保存模型 class 的 ListMyQuote 列表(如果已选中收藏夹按钮

按照此操作在 SharedPreferences 中保存列表。 Save ArrayList to SharedPreferences

现在回到问题上来,我们需要从共享首选项中保存和获取列表。 为方便起见,我将创建三种方法,一种用于启动首选项中的 MyQuote 列表,第二种用于获取已保存的 MyQuote 列表,最后一种用于更新保存的列表 MyQuote

Gson gson = new Gson();


// create an empty list of MyQuote
public void initializeMyQuoteList(){
List<MyQuote> quoteList = new ArrayList<MyQuote>();
String jsonText = gson.toJson(quoteList);
prefsEditor.putString("MYQUOTE_LIST", jsonText);
prefsEditor.apply();
}


//getting quote list
public List<MyQuote> getQuoteList(){
 Type type = new TypeToken<List<MyQuote>>() {
    }.getType();
    return gson.fromJson(preferenceManager.getString("MYQUOTE_LIST",
            null), type);
}


//updating saved quote list
public void updateQuoteList(MyQuote quote){
 List<MyQuote> quoteList = getQuoteList();
quoteList.add(quote);
 String jsonText = gson.toJson(quoteList);
 prefsEditor.putString("MYQUOTE_LIST", jsonText);
 prefsEditor.apply();
}

创建 activity 后,只要 favorite 调用 initializeMyQuoteList() 就可以初始化空列表单击按钮通过将 MyQuote 添加到保存的列表来更新列表,即通过调用 updateQuoteList(quote),这里传递 MyQuote object单击项目时。

现在,我们有所有已保存的报价单,只需调用新 activity 中的 getQuoteList() 即可获取已保存的列表 MyQuote列表.