当我从WODListFragment的onCreateView方法中的mMovements ArrayList中删除时,我从WODListActivity调用setMovements并传入更新的ArrayList(成功)。
public class WODListFragment extends ListFragment{
public static final String NEW_MOVE_ADDED = "new_move_in_list";
public static final String MOVE_REMOVED = "move_removed";
private ArrayList<Integer> mMovements;
public static WODListFragment newInstance(ArrayList<Integer> moves) {
Bundle args = new Bundle();
args.putSerializable(NEW_MOVE_ADDED, moves);
Log.d("fmovements", "size: " + moves.size());
WODListFragment fragment = new WODListFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().setTitle(R.string.movement_title);
// Get the arguments that were serialized in WODListActivity.
Bundle args = getArguments();
mMovements = (ArrayList<Integer>) args.getSerializable(NEW_MOVE_ADDED);
MovementsAdapter adapter = new MovementsAdapter(mMovements);
setListAdapter(adapter);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.wod_list, parent, false);
// get the amount of total movements allowed in the wod
Intent i = getActivity().getIntent();
int moveCount = i.getExtras().getInt("count");
final Button finalize_wod = (Button)v.findViewById(R.id.finalize_wod);
final Button delete_move = (Button)v.findViewById(R.id.delete_move);
final TextView wod_scoring = (TextView)v.findViewById(R.id.wod_scoring);
final Button start_wod = (Button)v.findViewById(R.id.start_wod);
// show finalize button when wod is full
if (mMovements.size() == moveCount) {
finalize_wod.setVisibility(View.VISIBLE);
}
// set finalize button
finalize_wod.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// change visible buttons on finalize click
finalize_wod.setVisibility(View.GONE);
delete_move.setVisibility(View.GONE);
wod_scoring.setVisibility(View.VISIBLE);
start_wod.setVisibility(View.VISIBLE);
Movement m = new Movement();
if (mMovements.size() == 3) { // 3x3
wod_scoring.setText(m.scoringExpl[0]);
}
else if (mMovements.size() == 2) { // 21-15-9
wod_scoring.setText(m.scoringExpl[1]);
}
else // max effort
wod_scoring.setText(m.scoringExpl[2]);
// set start wod button
start_wod.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// access stopwatch so user can start their wod
}
}); // end of start_wod setOnClickListener
}
}); // end of finalize_wod setOnClickListener
// set delete button
delete_move.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "delete",
Toast.LENGTH_SHORT).show();
mMovements.remove(mMovements.size() - 1);
// update the listView with the value deleted
((MovementsAdapter)getListAdapter()).notifyDataSetChanged();
Activity activity = getActivity();
if(activity instanceof WODListActivity) {
((WODListActivity) activity).setMovements(mMovements);
}
finalize_wod.setVisibility(View.GONE);
}
}); // end of delete_move setOnClickListener
return v;
}
public class MovementsAdapter extends ArrayAdapter<Integer> {
public MovementsAdapter(ArrayList<Integer> movements) {
super(getActivity(), 0, movements);
Log.d("testing", "here" + " " + movements.size());
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
if (convertView == null) {
convertView = getActivity().getLayoutInflater()
.inflate(R.layout.wod_list_item, null);
}
final int move = getItem(position);
TextView movementName = (TextView)convertView.findViewById(R.id.movement_list_item_name);
TextView movementReps = (TextView)convertView.findViewById(R.id.movement_list_item_reps);
Button aboutMove = (Button)convertView.findViewById(R.id.btnViewAbout);
Movement m = new Movement();
movementName.setText("Movement: " + m.movementNames[move]);
movementReps.setText("Reps: " + m.reps[move]);
// set about button
aboutMove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent describeMov = new Intent(v.getContext(), MovementDescriptionActivity.class);
describeMov.putExtra("id", move);
startActivity(describeMov);
}
});
return convertView;
}
}
}
public class WODListActivity extends SingleFragmentActivity{
ArrayList<Integer> movements = new ArrayList<Integer>();
@Override
protected Fragment createFragment() {
movements.addAll((Collection<? extends Integer>) getIntent().getSerializableExtra(WODListFragment.NEW_MOVE_ADDED));
return WODListFragment.newInstance(movements);
}
@Override
public void onPause() {
super.onPause();
setMovements(movements);
}
public void setMovements(ArrayList<Integer> moves) {
Log.d("amovements", "size: " + moves.size());
Intent data = new Intent();
data.putExtra(WODListFragment.NEW_MOVE_ADDED, moves);
setResult(Activity.RESULT_OK, data);
}
}
我正在尝试将此ArrayList返回到MovementSelectionFragment并将运动ArrayList设置为更新的ArrayList。在WODListActivity中,它作为移动ArrayList传入。我尝试在MovementSelectionFragment中修改onActivityResult方法(以及其他大约100种不同的组合)。我也尝试修改WODListActivity中的onPause方法。无论如何,我很沮丧,我需要听到一些不同的观点。
public class MovementSelectionFragment extends Fragment{
private static final int REQUEST_NEW_MOVEMENT = 0;
public static final String NEW_MOVE_ADDED = "new_move_in_list";
public static final String MOVE_REMOVED = "move_removed";
private int numSelLeft;
private int origCount;
private GridView mGridView;
Boolean moveDel = false;
/*
* This array allows the user to select multiple workouts at once.
* It doesn't need to be reinstantiated every time the user backs up
* to this view.
*/
private ArrayList<Integer> movements = new ArrayList<Integer>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.movement_selection, parent, false);
// display number of moves in wod
if (movements.isEmpty()) {
numSelLeft = displayBeforeSelect(v);
origCount = numSelLeft;
}
// locate gridView and assign ImageAdapter
mGridView = (GridView)v.findViewById(R.id.gridView);
mGridView.setAdapter(new ImageAdapter(v.getContext()));
mGridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position,
long id) {
// Sending image id to WODListActivity
Intent movementIntent = new Intent(v.getContext(),
WODListActivity.class);
movements.add(position); // Add position of movement to array
v.setVisibility(View.GONE); // kill selected button
movementIntent.putExtra(WODListFragment.NEW_MOVE_ADDED,
movements);
movementIntent.putExtra("count", origCount);
startActivityForResult(movementIntent, REQUEST_NEW_MOVEMENT);
}
});
return v;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != Activity.RESULT_OK) {
return;
}
}
感谢。
答案 0 :(得分:0)
您可以将Adapter
作为字段存储在Activity
中,对其进行获取并通过片段访问它,例如(WODListActivity) getActivity().getMovementsAdapter();