Android系统。更新ExpandableListView

时间:2010-05-04 13:09:19

标签: android listview

我有以下问题:

我想在点击时更改ExpandableListView的单元格的值。我该怎么做?

以下是我正在使用的内容:

    public class TemplateActivity extends ExpandableListActivity {

     private SimpleExpandableListAdapter expListAdapter = null;

     protected void onCreate(Bundle savedInstanceState) {
  setContentView(R.layout.template_create);
     expListAdapter = new SimpleExpandableListAdapter(
    this, createGroupList(), 
    R.layout.template_create_group_row, 
    new String[] { KEY_GROUP }, 
    new int[] { R.id.create_template_groupname }, 
    createChildList(), 
    R.layout.flower_template_create_child_row, 
    new String[] { KEY_ATTRIBUTE_VALUE }, 
    new int[] { R.id.flowerx_text_attr }
  );

  setListAdapter(expListAdapter);

     }

 private List<HashMap<String, String>> createGroupList() {
  ArrayList<HashMap<String, String>> result = new ArrayList<HashMap<String, String>>();

  for (String entryKey: <ArrayOfGroupTitles>) {
   HashMap<String, String> m = new HashMap<String, String>();
   m.put(KEY_GROUP, entryKey);
   result.add(m);
  }

  return result;
 }

 private List<ArrayList<HashMap<String, Object>>> createChildList() {
  ArrayList<ArrayList<HashMap<String, Object>>> result = new ArrayList<ArrayList<HashMap<String, Object>>>();
  for (String entryKey: fieldsInList.keySet()) {
   ArrayList<HashMap<String, Object>> secList = new ArrayList<HashMap<String, Object>>();

   HashMap<String, Object> attrVal = new HashMap<String, Object>();
   attrVal.put(KEY_ATTRIBUTE_VALUE, String.valueOf(fieldsInList.get(entryKey).getFieldValue()));
   secList.add(attrVal);
   result.add(secList);
  }

  return result;
 }

应用程序正处于进行阶段,因此attrVal将包含多个属性(可以放置或不取决于某些逻辑),因此我使用这种类型的适配器和填充单元格数据的方式。

fieldsInList - LinkedHashMap类型的外部变量。

我有一个函数可以改变其中一个成员的fieldsInList变量。 我应该如何通知我的expListAdapter或ExpanableListView被刷新?

2 个答案:

答案 0 :(得分:0)

expListAdapter.notifyDataSetChanged(); - 通知适配器数据已更改:

对于onclick:

OnChildClickListener myOnChildClick = new OnChildClickListener() {

        @Override
        public boolean onChildClick(ExpandableListView arg0, View arg1, int arg2, int arg3,
                long arg4) {
            // TODO Auto-generated method stub
            return false;
        }

    };

和setOnChildClickListener(myOnChildClick);在onCreate

答案 1 :(得分:0)

要更新expandableListView的单元格,您应该在创建适配器时使用List<ArrayList<HashMap<Object, Object>>>变量。它第一次对我不起作用,因为我使用内部方法,它创建了列表。也许这个事实是问题的原因。正如Alex所说,您需要调用adapter.notifyDataSetChanged()来将更改应用于ExpandableList。感谢