Android刷新片段中的列表视图 [英] Android refresh listview in fragment

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

问题描述

如何刷新此片段中的ListView? 我有一个活动性和一个片段,我想用SwipeRefreshLayout和FloatingActionButton刷新ListView. 这是我的片段:

How can i refresh the ListView in this Fragment? I have an acivity and a fragment, and i want to refresh the ListView with a SwipeRefreshLayout and with a FloatingActionButton. Here is my Fragment:

public class NotesBottomFragment extends Fragment {



public final Object getInt(int num)
{
    DatabaseNote dbn = new DatabaseNote();
    SQLiteDatabase notes = getActivity().openOrCreateDatabase("ENAPLO", getActivity().MODE_PRIVATE, null);
    String value2 = "novalue";
    int value = 0;
    Bundle extras = getActivity().getIntent().getExtras();
    if(extras != null) {       
        value = dbn.getIdBySubject(notes, extras.getString(SubjectBottomFragment.SUB_CODE));       
        value2 = extras.getString(SubjectBottomFragment.SUB_NAME);
    }
    if (num == 1){
        return value;
    }
    else {
        return value2;

    }

}
public NotesBottomFragment(){}
public static final String TAG = "NotesBottomFragment";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    TextView tv = (TextView)getActivity().findViewById(R.id.tvHead);
    tv.setText(getInt(2) + " notes");
    View rootView = inflater.inflate(R.layout.fragment_notes, container, false);
    final ArrayList<ListviewNotes> listContact = GetlistContact();
    final ListView lv = (ListView)rootView.findViewById(R.id.lv_contact);
    ListviewNotesAdapter adapter = new ListviewNotesAdapter(getActivity(), listContact);
    lv.setAdapter(adapter);
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView parent, View v, int position, long id) {
            ListviewNotes selectedListviewSubject = (ListviewNotes) listContact.get(position);
            Toast.makeText(getActivity(), selectedListviewSubject.getDescription(), Toast.LENGTH_LONG).show();
        }
    });
    return rootView;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    final DatabaseNote dbn = new DatabaseNote();
    final SQLiteDatabase notes = getActivity().openOrCreateDatabase("ENAPLO", getActivity().MODE_PRIVATE, null);
    final SwipeRefreshLayout swr = (SwipeRefreshLayout) getActivity().findViewById(R.id.swiperefresh);
    final FloatingActionButton fab = (FloatingActionButton) getActivity().findViewById(R.id.fab);
    swr.setColorSchemeResources(R.color.green, R.color.orange, R.color.blue);
    swr.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                                 @Override
                                 public void onRefresh() {
                                     fab.setVisibility(View.INVISIBLE);
                                     dbn.refreshDatabase(notes, getContext(), getActivity().findViewById(android.R.id.content));
                                     //here want I do the refresh
                                     fab.setVisibility(View.VISIBLE);
                                 }
                             }
    );

}


private ArrayList<ListviewNotes> GetlistContact(){
    ArrayList<ListviewNotes> contactlist = new ArrayList<ListviewNotes>();
    ListviewNotes contact;
    SQLiteDatabase notes = getActivity().openOrCreateDatabase("ENAPLO", getActivity().MODE_PRIVATE, null);
    DatabaseNote dbn =new DatabaseNote();
    dbn.Create(notes);
    String tvin = "";
    for (int i = 1; i <= dbn.getCount(notes, (Integer) getInt(1)); i++) {
        String[] arr = dbn.getArray(notes, i, (Integer) getInt(1));
        tvin = tvin + arr[0] + arr[1] + arr[2] + arr[3] + arr[4]+ "\n";
        Log.d("ASD", "in");
        contact = new ListviewNotes(dbn.getSubjectById(notes, Integer.parseInt(arr[2])), arr[1], arr[3], arr[4]);
        contactlist.add(contact);
    }

    return contactlist;
}}

我不想重新开始活动,因为它看起来像是打开一个新活动. 这是我的活动:

I don't want to restart activity, because it looks like opening a new activity. Here is my activity:

public class Notes extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notes);
        TextView tv = (TextView) findViewById(R.id.tvHead);
        final DatabaseNote dbn = new DatabaseNote();

        final FragmentManager fmTop = getSupportFragmentManager();
        final FragmentTransaction ftTop = fmTop.beginTransaction();
        final Fragment fragm =  new NotesBottomFragment();
        ftTop.replace(R.id.layout_content_notes, fragm,
                SubjectBottomFragment.TAG);
        ftTop.commit();

        final SQLiteDatabase notes = openOrCreateDatabase("ENAPLO", MODE_PRIVATE, null);
        final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fab.setVisibility(View.INVISIBLE);
                dbn.refreshDatabase(notes, Notes.this, findViewById(android.R.id.content));
                //Here want I do the same refresh
                fab.setVisibility(View.VISIBLE);
            }
        });
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openOptionsMenu();
            }
        });


    }}

推荐答案

在onCreateView()外部声明适配器,以便对onCreateView()和onViewCreated()方法都可见.

Declare the adapter outside of the onCreateView(), so that it will visible to both the methods onCreateView() and onViewCreated().

ListviewNotesAdapter adapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
adapter = new ListviewNotesAdapter(getActivity(), listContact);
lv.setAdapter(adapter);

return rootView;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    final SwipeRefreshLayout swr = (SwipeRefreshLayout)getActivity().findViewById(R.id.swiperefresh);

    swr.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

    @Override
    public void onRefresh() {
        adapter.notifyDataSetChanged();
    }});
  }

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

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