通过sharedpreferences在列表视图中保存.class名称

时间:2014-01-28 20:54:37

标签: java android eclipse listview android-listview

嗨我正在制作一个字典应用程序,我希望当我从列表视图中打开一个单词,然后在单词页面(word.class)(其中解释单词含义)时,我想将该单词添加到listview形状中的favorite.class活动,稍后我可以从列表视图中检索该单词。

我想在一个java类中点击收藏夹按钮并在favorite.class的anathor活动中保存该单词(该词的类名称)。最喜欢的按钮实际上是操作栏上可见的菜单项。请解释我所有的代码以及如何做到这一点的方式..请给出一些代码以便完成它。

public class Atherosclerosis extends Activity {

    // declare variables for the table of content and paragraph heading here//

    ScrollView scrollView;

     TextView sign_atherosclerosis;
     TextView sign_id;

     TextView def_id;
     TextView def_atherosclerosis;

     TextView riskfac_id;
     TextView riskfac_atherosclerosis;

     TextView pathophy_id;
     TextView pathophy_atherosclerosis;

     TextView Dx_id;
     TextView Dx_atherosclerosis;

     TextView Rx_id;
     TextView Rx_atherosclerosis;

     TextView prevent_id;
     TextView prevent_atherosclerosis;

     TextView compl_id;
     TextView compl_atherosclerosis;

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.atherosclerosis);



        //here relate the variable for clickonlistener activity and direct scrolldown activity //

        final ScrollView scrollView=(ScrollView)findViewById(R.id.scrollatherosclerosis);

        TextView sign_id=(TextView)findViewById(R.id.Signandsymptomps_id);
        final TextView sign_atherosclerosis=(TextView)findViewById(R.id.Signandsymptoms_atherosclerosis);


        TextView def_id=(TextView)findViewById(R.id.definition_id);
        final TextView def_atherosclerosis=(TextView)findViewById(R.id.definition_atherosclerosis);

        TextView riskfac_id=(TextView)findViewById(R.id.riskfactor_id);
        final TextView riskfac_atherosclerosis=(TextView)findViewById(R.id.riskfactor_atherosclerosis);

        TextView pathophy_id=(TextView)findViewById(R.id.pathophysiology_id);
        final TextView pathophy_atherosclerosis=(TextView)findViewById(R.id.pathophysiology_atherosclerosis);

        TextView Dx_id=(TextView)findViewById(R.id.Diagnosis_id);
        final TextView Dx_atherosclerosis=(TextView)findViewById(R.id.diagnosis_atherosclerosis);

        TextView Rx_id=(TextView)findViewById(R.id.treatment_id);
        final TextView Rx_atherosclerosis=(TextView)findViewById(R.id.treatment_atherosclerosis);

        TextView prevent_id=(TextView)findViewById(R.id.prevention_id);
        final TextView prevent_atherosclerosis=(TextView)findViewById(R.id.prevention_atherosclerosis);

        TextView compl_id=(TextView)findViewById(R.id.complication_id);
        final TextView compl_atherosclerosis=(TextView)findViewById(R.id.complication_atherosclerosis);

        // this code is used for the action bar color change//
         ActionBar bar = getActionBar();
            bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#6B8E23")));
            getActionBar().setDisplayHomeAsUpEnabled(true);
            getActionBar().setHomeButtonEnabled(true); 




// this is the code for jumping from the table of content to the paragraph//
            sign_id.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    scrollView.post(
                            new Runnable() {
                                @Override
                                public void run() {
                                    new CountDownTimer(300, 20) {
                                        @Override
                                        public void onTick(long millisUntilFinished) {
                                            scrollView.scrollTo(0, (int) (sign_atherosclerosis.getBottom()-millisUntilFinished));
                                        }

                                       @Override
                                       public void onFinish() {                 
                                       }
                                    }.start();
                                }
                            }
                        );
                }
            });

            def_id.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    scrollView.post(
                            new Runnable() {
                                @Override
                                public void run() {
                                    new CountDownTimer(300, 20) {
                                        @Override
                                        public void onTick(long millisUntilFinished) {
                                            scrollView.scrollTo(0, (int) (def_atherosclerosis.getBottom()-millisUntilFinished));
                                        }

                                       @Override
                                       public void onFinish() {                 
                                       }
                                    }.start();
                                }
                            }
                        );
                }
            });

            riskfac_id.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    scrollView.post(
                            new Runnable() {
                                @Override
                                public void run() {
                                    new CountDownTimer(300, 20) {
                                        @Override
                                        public void onTick(long millisUntilFinished) {
                                            scrollView.scrollTo(0, (int) (riskfac_atherosclerosis.getBottom()-millisUntilFinished));
                                        }

                                       @Override
                                       public void onFinish() {                 
                                       }
                                    }.start();
                                }
                            }
                        );
                }
            });

            pathophy_id.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    scrollView.post(
                            new Runnable() {
                                @Override
                                public void run() {
                                    new CountDownTimer(300, 20) {
                                        @Override
                                        public void onTick(long millisUntilFinished) {
                                            scrollView.scrollTo(0, (int) (pathophy_atherosclerosis.getBottom()-millisUntilFinished));
                                        }

                                       @Override
                                       public void onFinish() {                 
                                       }
                                    }.start();
                                }
                            }
                        );
                }
            });

            Dx_id.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    scrollView.post(
                            new Runnable() {
                                @Override
                                public void run() {
                                    new CountDownTimer(300, 20) {
                                        @Override
                                        public void onTick(long millisUntilFinished) {
                                            scrollView.scrollTo(0, (int) (Dx_atherosclerosis.getBottom()-millisUntilFinished));
                                        }

                                       @Override
                                       public void onFinish() {                 
                                       }
                                    }.start();
                                }
                            }
                        );
                }
            });

            Rx_id.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    scrollView.post(
                            new Runnable() {
                                @Override
                                public void run() {
                                    new CountDownTimer(300, 20) {
                                        @Override
                                        public void onTick(long millisUntilFinished) {
                                            scrollView.scrollTo(0, (int) (Rx_atherosclerosis.getBottom()-millisUntilFinished));
                                        }

                                       @Override
                                       public void onFinish() {                 
                                       }
                                    }.start();
                                }
                            }
                        );
                }
            });

            prevent_id.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    scrollView.post(
                            new Runnable() {
                                @Override
                                public void run() {
                                    new CountDownTimer(300, 20) {
                                        @Override
                                        public void onTick(long millisUntilFinished) {
                                            scrollView.scrollTo(0, (int) (prevent_atherosclerosis.getBottom()-millisUntilFinished));
                                        }

                                       @Override
                                       public void onFinish() {                 
                                       }
                                    }.start();
                                }
                            }
                        );
                }
            });

            compl_id.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    scrollView.post(
                            new Runnable() {
                                @Override
                                public void run() {
                                    new CountDownTimer(300, 20) {
                                        @Override
                                        public void onTick(long millisUntilFinished) {
                                            scrollView.scrollTo(0, (int) (compl_atherosclerosis.getBottom()-millisUntilFinished));
                                        }

                                       @Override
                                       public void onFinish() {                 
                                       }
                                    }.start();
                                }
                            }
                        );
                }
            });
    }

    // this is for the options selected from the menu button of mobile//
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.atherosclerosis, menu);
        return true;
    }

    // for starting activity from the option or menu//
      @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Take appropriate action for each action item click
            switch (item.getItemId()) {
            case R.id.id_search:
                Intent newActivity0 = new Intent(this,Search.class);     
                 startActivity(newActivity0);
                return true;
            case R.id.id_favorit:
                SharedPreferences sp = this.getSharedPreferences("bookmarks", MODE_PRIVATE);
                Editor editor = sp.edit();
                editor.putString("favorite", "com.kmcpesh.shortreviewofcardiology.Favorite");
                editor.commit();


               return true;

            default:
                return super.onOptionsItemSelected(item);
            }
        }
}

这是我最喜欢的.class活动

public class Favorite extends Activity {

    private TextView mEmptyText;
    private LinearLayout mBookmarkLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.favorite);

        mEmptyText = (TextView) findViewById(R.id.empty_textview);
        mBookmarkLayout = (LinearLayout) findViewById(R.id.bookmark_insert_point);

        getAllKeys();
    }
     private void getAllKeys()
        {
            SharedPreferences sp = this.getSharedPreferences("bookmarks", MODE_PRIVATE);
            Map<String,?> keys = sp.getAll();

            int count = 0;
            for(Map.Entry<String,?> entry : keys.entrySet())
            {
                String value = entry.getValue().toString();
                System.out.println("!!!!!!!!!!!!!!!!!value = "+value);
                String delimiter = ",";
                String[] values_array = value.split(delimiter);
                addBookmark(values_array);
                count++; //keep track of the number of bookmarks
            }

            //if there are no bookmarks, display a text view saying so.  Otherwise, make the text view go away
            if (count == 0)
            {
                mEmptyText.setVisibility(View.VISIBLE);
                mEmptyText.setText(getString(R.string.no_bookmark));
            }
            else
                mEmptyText.setVisibility(View.GONE);

        }

        @SuppressWarnings("deprecation")
        private void addBookmark(String[] values_array)
        {       
            LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View v = vi.inflate(R.layout.favorite, null);

            TextView text = (TextView) v.findViewById(R.id.bookmark_text);


            text.setText(values_array[1]);

            // insert into main view
            mBookmarkLayout.addView(v, 0, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
            System.out.println("!!!!!!!!!!!!!!!!!!!!Just added a view");
        }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.favorite, menu);
        return true;
    }

}

1 个答案:

答案 0 :(得分:0)

Android中的SharedPreferences通常用于存储键值对,而不是您描述的列表。要将值存储到SharedPreferences中,您可以执行以下操作:

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
Editor ed = sp.edit();
ed.putString("myKey", "myValue");

然后从SharedPreferences中检索字符串键,你会这样做:

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
String value = sp.getString("myKey", "");

你可以在这里存储以逗号分隔的喜欢单词列表,然后解析列表:

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
Editor ed = sp.edit();
ed.putString("favorites", "dog,cat,bird,fish");

然后检索它们:

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
String favorites = sp.getString("myKey", "");
String[] words = values.split(",");
System.out.println(Arrays.toString(words));

然后你有一个包含所有喜欢的单词的数组。

[dog, cat, bird, fish]