使用HashMap的时候刷新列表视图 [英] refresh listview when using hashmap

查看:161
本文介绍了使用HashMap的时候刷新列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a multiline listview , implemented using hashmaps . I get my data from a database .

我有另一个活动中,我加入了我的项目,它被添加到数据库中。现在,当pressing保存按钮,新添加的项目应立即在其中一个片段中实现的列表视图出现。我无法弄清楚如何刷新列表视图这一活动。

I have another activity in which i am adding my items , which is being added to the database . Now when pressing the save button , the new added item should appear immediately in the listview which is implemented in a fragment . I can't figure out how to refresh the listview in this activity .

这是我的片段实施的ListView:

here's my fragment implementing the listview :

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;
    }

和这里这个活动我加入项目数据库,应立即出现在我上面的列表视图:

and here in this activity i am adding items to the database , which should immediately appear in my listview above :

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);
  }

}

推荐答案

这可能是因为findFragmentByTag()是无法找到所需的片段,所以是返回null,导致NullPointerException异常当您尝试访问它。
您可以通过检索标记的片段和它的活动类变量存储为进一步的访问尝试一种解决方法如下:

This is probably because findFragmentByTag() is not able to find the required fragment and so is returning null, resulting in NullPointerException when you try to access it. You can try a workaround by retrieving the tag for fragment and storing it in activity class variable for further access as follows:

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

In your activity class, declare a private String variable and getter/setter methods for it:

    public String fragmentView1Tag;

public String getfragmentView1Tag() {
    return fragmentView1Tag;
}

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

在FragmentView1类,检索片段的标签,并设置fragmentView1Tag变量:

In FragmentView1 class, retrieve the tag of the fragment and set fragmentView1Tag variable:

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

现在,在检索中活动课的片段,用上面的getter方法​​:

And now while retrieving the fragment in activity class, use the above getter method:

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

这篇关于使用HashMap的时候刷新列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆