- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有 2 个 fragment 和一个 Activity 。 fragment 1 有一个带有 ListView 和一些按钮等的相对布局,Fragment 2 在底部有一个 ListView 和一个按钮。 fragment 2 的 ListView 是一个 ListView ,您可以在其中选择多个项目,当一个项目被选中时,它被添加到一个 arraylist(我们将称之为选中)或者如果取消选择则被删除,我想将这个 arrayList 发送到 Fragment 1,它在那里可以将它添加到它自己的 ListView (以及它的其他项目,如果它们存在)。我已将选定的 ArrayList 存储在 Activity 中。
Activity 有以下代码
public class AddExerciseActivity extends AppCompatActivity implements ActionBar.TabListener {
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;
static ArrayList<Set> sets = new ArrayList<>();
String name, date;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
Bundle extras = getIntent().getExtras();
if (extras != null) {
name = extras.getString("ExerciseName");
date = extras.getString("date");
}
// Set up the action bar.
final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setTitle(name);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
// When swiping between different sections, select the corresponding
// tab. We can also use ActionBar.Tab#select() to do this if we have
// a reference to the Tab.
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by
// the adapter. Also specify this Activity object, which implements
// the TabListener interface, as the callback (listener) for when
// this tab is selected.
actionBar.addTab(
actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in
// the ViewPager.
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
switch (position) {
case 0:
Bundle bundle = new Bundle();
bundle.putString("date", date);
bundle.putInt("position", position);
return new FirstFragment();
case 1:
return new SecondFragment();
}
return null;
}
@Override
public int getCount() {
// Show 3 total pages.
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Track";
case 1:
return "History";
}
return null;
}
}
public ArrayList<Set> getSets() {
return sets;
}
public void setSets(ArrayList<Set> sets) {
this.sets = sets;
}
}
这是 fragment 1
public class FirstFragment extends android.support.v4.app.Fragment {
CardDatabaseAdapter2 mCardDatabaseAdapter2;
TextView workoutName;
EditText weight, reps;
double weightCount;
int repsCount;
ListView list;
Button plus, plus2, minus, minus2, update;
String name, date;
int type, category, count;
Exercise ex;
Card card;
View rootView;
ArrayList<Set> array = new ArrayList<>();
private AddExerciseAdapter mAddExerciseAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.add_exercise_view, container, false);
setHasOptionsMenu(true);
Bundle extras = getActivity().getIntent().getExtras();
if (extras != null) {
name = extras.getString("ExerciseName");
type = extras.getInt("ExerciseType");
category = extras.getInt("ExerciseCategory");
date = extras.getString("date");
Log.v("date", date);
}
mCardDatabaseAdapter2 = new CardDatabaseAdapter2(getActivity());
Log.v("created", "item created");
//mCardDatabaseAdapter2.insert(name, date);
card = new Card(0, name, date);
Log.v("name & date", name + " " + date);
ex = new Exercise(name, type, category);
Log.v("Exercise Recieved", ex.toString());
workoutName = (TextView) rootView.findViewById(R.id.name);
weight = (EditText) rootView.findViewById(R.id.textWeight);
reps = (EditText) rootView.findViewById(R.id.textRep);
list = (ListView) rootView.findViewById(R.id.rowReps);
plus = (Button) rootView.findViewById(R.id.plus);
minus = (Button) rootView.findViewById(R.id.minus);
plus2 = (Button) rootView.findViewById(R.id.plus2);
minus2 = (Button) rootView.findViewById(R.id.minus2);
update = (Button) rootView.findViewById(R.id.update);
weightCount = 0;
repsCount = 0;
count = 1;
array = new ArrayList<>();
plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!weight.getText().toString().equals("")) {
weightCount = Double.parseDouble(weight.getText().toString());
weightCount = weightCount + 2.5;
weight.setText(String.valueOf(weightCount));
} else {
weight.setText(String.valueOf(2.5));
}
}
});
minus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(weightCount <= 0.0 || weight.getText().toString().equals("")) {
weight.setText(String.valueOf(0.0));
} else {
weightCount = Double.parseDouble(weight.getText().toString());
weightCount = weightCount - 2.5;
weight.setText(String.valueOf(weightCount));
}
}
});
plus2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!reps.getText().toString().equals("")) {
repsCount = Integer.parseInt(reps.getText().toString());
repsCount = repsCount + 1;
reps.setText(String.valueOf(repsCount));
} else {
reps.setText(String.valueOf(0));
}
}
});
minus2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(repsCount <= 0 || reps.getText().toString().equals("")){
reps.setText(String.valueOf(0));
} else {
repsCount = Integer.parseInt(reps.getText().toString());
repsCount = repsCount - 1;
reps.setText(String.valueOf(repsCount));
}
}
});
update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(weight.getText().toString().equals("") || weight.getText().toString().equals(String.valueOf(0.0))) {
weight.setText(String.valueOf(0.0));
Toast.makeText(getActivity(), "You need to enter a weight!", Toast.LENGTH_SHORT).show();
}
else if(reps.getText().toString().equals("")|| reps.getText().toString().equals(String.valueOf(0))) {
reps.setText(String.valueOf(0));
Toast.makeText(getActivity(), "You need to have at least 1 rep!", Toast.LENGTH_SHORT).show();
} else {
double temp2 = Double.parseDouble(weight.getText().toString());
int temp3 = Integer.parseInt(reps.getText().toString());
Set s = new Set(name, temp2, temp3, date);
array.add(s);
//mCardDatabaseAdapter2.insert2(mCardDatabaseAdapter2.highestID(), name, s.getWeight(), s.getReps(), date);
//Log.v("inserted data", "id " + mCardDatabaseAdapter2.highestID() + " name " + name + " weight " + s.getWeight() + " reps " + s.getReps() + " date " + date);
//Log.v("id", mCardDatabaseAdapter2.highestID() + "");
mAddExerciseAdapter.notifyDataSetChanged();
count++;
}
}
});
mAddExerciseAdapter = new AddExerciseAdapter(getActivity(), R.layout.row2, array);
list.setAdapter(mAddExerciseAdapter);
mAddExerciseAdapter.notifyDataSetChanged();
return rootView;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_add_exercise, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_done) {
Intent intent = new Intent(getActivity(), MainMenuActivity.class);
if(!array.isEmpty()) {
mCardDatabaseAdapter2.insert(name, date);
for (Set s : array) {
mCardDatabaseAdapter2.insert2(mCardDatabaseAdapter2.highestID(), name, s.getWeight(), s.getReps(), date);
}
mCardDatabaseAdapter2.resetID();
}
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
@Override
public void onPause() {
super.onPause();
}
@Override
public void onStop() {
super.onStop();
}
@Override
public void onResume() {
super.onResume();
ArrayList<Set> s = ((AddExerciseActivity)getActivity()).getSets();
if(!s.isEmpty()) {
for(Set t : s) {
array.add(t);
mAddExerciseAdapter.notifyDataSetChanged();
}
}
}
}
这是 fragment 2
public class SecondFragment extends Fragment {
CardDatabaseAdapter2 mCardDatabaseAdapter;
ExerciseHistoryAdapter mExerciseHistoryAdapter;
AddExerciseActivity mAddExerciseActivity;
ArrayList<Set> sets = new ArrayList<>();
ArrayList<Set> selected = new ArrayList<>();
String name;
ListView lv;
Card c;
int x;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.exercise_history, container, false);
Bundle extras = getActivity().getIntent().getExtras();
if (extras != null) {
name = extras.getString("ExerciseName");
}
mAddExerciseActivity = new AddExerciseActivity();
mCardDatabaseAdapter = new CardDatabaseAdapter2(getActivity());
sets = mCardDatabaseAdapter.getAllSetsWithName(name);
Log.v("array", sets.toString());
lv = (ListView) rootView.findViewById(R.id.history_list);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (!selected.contains(sets.get(position))) {
lv.setItemChecked(position, true);
view.setBackgroundColor(Color.rgb(255, 152, 0));
Toast.makeText(getActivity(), "Item Selected", Toast.LENGTH_SHORT).show();
selected.add(sets.get(position));
} else {
lv.setItemChecked(position, false);
view.setBackgroundColor(Color.rgb(221, 221, 221));
selected.remove(sets.get(position));
Toast.makeText(getActivity(), "Item Unselected", Toast.LENGTH_SHORT).show();
}
}
});
Button add = (Button) rootView.findViewById(R.id.add_button);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((AddExerciseActivity)getActivity()).setSets(selected);
Toast.makeText(getActivity(), "items have been added successfully", Toast.LENGTH_SHORT).show();
}
});
mExerciseHistoryAdapter= new ExerciseHistoryAdapter(getActivity(), R.layout.history_listview_item, sets, lv);
lv.setAdapter(mExerciseHistoryAdapter);
return rootView;
}
}
编辑:我的尝试就在那里,我在那里获取/设置 arrayList。但它不起作用,因为 fragment 已经创建。
编辑 2:
04-23 20:18:48.034 598-598/com.company.joeliomason.projectmeE/InputEventReceiver﹕ Exceptiondispatching input event.
04-23 20:18:48.034 598-598/com.company.joeliomason.projectme
E/MessageQueue-JNI﹕ Exception in MessageQueue callback:
handleReceiveCallback
04-23 20:18:48.034 598-598/com.company.joeliomason.projectme
E/MessageQueue-JNI﹕ java.lang.IllegalStateException: Fragment already
active
at android.support.v4.app.Fragment.setArguments(Fragment.java:549)
at com.company.joeliomason.projectme.Views.FirstFragment.setArguments(FirstFragment.java:200)
at com.company.joeliomason.projectme.Views.AddExerciseActivity.onTabSelected(AddExerciseActivity.java:94)
at android.support.v7.internal.app.WindowDecorActionBar.selectTab(WindowDecorActionBar.java:640)
at android.support.v7.internal.app.WindowDecorActionBar.setSelectedNavigationItem(WindowDecorActionBar.java:414)
at com.company.joeliomason.projectme.Views.AddExerciseActivity$1.onPageSelected(AddExerciseActivity.java:68)
最佳答案
这可能有效,但我还没有测试过。看起来您可以在 onTabSelected()
方法中使用新方法调用来传递 Bundle
。
在 AddExerciseActivity
中创建对 FirstFragment
的引用作为实例变量:
ViewPager mViewPager;
static ArrayList<Set> sets = new ArrayList<>();
FirstFragment fragmentFirst; //added
在您的 SectionsPagerAdapter
中设置它:
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
switch (position) {
case 0:
Bundle bundle = new Bundle();
bundle.putString("date", date);
bundle.putInt("position", position);
fragmentFirst = new FirstFragment(); //modified
return fragmentFirst; //added
case 1:
return new SecondFragment();
}
return null;
}
然后,添加,添加sets
到一个Bundle
,并传入onTabSelected
中的FirstFragment
:
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in
// the ViewPager.
mViewPager.setCurrentItem(tab.getPosition());
if (fragmentFirst != null){
Bundle bundle = new Bundle();
bundle.putSerializable("sets", sets);
//fragmentFirst.setArguments(bundle); //not working
fragmentFirst.updateArguments(bundle); //try this
}
}
编辑:在 FragmentFirst
中添加一个新的公共(public)方法:
public void updateArguments(Bundle args) {
ArrayList<Set> x = new ArrayList<>();
x = (ArrayList<Set>) args.getSerializable("sets");
for(Set p: x) {
array.add(p);
}
mAddExerciseAdapter.notifyDataSetChanged();
}
关于java - 在同一 Activity 的 2 个 fragment 之间发送 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29829985/
我需要修复 getLineNumberFor 方法,以便如果 lastName 的第一个字符位于 A 和 M 之间,则返回 1;如果它位于 N 和 Z 之间,则返回 2。 在我看来听起来很简单,但我不
您好,感谢您的帮助!我有这个: 0 我必须在每次点击后增加“pinli
Javascript 中是否有一种方法可以在不使用 if 语句的情况下通过 switch case 结构将一个整数与另一个整数进行比较? 例如。 switch(integer) { case
我有一列是“日期”类型的。如何在自定义选项中使用“之间”选项? 最佳答案 请注意,您有2个盒子。 between(在SQL中)包含所有内容,因此将框1设置为:DATE >= startdate,将框2
我有一个表,其中包含年、月和一些数字列 Year Month Total 2011 10 100 2011 11 150 2011 12 100 20
这个问题已经有答案了: Extract a substring between double quotes with regular expression in Java (2 个回答) how to
我有一个带有类别的边栏。正如你在这里看到的:http://kees.een-site-bouwen.nl/ url 中类别的 ID。带有 uri 段(3)当您单击其中一个类别时,例如网页设计。显示了一
这个问题在这里已经有了答案: My regex is matching too much. How do I make it stop? [duplicate] (5 个答案) 关闭 4 年前。 我
我很不会写正则表达式。 我正在尝试获取括号“()”之间的值。像下面这样的东西...... $a = "POLYGON((1 1,2 2,3 3,1 1))"; preg_match_all("/\((
我必须添加一个叠加层 (ImageView),以便它稍微移动到包含布局的左边界的左侧。 执行此操作的最佳方法是什么? 尝试了一些简单的方法,比如将 ImageView 放在布局中并使用负边距 andr
Rx 中是否有一些扩展方法来完成下面的场景? 我有一个开始泵送的值(绿色圆圈)和其他停止泵送的值(簧片圆圈),蓝色圆圈应该是预期值,我不希望这个命令被取消并重新创建(即“TakeUntil”和“Ski
我有一个看起来像这样的数据框(Dataframe X): id number found 1 5225 NA 2 2222 NA 3 3121 NA 我有另一个看起来
所以,我正在尝试制作正则表达式,它将解析存储在对象中的所有全局函数声明,例如,像这样 const a = () => {} 我做了这样的事情: /(?:const|let|var)\s*([A-z0-
我正在尝试从 Intellivision 重新创建 Astro-Smash,我想让桶保持在两个 Angular 之间。我只是想不出在哪里以及如何让这个东西停留在两者之间。 我已经以各种方式交换了函数,
到处检查但找不到答案。 我有这个页面,我使用 INNER JOIN 将两个表连接在一起,获取它们的值并显示它们。我有这个表格,用来获取变量(例如开始日期、结束日期和卡号),这些变量将作为从表中调用值的
我陷入了两个不同的问题/错误之间,无法想出一个合适的解决方案。任何帮助将不胜感激 上下文、FFI 和调用大量 C 函数,并将 C 类型包装在 rust 结构中。 第一个问题是ICE: this pat
我在 MySQL 中有一个用户列表,在订阅时,时间戳是使用 CURRENT_TIMESTAMP 在数据库中设置的。 现在我想从此表中选择订阅日期介于第 X 天和第 Y 天之间的表我尝试了几个查询,但不
我的输入是开始日期和结束日期。我想检查它是在 12 月 1 日到 3 月 31 日之间。(年份可以更改,并且只有在此期间内或之外的日期)。 到目前为止,我还没有找到任何关于 Joda-time 的解决
我正在努力了解线程与 CPU 使用率的关系。有很多关于线程与多处理的讨论(一个很好的概述是 this answer )所以我决定通过在运行 Windows 10、Python 3.4 的 8 CPU
我正在尝试编写 PHP 代码来循环遍历数组以创建 HTML 表格。我一直在尝试做类似的事情: fetchAll(PDO::FETCH_ASSOC); ?>
我是一名优秀的程序员,十分优秀!