Android可滚动列表



我有一个元素列表和一个AbsListView。如何在滚动时加载列表中的元素?

这是我的列表:private List<Database_elem> database_elemList = new ArrayList<Database_elem>();
public class PlacesFragment extends Fragment implements AbsListView.OnItemClickListener {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private List<Database_elem> database_elemList = new ArrayList<Database_elem>();
    private OnFragmentInteractionListener mListener;
    /**
     * The fragment's ListView/GridView.
     */
    private AbsListView mListView;
    ImageView imageView;
    /**
     * The Adapter which will be used to populate the ListView/GridView with
     * Views.
     */
    private ListAdapter mAdapter;
    // TODO: Rename and change types of parameters
    public static PlacesFragment newInstance(String param1, String param2) {
        PlacesFragment fragment = new PlacesFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }
    /**
     * Mandatory empty constructor for the fragment manager to instantiate the
     * fragment (e.g. upon screen orientation changes).
     */
    public PlacesFragment() {
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
        // TODO: Change Adapter to display your content
//        mAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1, DummyContent.ITEMS);
    }
    String[] test;
    String text = "Central Park";
    Date data = new Date();
    String data_database = String.valueOf(data);

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.fragment_place, container, false);
        Context ctx = view.getContext();
        DataBase dataBase = new DataBase(getActivity().getApplicationContext());
//        SQLiteDatabase db = dataBase.getReadableDatabase();
        Cursor cr = dataBase.getInformation(dataBase);
        cr.moveToFirst();

//        dataBase.onCreate(db);
//        ctx.deleteDatabase(DataBase.TABLE_NAME);
        final DetectionLocation detectionLocation = new DetectionLocation(ctx);
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
        Integer val = Integer.valueOf(prefs.getString("sync_frequency", ""));
        String sync_status = String.valueOf(val);

        dataBase.insertInformation(dataBase, "Guid", String.valueOf(detectionLocation.getLongitude()), String.valueOf(detectionLocation.getLatitude()), "Sincronizare", data, sync_status);
        while (true) {
            if (!cr.isLast()) {
                database_elemList.add(new Database_elem(cr.getString(0), cr.getString(1), cr.getString(2), cr.getString(3), cr.getString(4), cr.getString(5)));
                cr.moveToNext();
            } else {
                break;
            }
        }
        test = new String[database_elemList.size()];
        //System.out.println(cr.getString(1));
        for (int i = 0; i < database_elemList.size(); i++) {
            CustomListAdapter adapter = new CustomListAdapter(getActivity(), test, database_elemList.get(0).getLat() + "  " + database_elemList.get(0).getLon(), database_elemList.get(0).getSync_date());
            mListView = (AbsListView) view.findViewById(android.R.id.list);
            mListView.setAdapter(adapter);
        }
        Timer timer = new Timer();

        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
//                        DataBase dataBase = new DataBase(getActivity().getApplicationContext());
//                        Cursor cr = dataBase.getInformation(dataBase);
//                        cr.moveToFirst();
                        for (int i = 0; i < database_elemList.size(); i++) {
                            CustomListAdapter adapter = new CustomListAdapter(getActivity(), test, database_elemList.get(0).getLat() + "  " + database_elemList.get(0).getLon(), database_elemList.get(0).getSync_date());
                            mListView = (AbsListView) view.findViewById(android.R.id.list);
                            mListView.setAdapter(adapter);
                        }
                    }
                });
            }
        }
                , val);
        mListView.setOnItemClickListener(this);
        return view;
    }
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }
    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (null != mListener) {
            // Notify the active callbacks interface (the activity, if the
            // fragment is attached to one) that an item has been selected.
            mListener.onFragmentInteraction(DummyContent.ITEMS.get(position).id);
        }
    }
    /**
     * The default content for this Fragment has a TextView that is shown when
     * the list is empty. If you would like to change the text, call this method
     * to supply the text it should use.
     */
    public void setEmptyText(CharSequence emptyText) {
        View emptyView = mListView.getEmptyView();
        if (emptyView instanceof TextView) {
            ((TextView) emptyView).setText(emptyText);
        }
    }
    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p/>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        public void onFragmentInteraction(String id);
    }
}

您可以直接使用CursorAdapter

相关内容

  • 没有找到相关文章

最新更新