在运行时使用sharedpreferences在recylerview中存储多个视图

时间:2016-09-28 03:01:32

标签: java android listview import

您正在使用"添加"我的应用的ActionBar按钮点击它ListView开启,用户可以点按任何列表项,将该项添加到作为父活动的RecyclerView(MainActivity持有Recyclerview)但在退出应用程序时,最后添加的项目仍保留在MainActivity的视图中。我猜SharedPreferences'编辑每次都被覆盖。你可以帮忙提供一些片段,因为我无法在listView上点击时多次调用editor.put()方法。我的RecyclerView包含图片和文字。

这是我的 MainActivity代码

package com.example.mohitmehndiratta.customlistadap;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Icon;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;

import static android.support.v7.widget.LinearLayoutManager.*;


public class MainActivity extends AppCompatActivity {
    public static ArrayList<DataSet> alist;
    int i;
    RecyclerView rv;
    static RecycledAdap adap;
    static String args;
    static int rid;
    static SharedPreferences sharedPreferences;

    public static void addnow(String arg)
    {
        args=arg;
        rid=R.drawable.i;
        alist.add(new DataSet(args,rid));
        adap.notifyDataSetChanged();
        SharedPreferences.Editor editor=sharedPreferences.edit();
        editor.putInt("Iconid",rid);
        editor.putString("AppName",args);
        editor.apply();



        if (sharedPreferences!=null)
        {
        int x=sharedPreferences.getInt("Iconid",rid);
            String y=sharedPreferences.getString("AppName",args);
            addnow(y);

        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater inflater=getMenuInflater();
        inflater.inflate(R.menu.new_menu,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if(item.getItemId()==R.id.addbtn)
        {
            Intent intent=new Intent(this,AppList.class);
            startActivity(intent);
        }
        return super.onOptionsItemSelected(item);
    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sharedPreferences = getSharedPreferences("MyPrefs",getApplicationContext().MODE_PRIVATE);


        rv=(RecyclerView)findViewById(R.id.rv);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
        rv.setLayoutManager(mLayoutManager);
        rv.setItemAnimator(new DefaultItemAnimator());
        rv.addItemDecoration(new ItemDecoration(this, LinearLayoutManager.VERTICAL));

        for(i=0;i<DataModel.name.length;i++) {
            alist = new ArrayList<DataSet>();
            alist.add(new DataSet("Paytm",R.drawable.i));
            alist.add(new DataSet("Facebook",R.drawable.i1));
            alist.add(new DataSet("ShareIt",R.drawable.i2));
            alist.add(new DataSet("Instagram",R.drawable.i3));
            alist.add(new DataSet("BookMyShow",R.drawable.i4));
        }

        adap=new RecycledAdap(getApplicationContext(),alist);
        rv.setAdapter(adap);
        MyListener mlistener=new MyListener(getApplicationContext(),rv, new MyListener.ReClickListener() {


            @Override
            public void onClick(int position) {
                Toast.makeText(getApplicationContext(),"Launching item"+position,Toast.LENGTH_SHORT).show();
                LauncherHandler lh=new LauncherHandler(position);
                String pkname=lh.getpack();
                AppDialog adialog=new AppDialog();
                adialog.packagenameset(pkname);
                adialog.show(getFragmentManager(),"AppDialogFrag");

            }

            @Override
            public void onLongClick(int position) {

                LauncherHandler lh=new LauncherHandler(position);
                String pkname=lh.getpack();
                Toast.makeText(getApplicationContext(),"Launching item"+position,Toast.LENGTH_SHORT).show();
                AppDialog adialog=new AppDialog();
                adialog.packagenameset(pkname);
                adialog.show(getFragmentManager(),"AppDialogFrag");
            }
        });
        rv.addOnItemTouchListener(mlistener);

        if (sharedPreferences!=null)
        {
            int x=sharedPreferences.getInt("Iconid",rid);
            String y=sharedPreferences.getString("AppName",args);
            addnow(y);
        }
        else
        {
            Toast.makeText(getApplication(),"There is nothing in app's cache",Toast.LENGTH_SHORT).show();
        }


    }

}

ListView代码:

package com.example.mohitmehndiratta.customlistadap;

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;

public class AppList extends AppCompatActivity {
ListView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_app_list);
        PackageManager pm=getPackageManager();
        ArrayList<ApplicationInfo> li= (ArrayList<ApplicationInfo>) pm.getInstalledApplications(0);
        ArrayList al=new ArrayList<String>();
        String str;
        String strpk;
        ApplicationInfo info;
        for(int i=0;i<li.size();i++)
        {
            info=li.get(i);
            str=info.loadLabel(pm).toString();
            Drawable appicon=info.loadIcon(pm);
            al.add(str);
        }
        lv=(ListView)findViewById(R.id.listView);
        final ArrayAdapter adap=new ArrayAdapter<String>(this,R.layout.support_simple_spinner_dropdown_item,al);
        lv.setAdapter(adap);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String text=lv.getItemAtPosition(i).toString();
                Toast.makeText(getApplicationContext(),text+i,Toast.LENGTH_SHORT).show();
                MainActivity.addnow(text);
            }
        });
    }
}

0 个答案:

没有答案