2014-03-25 38 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”标识符中获得项目标识。 –

相关问题