繁体   English   中英

notifyDataSetChanged()无法更新适配器onClick

[英]notifyDataSetChanged() fails to update adapter onClick

我正在ExpandableListViews上关注本教程 我希望我的实现显示点击按钮后添加的新值。 但是,我没有尝试过。 除了尝试使用notifyDataSetChanged()之外,我还尝试了notifyDataSetInvalidated()并实例化一个新的适配器,这在类似的文章中有所建议。 未显示的新数据在我的onClick方法下。

关于修复的任何建议吗?

public class ClassesFragment extends Fragment implements OnClickListener {
private View v;

 Integer term;
 String termSelection;

 ListView myList;
 EditText editText1;
 Spinner spinner;

 MyExpandableListAdapter adapter;
 ExpandableListView listView;

 SparseArray<Group> groups = new SparseArray<Group>();

 Group fall2013 = new Group("Fall 2013");
 Group winter2014 = new Group("Winter 2014");
 Group spring2014 = new Group("Spring 2014");
 Group summer2014 = new Group("Summer 2014");


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    v = inflater.inflate(R.layout.fragment_classes, container, false);

    editText1 = (EditText) v.findViewById(R.id.editText1);

    Spinner spinner = (Spinner) v.findViewById(R.id.spinner1);
    spinner.setOnItemSelectedListener(new SpinnerItemSelectedListener());



    Button add = (Button) v.findViewById(R.id.button1);
    add.setOnClickListener(this);

    createData();
    listView = (ExpandableListView) v.findViewById(R.id.expandableListView1);
    adapter = new MyExpandableListAdapter(getActivity(), groups);
    listView.setAdapter(adapter);
    return v;              
}

public void createData() {      
    groups.append(0, fall2013);
    groups.append(1, winter2014);
    groups.append(2, spring2014);
    groups.append(3, summer2014);

}

@SuppressWarnings("boxing")
public void onClick(View v) {
    switch (v.getId()) {           
        case R.id.button1:                  

            Integer crn = Integer.parseInt(editText1.getText().toString());
            editText1.setText("");

            if (termSelection.toString() == "Fall 2013") {                      

                fall2013.children.add(crn.toString());
                adapter.notifyDataSetChanged();
            }

            break;

            case R.id.button2:
                Toast.makeText(getActivity(), "Clicked on delete button", Toast.LENGTH_LONG).show();
                break;  

        }
   }


   public class SpinnerItemSelectedListener implements OnItemSelectedListener {
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
            termSelection = parent.getItemAtPosition(pos).toString();
        }

        public void onNothingSelected(AdapterView parent) {
        }
    }
}

我的自定义适配器:

public class MyExpandableListAdapter extends BaseExpandableListAdapter {

  private final SparseArray<Group> groups;
  public LayoutInflater inflater;
  public Activity activity;

  public MyExpandableListAdapter(Activity act, SparseArray<Group> groups) {
    activity = act;
    this.groups = groups;
    inflater = act.getLayoutInflater();
  }

  @Override
  public Object getChild(int groupPosition, int childPosition) {
    return groups.get(groupPosition).children.get(childPosition);
  }

  @Override
  public long getChildId(int groupPosition, int childPosition) {
    return 0;
  }

  @Override
  public View getChildView(int groupPosition, final int childPosition,
      boolean isLastChild, View convertView, ViewGroup parent) {
    final String children = (String) getChild(groupPosition, childPosition);
    TextView text = null;
    if (convertView == null) {
      convertView = inflater.inflate(R.layout.listrow_details, null);
    }
    text = (TextView) convertView.findViewById(R.id.textView1);
    text.setText(children);
    convertView.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        Toast.makeText(activity, children,
            Toast.LENGTH_SHORT).show();
      }
    });
    return convertView;
  }

  @Override
  public int getChildrenCount(int groupPosition) {
    return groups.get(groupPosition).children.size();
  }

  @Override
  public Object getGroup(int groupPosition) {
    return groups.get(groupPosition);
  }

  @Override
  public int getGroupCount() {
    return groups.size();
  }

  @Override
  public void onGroupCollapsed(int groupPosition) {
    super.onGroupCollapsed(groupPosition);
  }

  @Override
  public void onGroupExpanded(int groupPosition) {
    super.onGroupExpanded(groupPosition);
  }

  @Override
  public long getGroupId(int groupPosition) {
    return 0;
  }

  @Override
  public View getGroupView(int groupPosition, boolean isExpanded,
      View convertView, ViewGroup parent) {
    if (convertView == null) {
      convertView = inflater.inflate(R.layout.listrow_group, null);
    }
    Group group = (Group) getGroup(groupPosition);
    ((CheckedTextView) convertView).setText(group.string);
    ((CheckedTextView) convertView).setChecked(isExpanded);
    return convertView;
  }

  @Override
  public boolean hasStableIds() {
    return false;
  }

  @Override
  public boolean isChildSelectable(int groupPosition, int childPosition) {
    return false;
  }
} 

这种方法对我也不起作用(我不确定为什么。我的猜测是它仅适用于Contnet提供程序)。 代替使用notifyDataSetChanged()您可以仅向groups重新查询/添加新数据并重置adapter

adapter = new MyExpandableListAdapter(getActivity(), groups);
listView.setAdapter(adapter);

您的if语句可能永远不会被调用:

 if (termSelection.toString() == "Fall 2013")

应该

 if (termSelection.toString().equals("Fall 2013"))

编辑:

==使用字符串时检查对象身份。 您很可能是说等于。

另外,我建议您熟悉调试器和/或LogCat / Log.d(),这将为您省去很多麻烦;

您可以再次尝试设置列表适配器:

listView.setAdapter(adapter);

尝试用runOnUIThread(new Runnable(...))来包围您的notifysetchange。 由于您使用listView进行了更改,因此应该在您的UI线程中进行更新。 如果您不熟悉断点,这也是一个好方法,请尝试使用if语句记录日志,并查看您的条件是否设置为true。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM