2014-01-13 96 views
2

我正在關注ExpandableListViews上的this tutorial。我想讓我的實現在點擊一個按鈕後顯示一個新的值。但是,沒有任何我嘗試過的作品。除了使用notifyDataSetChanged()之外,我還嘗試了notifyDataSetInvalidated()並實例化一個新的適配器,這在類似的帖子中被建議。未顯示的新數據在我的onClick方法下。notifyDataSetChanged()無法更新適配器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; 
    } 
} 

回答

1

此方法不爲我工作也(我不知道爲什麼我的猜測是,它只能與Contnet提供商合作)。您可以使用notifyDataSetChanged()而不是使用notifyDataSetChanged()重新添加新數據到您的groups並重置adapter

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

這工作 - 謝謝你! –

+1

...這是非常低效的......你每次都創建一個新的適配器。 –

1

你的if語句可能永遠也稱爲:

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

應該

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

編輯:

==檢查對象標識,使用字符串時。你最可能的意思是相等的。另外我建議你熟悉調試器和/或LogCat/Log.d(),這樣可以爲你節省大量的麻煩;

+0

我測試過它,它工作正常,所以我不認爲就是這樣。 。 –

1

您可以嘗試重新設置列表適配器:

listView.setAdapter(adapter); 
1

嘗試與周圍的runOnUIThread notifysetchange(新的Runnable(...))。由於您使用listView進行了更改,因此應該在UI線程中更新它。如果您不熟悉斷點,也是一種好方法,請嘗試使用if語句記錄並查看您的條件是否設置爲true。

+1

'getActivity()runOnUiThread(新的Runnable(){ \t \t \t @Override \t \t \t公共無效的run(){ \t \t \t \t adapter.notifyDataSetChanged(); \t \t \t} \t \t}) ;' –

相關問題