我正在通过函数Actionmodecallback在我的类Topratedfragment.java中传递 Context,Actionmode,list和position 的值 并且在函数中检索值时,除了位置的值,我正在尝试将其值保存在类的全局变量中时,所有值都正常。我很抱歉,如果我的问题是愚蠢的,我对传递java
的想法非常少我的两个班级都是
Topratedfragment class
public class TopRatedFragment extends Fragment {
Intent i;
TextView studentid;
Button Bite;
DBHelper mydb;
Cursor mcursor;
ListView list;
// Context context;
Simpleadapter adapter;
int nr = 0;
ArrayList<String> array_list;
ActionMode mActionMode;
String valstudentid;
int _position;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_top_rated,
container, false);
DBHelper mydb = new DBHelper(getActivity().getApplicationContext());
array_list = mydb.getAllCotacts();
list = (ListView) rootView.findViewById(R.id.listview1);
adapter = new Simpleadapter(getActivity(), array_list);
list.setAdapter(adapter);
list.clearChoices();
for (int i = 0; i < list.getCount(); i++)
list.setItemChecked(i, false);
list.post(new Runnable() {
@Override
public void run() {
list.setChoiceMode(ListView.CHOICE_MODE_NONE);
}
});
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
if (mActionMode == null) {
int id_To_Search = position + 1;
Bundle dataBundle = new Bundle();
dataBundle.putInt("id", id_To_Search);
Intent intent = new Intent(
getActivity().getApplicationContext(),
info.meritech.project.databasefiles.DisplayContact.class);
intent.putExtras(dataBundle);
startActivity(intent);
} else {
onListItemCheck(position);
}
}
});
list.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int position, long id) {
onListItemCheck(position);
return true;
}
});
return rootView;
}
private void onListItemCheck(int position) {
// TODO Auto-generated method stub
_position = position;
adapter.toggleSelection(position);
boolean hasCheckedItems = adapter.getSelectedCount() > 0;
if (hasCheckedItems && mActionMode == null)
// there are some selected items, start the actionMode
mActionMode = getActivity().startActionMode(
new ActionModeCallback(getActivity()
.getApplicationContext(), array_list, list,
mActionMode, position));
else if (!hasCheckedItems && mActionMode != null)
// there no selected items, finish the actionMode
mActionMode.finish();
if (mActionMode != null)
mActionMode.setTitle(String.valueOf(adapter.getSelectedCount())
+ " selected");
}
public Context getActivity(Context context) {
context = getActivity().getApplicationContext();
return context;
}
}
Actionmodecallback类
public class ActionModeCallback implements ActionMode.Callback {
Context _context;
ActionMode _mActionMode;
ArrayList<String> _array_list;
ListView _list;
int _position_list;
public ActionModeCallback(Context context, ArrayList<String> array_list,
ListView list, ActionMode mActionMode, int position) {
super();
this._context = context;
this._mActionMode = mActionMode;
this._array_list = array_list;
this._list = list;
this._position_list=position;
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// inflate contextual menu
mode.getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// retrieve selected items and print them out
switch (item.getItemId()) {
case R.id.add:
int id_To_Search = +1;
Bundle dataBundle = new Bundle();
dataBundle.putInt("id", id_To_Search);
Intent intent = new Intent(_context, Edit.class);
intent.putExtras(dataBundle);
_context.startActivity(intent);
Toast.makeText(_context, "new class", Toast.LENGTH_SHORT).show();
// close action mode
mode.finish();
break;
}
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
// remove selection
Simpleadapter adapter = new Simpleadapter(_context, _array_list);
adapter.removeSelection();
_mActionMode = null;
_list.clearChoices();
for (int i = 0; i < _list.getCount(); i++)
_list.setItemChecked(i, false);
_list.post(new Runnable() {
@Override
public void run() {
_list.setChoiceMode(ListView.CHOICE_MODE_NONE);
}
});
}
}
我在ActionmodeCallback.class的第13行得到 this._position_list = 0 的值,其中因为其他变量的值非常好,为什么它会发生在这个变量位置
this._position_list=position;