Android:即使在应用程序关闭后也保存颜色变化的状态
我想在单击Android:即使在应用程序关闭后也保存颜色变化的状态,android,listview,Android,Listview,我想在单击项目时将文本颜色的状态保存在列表视图中。目前,我有一个自定义适配器,在那里我做的所有事情都是正确的,但只有当用户在应用程序中时,颜色才会保持更改。一旦用户关闭应用程序。颜色再次设置为上一个。即设置为defaultone。我尝试使用getView的位置将颜色状态保存在arraylist中,但这对我来说太不起作用了。或者我应该使用db吗?你知道如何在用户关闭应用程序后保存颜色状态吗。在getView开始时,访问该状态并相应地设置颜色。这个概念就像我们在电子邮件应用程序中看到的一样。其中一些
项目时将文本颜色的状态保存在列表视图中。目前,我有一个自定义适配器
,在那里我做的所有事情都是正确的,但只有当用户在应用程序中时,颜色才会保持更改。一旦用户关闭应用程序。颜色再次设置为上一个。即设置为default
one。我尝试使用getView
的位置将颜色状态保存在arraylist
中,但这对我来说太不起作用了。或者我应该使用db吗?你知道如何在用户关闭应用程序后保存颜色状态吗。在getView开始时,访问该状态并相应地设置颜色。这个概念就像我们在电子邮件
应用程序中看到的一样。其中一些邮件被标记为已读,一些邮件被标记为未读,并使用不同的颜色和文本样式。我已经解释了我的问题,如果有人想要更多的解释,请告诉我我会的
这是我的适配器代码:
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;
}
给你一个简单的窍门:-
在应用程序中使用SharedReferences
您可以在SharedReferences中保存颜色哈希值、任何字符串或任何对象
若您需要保存项目的状态,那个么SharedReferences将是一个非常好的选择
感谢使用SharedReference在SharedReferences中添加索引:-SharedReferences sp=getSharedReferences(“您的首选项”,Activity.MODE\u PRIVATE);SharedReferences.Editor=sp.edit();putin(“your_int_key”,yourIntValue);commit();您可以通过以下方式获得它:SharedReferences sp=GetSharedReferences(“您的首选项”,Activity.MODE\u PRIVATE);int myIntValue=sp.getInt(“您的int键”,-1);知道如何在共享pref中保存特定位置的状态吗?这就是我目前所做的。Editor e=mSharedPreferences.edit();e、 putBoolean(“真”,真);e、 提交();并返回boolean state=mSharedPreferences.getBoolean(“true”,true);if(state){date.setTextColor(Color.GRAY);}else{date.setTextColor(Color.rgb(0255255));lv.setOnItemClickListener(new-OnItemClickListener(){public void-onItemClick(AdapterView-myAdapter,View-myView,int-myItemInt,long-myling){String selectedFromList=(String)(lv.getItemAtPosition(myItemInt));});您将在“mylng”标识符中获得项目id。这是我所做的boolean state=mSharedPreferences.getBoolean(“true”,true);int pos=mSharedPreferences.getInt(“position”,-1);if(!state){date.setId(pos);date.setTextColor(Color.GRAY);}
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;
}
}