我想在点击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;
}
}
使用SharedPreference对于 – NarendraJi