2014-01-28 43 views
-2

您好我正在製作一個字典應用程序,我希望當我從一個列表視圖中打開一個單詞,然後在單詞頁面(word.class)(這裏的單詞含義解釋) ,我想將這個單詞添加到listview形狀的favorite.class活動中,稍後我可以從列表視圖中檢索該單詞。通過sharedpreferences保存在列表視圖中的.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); 
      } 
     } 
} 

這是我favorite.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; 
    } 

} 
+0

您需要對java進行自己的研究。試着弄清楚自己,然後在這裏問一個具體的問題。 – cbrulak

+0

你使用數據庫?你可以發佈一些代碼嗎? –

+0

不,我不使用數據庫...我希望它通過sharedpreferences存儲 – user2592807

回答

0

SharedPreferences在Android是通常用於存儲鍵值對,沒有列表,如你所描述的一個。要存儲一個值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] 
+0

在哪裏我將這些保存到Favorite.class,以便我可以在以後看到它? – user2592807

+0

我不確定我是否理解這個問題。但我認爲你想要做的是,當你最喜歡的活動啓動時,閱讀你寫的SharedPreferences,並將它們呈現在用戶的菜單中。 – AWT

相關問題