2014-03-25 39 views
0

我想在點擊item時保存listview中的文本顏色狀態。目前我有一個custom adapter我正在做所有事情都正確,但只有當用戶在應用程序中,顏色纔會改變。並且只要用戶關閉應用程序。顏色再次設置爲前一個.i.e至default之一。我試圖用getView的位置在arraylist中保存顏色狀態,但這對我來說也不適用。或者我應該使用數據庫?任何想法,即使在用戶關閉應用程序後,如何保存這種顏色狀態。並在getView訪問的開始狀態並相應地設置顏色。這個概念就像我們在email應用中看到的一樣。其中一些郵件被標記爲已讀,而另一些則以不同的顏色和文本樣式未讀。我已經解釋了我的問題,如果有人想要更多的解釋,那麼請告訴我,我會的。Android:保存關閉應用程序後的顏色更改狀態

這是我的適配器代碼:

ArrayList<ReadNotifications> saveState; 

public AdatperReadNotification(Context context , ArrayList<ReadNotifications> save) { 
     this.context = context; 
     this.saveState = save; 
    } 



@Override 
    public View getView(final int position, View view , ViewGroup arg2) { 

     LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     if (view == null) 
     { 
      view = inflater.inflate(R.layout.adapter_readnotificaiton, null); 
     } 

     ReadNotifications details = saveState.get(position); 

     TextView tv = (TextView) view.findViewById(R.id.txtview); 


     if(saveState.get(position).isSelected) 
     { 
        // if was clicked set this color on start of view 
      tv.setTextColor(Color.GRAY); 
     } 

     else{ 
          // Set to Default color 
       tv.setTextColor(Color.rgb(0,255,255)); 
     } 

     date.setText(details.tvText()); 

     tv.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 

       tv.setTextColor(Color.GRAY); 
       Intent intent = new Intent(context,ReadNotif.class); 
       v.getContext().startActivity(intent); 
       saveState.get(position).isSelected = true; 
      } 
     }); 


     return view; 
    } 

這是我ReadNotifications類:

public class ReadNotifications implements Serializable 
{ 

    public boolean isSelected; 
    ContentValues colmnValues; 


    public ReadNotifications(ContentValues values ) 
    { 
     colmnValues = values; 
    } 



    public String tvText() { 
     return getValue(colmnValues.get("tvText")); 
    } 


    private String getValue(Object obj){ 
     if(obj == null){ 
      return ""; 
     } 
     return (String) obj; 
    } 


} 
+0

使用SharedPreference對於 – NarendraJi

回答

0

只是一個簡單的技巧給你: - 在您的應用程序

使用sharedpreferences。

您可以保存SharedPreferences中的顏色哈希值或任何字符串或任何對象。

如果您需要保存項目的狀態,那麼SharedPreferences將是一個非常好的選擇。

由於

+0

要在sharedpreferences添加索引: - SharedPreferences SP = getSharedPreferences( 「your_prefs」,Activity.MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); editor.putInt(「your_int_key」,yourIntValue); editor.commit(); 你可以得到它: SharedPreferences sp = getSharedPreferences(「your_prefs」,Activity.MODE_PRIVATE); int myIntValue = sp.getInt(「your_int_key」,-1); –

+0

任何想法如何保存共享pref中特定位置的狀態?這是我目前所做的。編輯器e = mSharedPreferences.edit(); e.putBoolean(「true」,true); e.commit();並返回布爾狀態= mSharedPreferences.getBoolean(「true」,true); if(state){date.setTextColor(Color.GRAY); } else {date.setTextColor(Color.rgb(0,255,255)); –

+0

lv.setOnItemClickListener(新OnItemClickListener(){ 公共無效onItemClick(適配器視圖 myAdapter,查看MyView的,INT myItemInt,長mylng){ 字符串selectedFromList =(字符串)(lv.getItemAtPosition(myItemInt)); } } );您將在「mylng」標識符中獲得項目標識。 –

相關問題