4

在我的ListView, 我有TextViewImageview排。Listview行数据来自本地数据库。现在,当用户触摸任何行进行选择时,我想更改该行项目图像。现在再次当用户关闭此应用程序并再次打开时,我想为imageview上次选择的那一行设置新图像。

当用户选择listview. 并且当用户从该新活动中按下后退按钮时,所选行imageview不会更改,仅显示原始图像。我使用了下面的代码,但它不能完美地工作。

这是活动的 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;
}
4

0 回答 0