将位置列表存储在sharedPreferences中,并在另一个活动中进行检索 [英] Storing list of position in sharedPreferences, and retrieve in another activity

本文介绍了将位置列表存储在sharedPreferences中,并在另一个活动中进行检索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做类似,我已将cardview与recyclerview一起使用.我在下面添加了收藏夹按钮cardview,可以看到完整的代码.在recyclerview适配器中,我按位置打印Toast,并且工作正常,现在我需要将位置的int值保存在arraylist的共享首选项"中,并在下一个intent中显示这些arraylist.

I want to do similar like this, I have used cardview with recyclerview. I added favourite button cardview below you can see the full code. In recyclerview adapter I print the Toast as per position and its working fine, now I need to save the int value of position in shared Preferences in arraylist and display those arraylist in next intent.

这是我的recyclerview适配器

This is my recyclerview adapter

public void onBindViewHolder(NameViewHolder holder, final int position) {
    holder.textView.setText(names.get(position).textView);
    holder.favourite.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(view.getContext());
            SharedPreferences.Editor editor = sp.edit();
            editor.putInt("key", position);  //may be or may not be.
            //add code here to save position in array
            Toast.makeText(view.getContext(),"Fav "+position,Toast.LENGTH_SHORT).show();
        }
    });

cardview.xml

cardview.xml

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/cardview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:orientation="vertical"
app:cardBackgroundColor="@android:color/transparent"
app:cardCornerRadius="8dp"
app:cardElevation="3dp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true">

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="20dp"
    android:paddingTop="3dp"
    android:textAlignment="center"
    android:textColor="#ff9901"
    android:textSize="35sp"
    android:textStyle="italic" />

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:id="@+id/favourite"
        android:src="@drawable/star"
        android:background="@android:color/transparent"
        />
</RelativeLayout>
</android.support.v7.widget.CardView>

为我提供从首选项中获取arraylist的方法,以及如何在ListView中使用它.

Provide me getting arraylist from prefrences and how to use in ListView too.

推荐答案

使用此存储

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(view.getContext().MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putInt("key", position);  //may be or may not be.
editor.commit;

,并且在您的其他活动中使用它来检索

and in your other activity use this to retrieve

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(view.getContext().MODE_PRIVATE);
            int position=sp.getInt("key");

您也可以尝试就像您的活动A创建活动B的意图

you may also try this Like in your Activity A Create a Intent of Activity B

Intent intent =new Intent(A.this,B.class);
intent.putExtra("key",position);
startActivity(intent);

您的活动B

Intent intent= getIntent();
int position Integer.parseInt(intent.getExtras().get("key"));

编辑

使用arraylist并将其从一项活动转移到另一项活动

use the arraylist and transfer it from one activity to another

 ArrayList<Integer> mThumbIds=new ArrayList<Integer>() ;
    mThumbIds.add(R.drawable.sample_0);
            mThumbIds.add(R.drawable.sample_1);
            mThumbIds.add(R.drawable.sample_2);
            mThumbIds.add(R.drawable.sample_3);
            mThumbIds.add(R.drawable.sample_4);
            mThumbIds.add(R.drawable.sample_5);
            mThumbIds.add(R.drawable.sample_6);
            mThumbIds.add(R.drawable.sample_7);
 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                ArrayList list=mThumbIds;
                Intent intent= new Intent(A.this,B.class);
                       intent.putExtra("list",mThumbIds);
                intent.putExtra("position",position);
                intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
               //   overridePendingTransition(R.anim.hold,R.anim.slide_in_left);
                    startActivity(intent);




            }
        });

现在在您的Attitude B中使用它

now use this in your ativity B

    Intent intent =getIntent();


        int position=Integer.parseInt(intent.getExtras().get("position").toString());
        ArrayList<Integer> list= (ArrayList<Integer>) intent.getExtras().get("list");

int current_position=list.get(position)

这篇关于将位置列表存储在sharedPreferences中,并在另一个活动中进行检索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆