首先,如果已经有人提出并回答了类似的问题,我深表歉意,但我已经浏览了大量的帖子,但没有任何运气。其次,我是一个绝对的初学者(已经编码了 6 周),所以如果我遗漏了一些概念或者如果有更好的方法来做事情,请随时启发我。
现在,这是我的问题: 在我的 MainActivity 上,我有一个 EventsFragment,它有一个 ListView。在 MainActivity 上还有一个 FloatingActionBar,单击它会加载另一个名为 CreateEventActivity 的 Activity 。 CreateEventActivity 有一个 EditText 字段和一个创建按钮。我想做的是当用户在 EditText 字段中输入一些文本并单击创建按钮时,它应该从 EditText 字段中获取字符串并在 ListView 上创建一个项目,名称为 String。此外,单击创建按钮后,它应该立即返回到 MainActivity 中的 EventsFragment。
我按照建议使用了 startActivityForResults(),似乎我能够将 ArrayList 作为 Intent 从 CreateEventActivity 传递回 MainActivity,这是我所能得到的。我认为我将列表发送到 EventsFragment 的方式是错误的,因为 listView 没有更新。
主要 Activity -
public class MainActivity extends AppCompatActivity {
/**
* 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}.
*/
private SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), MainActivity.this);
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
//Iterate over all TabLayouts to set the custom View
for (int i = 0; i < tabLayout.getTabCount(); i++){
TabLayout.Tab tab = tabLayout.getTabAt(i);
tab.setCustomView(mSectionsPagerAdapter.getTabView(i));
}
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
loadCreateEventActivity();
// Snackbar.make(view, "Here the create a New Event Screen will be displayed", Snackbar.LENGTH_LONG)
// .setAction("Action", null).show();
}
});
}
private void loadCreateEventActivity(){
Intent intent = new Intent(MainActivity.this, CreateEventActivity.class);
startActivityForResult(intent, 2);
}
public ArrayList<String>list = new ArrayList<>();
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 2){
if (resultCode == RESULT_OK) {
list = data.getStringArrayListExtra("LIST_ITEMS");
}
}
}
public ArrayList<String> getList() {
return list;
}
创建事件 Activity -
public class CreateEventActivity extends AppCompatActivity {
EditText createEventName;
Button createButton;
ArrayList<String> listItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_event);
createEventName = (EditText) findViewById(R.id.createEventName);
createButton = (Button) findViewById(R.id.createButton);
listItems = new ArrayList<String>();
listItems.add("First Item - added on Activity Create");
createButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
listItems.add(createEventName.getText().toString());
Intent intent = new Intent();
intent.putStringArrayListExtra("LIST_ITEMS", listItems);
setResult(RESULT_OK, intent);
createEventName.setText("");
finish();
}
});
}
}
事件 fragment -
public class EventsFragment extends Fragment {
public EventsFragment() {
// Required empty public constructor
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_events, container, false);
MainActivity mainActivity = (MainActivity) getActivity();
ArrayList<String>list = mainActivity.getList();
ListView listView = (ListView)view.findViewById(R.id.list_view);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getApplicationContext(),android.R.layout.simple_list_item_1, list);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
return view;
}
}
提前感谢您的帮助:)
最佳答案
解决方案 1: 您需要有一个数据集,这对两个 Activity 都是通用的。 每次单击时,都会向该数据集添加一个项目。 使用完全相同的数据集填充 ListView 。 由于它们都在不同的 Activity 中,因此您需要保留该数据集。
您可以通过使用 table(database) 存储它来在本地持久保存它或者作为 json
字符串,在 [shared preference][1]
中。
持久化数据集的另一个捷径是将数据集作为 Application
类中的属性,这样您就可以从任何 activity
添加和访问数据集在应用程序中,因为 application
类在应用程序的整个生命周期中都处于 Activity 状态。
我不推荐这个。这很简单,也很有效,但这不是好的做法,会导致糟糕的代码质量,但如果您只是想要一种快速的方法,您可以做到。
解决方案 2:在您的主要 Activity 中,使用 startActivityForResult()
启动第二个 Activity .这会让你做的是让第二个 Activity 与第一个 Activity 对话。因此,当它回复时,您可以在第一个中知道在第二个中添加了多少项目,并相应地更新您的第一个 Activity 的 listView。
关于java - 通过单击另一个 Activity 中的按钮将项目添加到一个 Activity 中的 fragment 中的 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44764781/