我想在點擊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