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
}
}
感謝您的回覆。我很感激。 – DEADPOOL 2014-09-01 02:09:39
我試過了,它在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
它顯示listv.setAdapter(提醒)上的空指針; :( – DEADPOOL 2014-09-01 02:28:24