2014-09-01 76 views
0

我希望你能幫助我。我是android開發新手,我已經試着研究這個,但我沒有運氣。我希望你能幫助我簡單的問題。setListadapter和getListView()undefine片段

如何在片段上使用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 

    } 






    } 

回答

5

我的活動延伸片段,我不能將它轉換爲listfragment因爲我在我的片段標籤之一顯示它。

這對我來說不太合理。你可能會重新說明一下嗎?

如何使用setListAdapter上的一個分段

答案很簡單:你不知道。這只是ListFragment公開的一種便捷方法,但您可以通過調用ListView上的適當方法在常規Fragment中完全相同。

從外觀上來看,你的佈局已經擁有了ListView

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

現在,只是呼籲listv變量的相關方法。即的setListAdapter()相當於是:

listv.setAdapter(...); 

而且,由於你已經對相關ListView一個手柄,您不再需要調用getListView()

+0

感謝您的回覆。我很感激。 – DEADPOOL 2014-09-01 02:09:39

+0

我試過了,它在logcat 09-01 10:14:12.999上顯示出這個錯誤:E/AndroidRuntime(29966):致命異常:主要 09-01 10:14:12.999:E/AndroidRuntime(29966):java.lang .NullPointerException 09-01 10:14:12.999:E/AndroidRuntime(29966):\t at com.csu.eclassrecord.AccountFragment.fillData(AccountFragment.java:183) 09-01 10:14:12.999:E/AndroidRuntime (29966):\t at com.csu.eclassrecord.AccountFragment.onCreateView(AccountFragment.java:57) – DEADPOOL 2014-09-01 02:16:18

+0

它顯示listv.setAdapter(提醒)上的空指針; :( – DEADPOOL 2014-09-01 02:28:24