自定义ListView onClick更改图像,notifyDataSetInvalidated在重新启动活动后无法正常工作

时间:2014-08-21 06:12:35

标签: android android-listview wear-os

我正在创建包含图片和文字的列表视图。当我第一次启动活动时,notifyDataSetInvalidated()工作,onclick后图像确实发生了变化。但是,当我重新启动活动时,notifyDataSetInvalidated()不起作用,图像也不会更改。这些是我写的代码。

listview.activity

@Override
public void onLayoutInflated(WatchViewStub stub) {
     listView = (ListView)stub.findViewById(R.id.setting_item_listview);
     launcherSwitch = (Switch) stub.findViewById(R.id.Launcher_switch);

     adapter = new CustomListItem(WOMSettingActivity.this,
                    WOMSettingActivity.this.getResources().getStringArray(R.array.setting_item),imageId,app);
     listView.setAdapter(adapter);

     listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
               ImageView imageView = (ImageView)view.findViewById(R.id.img);
               imageView.setImageResource(R.drawable.icon_black);
               for(int i = 0; i < imageId.length;i++){
                    if(position == i){
                         checkState[i] = true;
                    }else{
                         checkState[i] = false;
                    }
               }
               adapter.notifyDataSetInvalidated();
          }
     });
}

CustomListItem

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = context.getLayoutInflater();
     final ViewHolder holder;
    if(convertView==null){
        convertView = inflater.inflate(R.layout.settingtextlayout,null);
        holder = new ViewHolder();
        holder.txtTitle = (TextView) convertView.findViewById(R.id.listview_item_text);
        holder.imageView = (ImageView) convertView.findViewById(R.id.img);
        convertView.setTag(holder);
    }else{
        holder = (ViewHolder)convertView.getTag();
    }

    holder.txtTitle.setText(launcherPosition[position]);
    if(checkstate[position]){
        holder.imageView.setImageResource(R.drawable.icon_black);
    }else{
        holder.imageView.setImageResource(R.drawable.ic_launcher);
    }

    return convertView;
}

请告诉我我在哪里做错了。 谢谢

2 个答案:

答案 0 :(得分:0)

尝试拨打

notifyDataSetChanged();    //Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.

而不是

notifyDataSetInvalidated();    //Notifies the attached observers that the underlying data is no longer valid or available.

参考:http://developer.android.com/reference/android/widget/BaseAdapter.html

答案 1 :(得分:0)

创建了另一个类并进行了一些修正。

listview.activity

 public void onLayoutInflated(WatchViewStub stub) {
            listView = (ListView)stub.findViewById(R.id.setting_item_listview);
            launcherSwitch = (Switch) stub.findViewById(R.id.Launcher_switch);

            adapter = new CustomListItem(WOMSettingActivity.this,list,app);
            Log.d(TAG,"adapter : " + adapter.getCount());
            listView.setAdapter(adapter);

            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    for(int i = 0;i<adapter.getCount();i++){
                    if(position == i){ 
                      adapter.getItem(position).setChecked(true);
                     }else{
                      adapter.getItem(position).setChecked(false);
                     }
                    }
                    adapter.notifyDataSetChanged();
                }
            });

        }

public class setting(){
  private String Position;
  private boolean checked;

  public setting(String Position, boolean checked){
    this.Position = Position;
    this.checked = checked;
}
// Getter and setter
}

列表类扩展设置

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = context.getLayoutInflater();
     final ViewHolder holder;
    if(convertView==null){
        convertView = inflater.inflate(R.layout.settingtextlayout,null);
        holder = new ViewHolder();
        holder.txtTitle = (TextView) convertView.findViewById(R.id.listview_item_text);
        holder.imageView = (ImageView) convertView.findViewById(R.id.img);
        convertView.setTag(holder);
    }else{
        holder = (ViewHolder)convertView.getTag();
    }
    holder.txtTitle.setText(getItem(position).getPosition());
    if(getItem(position).isChecked()){
        holder.imageView.setImageResource(R.drawable.icon_red);
    }else{
        holder.imageView.setImageResource(R.drawable.icon_black);
    }
 return convertView;
}

notifyDataSetChanged()现在运行正常。