在我的ListView
中,我有一行TextView
和Imageview
。 Listview
行数据来自本地数据库。现在,当用户触摸任何行进行选择时,我想更改该行项目图像。现在再次当用户关闭此应用并再次打开时,我想将新图像设置为上次选择的该行的imageview
。
当用户选择任何listview
行时,也会调用第二个活动。当用户按下该新活动的后退按钮时,所选行的imageview
不会更改,仅显示原始图像。我使用了下面的代码,但效果并不理想。
这是activity的onstart方法:
@Override
public void onStart() {
super.onStart();
int pos = prefs.getInt(PrefernceData.PREF_CURR_SELECTED_ID, -1);
for(int i = 0; i < text.length; i++){
if(Integer.parseInt(text[i][0]) == pos){
dotPosition = i;
}
}
listview itemclick方法:
public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
dotPosition = position;
clearSelection();
lastSelectedView = (ImageView)view.findViewById(R.id.doticon);
ImageView v = (ImageView)view.findViewById(R.id.doticon);
v.setImageResource(R.drawable.dothighlight);
prefs.edit().putInt(PrefernceData.PREF_CURR_SELECTED_ID, Integer.parseInt(text[position][0]).commit();
Intent i = new Intent(this,SecondActivity.class);
startActivity(i);
这是clearselection方法:
public void clearSelection() {
if(lastSelectedView != null) {
Log.i("myLog","clear selection:lastselected icon::");
lastSelectedView.setImageResource(R.drawable.dot);
}
}
这是适配器的getview方法:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater mInflater = LayoutInflater.from(myActivity.this);
if(convertView == null) {
convertView = mInflater.inflate(R.layout.site_listview_items, null);
holder = new ViewHolder();
holder.siteText = (TextView)convertView.findViewById(R.id.homesitelisttext);
holder.dotIcon = (ImageView)convertView.findViewById(R.id.doticon);
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
holder.siteText.setText(text[position][1]);
Log.i("myLog","position::"+position+"dotPosition:"+dotPosition);
if(position == dotPosition) {
holder.dotIcon.setImageResource(R.drawable.dothighlight);
lastSelectedView = holder.dotIcon;
} else {
holder.dotIcon.setImageResource(R.drawable.dot);
}
return convertView;
}