setListadapter和getListView()在片段上取消定义

时间:2014-09-01 01:31:59

标签: android android-fragments android-listview

我希望你能帮助我。我是Android开发的新手,我已经尝试过对此进行研究,但我没有运气。我希望你能解决我的简单问题。

如何在片段上使用setListAdapter?我的活动扩展了片段,我无法将其转换为listfragment,因为我在其中一个片段选项卡中显示它。

public class AccountFragment extends Fragment implements OnItemClickListener {
private LoginDataBaseAdapter loginDataBaseAdapter;

private RemindersDbAdapter mDbHelper;

ListView listv;

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

         mDbHelper = new RemindersDbAdapter(getActivity());
         mDbHelper.open();
         fillData();

     //registerForContextMenu(getListView());----- Not working too :(

      //intialize variables


        View rootView = inflater.inflate(R.layout.fragment_account, container, false);




            registerForContextMenu(getView());
            listv = (ListView) rootView.findViewById(R.id.remlist);







        return rootView;


         }

    @Override
    public void onActivityResult( int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        fillData();


    }

    private void fillData() {
        Cursor remindersCursor = mDbHelper.fetchAllReminders();
        getActivity().startManagingCursor(remindersCursor);

        // Create an array to specify the fields we want to display in the list (only TITLE)
        String[] from = new String[]{RemindersDbAdapter.KEY_TITLE};

        // and an array of the fields we want to bind those fields to (in this case just text1)
        int[] to = new int[]{R.id.text1};

        // Now create a simple cursor adapter and set it to display
        SimpleCursorAdapter reminders = 
                new SimpleCursorAdapter(getActivity(), R.layout.reminder_row, remindersCursor, from, to);
        setListAdapter(reminders);
    }


    public boolean onCreateOptionsMenu(Menu menu) {
        super.getActivity().onCreateOptionsMenu(menu);
        MenuInflater mi = getActivity().getMenuInflater();
        mi.inflate(R.menu.list_menu, menu); 
        return true;
    }


    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch(item.getItemId()) {
        case R.id.menu_insert: 
            createReminder();
            return true; 
        case R.id.menu_settings: 
            Intent i = new Intent(getActivity(), TaskPreferences.class); 
            startActivity(i); 
            return true;
        }

        return super.getActivity().onMenuItemSelected(featureId, item);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        MenuInflater mi = getActivity().getMenuInflater(); 
        mi.inflate(R.menu.list_menu_item_longpress, menu); 
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch(item.getItemId()) {
        case R.id.menu_delete:
            AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
            mDbHelper.deleteReminder(info.id);
            fillData();
            return true;
        }
        return super.onContextItemSelected(item);
    }

    private void createReminder() {
        Intent i = new Intent(getActivity(), ReminderEditActivity.class);
        startActivityForResult(i, ACTIVITY_CREATE);
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        // TODO Auto-generated method stub

    }






    }

1 个答案:

答案 0 :(得分:6)

  

我的活动扩展了片段,我无法将其转换为listfragment,因为我在其中一个片段标签中显示它。

这对我来说并不合理。您是否可以重新阐述/阐述它?

  

如何在片段

上使用setListAdapter

答案很简单:你没有。它只是ListFragment公开的便捷方法之一,但您可以通过调用Fragment上的相应方法,在常规ListView中执行完全相同的操作。

从外观上看,您的布局已经有ListView

listv = (ListView) rootView.findViewById(R.id.remlist);

现在,只需调用listv变量上的相关方法即可。即相当于setListAdapter()的是:

listv.setAdapter(...);

此外,由于您已经掌握了相关的ListView,因此您无需再致电getListView()