在我重新启动应用程序之前,ListView不会在片段内更新

时间:2015-01-12 15:08:52

标签: android listview android-fragments android-listview android-sqlite

我的MainActivity实现了PagerSlidingTabStrip。所以我有两个标签(CalculatorFragment和HistoryFragment)。在第一个(CalculatorFragment)中,用户输入信息并单击将其添加到数据库的按钮。第二个选项卡(HistoryFragment)应该列出数据库中的所有项目。问题是该列表在重新启动我的应用程序之前不会更新。我知道为什么 - 因为列表只在onCreateView上填充。我的问题是如何解决这个问题。

MainActivity.java

public class MainActivity extends ActionBarActivity {

private TabsAdapter tabsAdapter;
private ViewPager viewPager;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tabsAdapter = new TabsAdapter(getSupportFragmentManager());
    viewPager = (ViewPager) findViewById(R.id.pager);
    viewPager.setAdapter(tabsAdapter);

    PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);

    tabs.setViewPager(viewPager);

}}

TAB1

public class CalculatorFragment extends Fragment {
@Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.calculator_layout, container, false);
        final DBTools dbTools = DBTools.getInstance(this.getActivity());

        //... the OnClickListener that adds all info to the database

        fab.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {

                //... code

                dbTools.insertRecord(queryValuesMap);
            }
        });

带有ListView的Tab2

public class HistoryFragment extends Fragment {

    public static RecordArrayAdapter adapter;
    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.history_layout, container, false);
        DBTools dbTools = DBTools.getInstance(this.getActivity());

        List<RecordObject> recordsList = dbTools.getAllRecords();

        Log.d("History-Fragment", recordsList.toString());

        if (recordsList.size() != 0) {

            ListView listView = (ListView) rootView.findViewById(R.id.records_list);


            adapter = new RecordArrayAdapter(this.getActivity(), recordsList);

            listView.setAdapter(adapter);

        }
        return rootView;
    }
}

RecordsArrayAdapter

public class RecordArrayAdapter extends ArrayAdapter<RecordObject> {

    SimpleDateFormat oldFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
    SimpleDateFormat newFormat = new SimpleDateFormat("MMM dd, yy", Locale.US);

    private LayoutInflater inflater;
    DBTools dbTools = new DBTools(this.getContext());

    public RecordArrayAdapter(Context context, List<RecordObject> objects) {
        super(context, 0, objects);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.record_entry, parent, false);
        }

        RecordObject recordObject = getItem(position);

        TextView recordsID = (TextView) convertView.findViewById(R.id.key_id);

        TextView weight = (TextView) convertView.findViewById(R.id.weight);
        TextView reps = (TextView) convertView.findViewById(R.id.reps);
        TextView oneRM = (TextView) convertView.findViewById(R.id.onerm);
        TextView date = (TextView) convertView.findViewById(R.id.date);

        newFormat.setLenient(false);
        try {
            date.setText(newFormat.format(oldFormat.parse(recordObject.getDate())));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        weight.setText(""+recordObject.getWeight());
        reps.setText(("'"+recordObject.getReps()));
        oneRM.setText(""+recordObject.getOneRM());

        return convertView;
    }
}

1 个答案:

答案 0 :(得分:1)

当片段位于所选标签中时,将使用setUserVisibleHint()调用true,当片段不是所选标签时,将使用false调用该片段。

HistoryFragment

中添加此方法
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);

    if (isVisibleToUser){
        DBTools dbTools = DBTools.getInstance(this.getActivity());
        List<RecordObject> recordsList = dbTools.getAllRecords();
        if (recordsList.size() != 0) {
            ListView listView = (ListView) rootView.findViewById(R.id.records_list);
            adapter = new RecordArrayAdapter(this.getActivity(), recordsList);
            listView.setAdapter(adapter);
        }
    }
    else
        Log.d("HistoryFragment", "HistoryFragment is not visible.");
}

注意在班级定义View rootView,现在在onCreateView()

您的片段应该如下所示

public class HistoryFragment extends Fragment {

    public static RecordArrayAdapter adapter;
    View rootView = null;
    DBTools dbTools = null;

    @Override
    public View onCreateView(LayoutInflater inflater,
                         ViewGroup container, Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.history_layout, container, false);
        loadScreen();
        return rootView;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

        if (isVisibleToUser)
            loadScreen();
        else
            Log.d("HistoryFragment", "HistoryFragment is not visible.");
     }

    private void loadScreen(){
        dbTools = DBTools.getInstance(this.getActivity());
        List<RecordObject> recordsList = dbTools.getAllRecords();
        if (recordsList.size() != 0) {
            ListView listView = (ListView) rootView.findViewById(R.id.records_list);
            adapter = new RecordArrayAdapter(this.getActivity(), recordsList);
            listView.setAdapter(adapter);
        }
    }

}