【问题标题】:refresh listview when using hashmap使用 hashmap 时刷新列表视图
【发布时间】:2013-01-15 19:33:33
【问题描述】:

我有一个多行列表视图,使用 hashmaps 实现。我从数据库中获取数据。

我有另一个活动,我正在添加我的项目,这些项目正在添加到数据库中。现在,当按下保存按钮时,新添加的项目应立即出现在片段中实现的列表视图中。我不知道如何刷新此活动中的列表视图。

这是我实现列表视图的片段:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {             
            myView = inflater.inflate(R.layout.fragment_layout, container, false);



            l = db.getAllEntries();

            list = (ListView) myView.findViewById(R.id.entrylist);
            mylist = new ArrayList<HashMap<String,String>>();


            HashMap<String,String> item;


            while(!l.isEmpty()){

                e = l.get(0);
                l.remove(0);

              item = new HashMap<String,String>();

              item.put( "line1", e._totime + " - " + e._totime);
              item.put( "line2", e._subject);
              item.put( "line3", e._place);
              mylist.add(n++,item);
            }

              sa = new SimpleAdapter(getActivity(), mylist,
                        R.layout.multi_line,
                        new String[] { "line1","line2", "line3" },
                        new int[] {R.id.line_a, R.id.line_b, R.id.line_c}); 

            list.setAdapter(sa);
return myView;
    }

在这个活动中,我正在向数据库中添加项目,它应该立即出现在我上面的列表视图中:

public boolean onOptionsItemSelected(MenuItem item) {
  switch (item.getItemId()) {
  case R.id.save:



      sub = subject.getText().toString();
      teach = teacher.getText().toString();
      pla = place.getText().toString();
      da = day.getSelectedItem().toString();
      fro = fromtime.getText().toString();
      to = totime.getText().toString();


      i = new Entries(sub,teach,pla,da,fro,to);
      db.addEntry(i);



      FragmentView1 f = (FragmentView1) this.getSupportFragmentManager().findFragmentByTag(FragmentView1.class.getName());


      l = db.getAllEntries();
      HashMap<String,String> map;


      while(!l.isEmpty()){

        e = l.get(0);
        l.remove(0);

        map = new HashMap<String,String>();

        map.put( "line1", e._fromtime + " - " + e._totime);
        map.put( "line2", e._subject);
        map.put( "line3", e._place);
        f.getArrayList().add(n++,map);    //nullpointerexception here
      }
      f.getMyListAdapter().notifyDataSetChanged();


        /*Toast.makeText(this, "You have chosen the " + getResources().getString(R.string.save) + " menu option",
                    Toast.LENGTH_SHORT).show();*/
        return true;


  default:
        return super.onOptionsItemSelected(item);
  }

}

【问题讨论】:

    标签: android android-listview hashmap


    【解决方案1】:

    这可能是因为 findFragmentByTag() 无法找到所需的片段,因此返回 null,导致尝试访问它时出现 NullPointerException。 您可以通过检索片段的标签并将其存储在活动类变量中以供进一步访问来尝试解决方法,如下所示:

    在您的活动类中,声明一个私有 String 变量和它的 getter/setter 方法:

        public String fragmentView1Tag;
    
    public String getfragmentView1Tag() {
        return fragmentView1Tag;
    }
    
    public void setfragmentView1Tag(String fragmentView1Tag) {
        this.fragmentView1Tag = fragmentView1Tag;
    }
    

    在 FragmentView1 类中,获取片段的标签并设置 fragmentView1Tag 变量:

        String tag = getTag();
        ((YourActivityName) getActivity()).setfragmentView1Tag(tag);
    

    现在在 Activity 类中检索片段时,使用上面的 getter 方法:

        FragmentView1 f = (FragmentView1) this.getSupportFragmentManager().findFragmentByTag(getfragmentView1Tag);
    

    【讨论】:

    • ((YourActivityName) getActivity()).setfragmentView1Tag(tag) 给了我classcastexception,我实际上是在尝试在其他活动中调用片段,而不是在主要活动中调用(此片段的父活动)跨度>
    • 您可以将来自其他活动的更新保存到数据库。为了更新列表中的结果(包含在另一个活动的片段中),您可以在片段类的 onResume() 回调方法中编写更新代码,而不是尝试调用其任何方法。因此,每当片段成为焦点时,它总是会在数据库中获取更新数据。欲了解更多信息:Fragment Lifecycle
    【解决方案2】:

    使用游标从数据库中获取所有行。然后使用光标适配器填充列表。向数据库添加新行后,只需调用 notifyDataSetChanged();在光标适配器上。

    【讨论】:

    • 我能够从数据库中填充我的列表,我只是无法在单击保存按钮后立即填充,我必须重新打开我的应用程序。单击“保存”按钮时,我想用新添加的项目刷新我的列表视图
    • 你是说我应该在 onoptionitemclicked 内再次使用光标适配器来填充列表
    • @user1304 如果您使用游标适配器填充列表,则只需在该适配器上调用 notifydatasetchanged 即可更新列表
    • @user1304 等待我明白你的意思,我认为你可以在你的简单适配器上调用 notifydatasetchanged,但你需要更新 mylist 以便它包含新条目
    • @user1304 您可以在将新条目插入数据库后将其添加到 mylist
    猜你喜欢
    • 1970-01-01
    • 2014-10-09
    • 2011-08-31
    • 2011-03-18
    • 2011-05-04
    • 2018-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多