[英]How to save the data in my ListView from the first Activity to the second Activity?
I am writing an app for a restaurant and would like to be able to select a specific dish that you would like to order and that it has been added to OrderActivity where, in the form of ListView, you will be displaying individual dishes selected by the user. 我正在为一家餐厅编写应用程序,希望能够选择要订购的特定菜肴,并且该菜肴已添加到OrderActivity中,在其中,您将以ListView的形式显示由菜单选择的菜肴用户。
I do not know how to do it in the best way, do you need to use the interface and maybe just get the intention of a specific dish? 我不知道如何以最好的方式做到这一点,您是否需要使用该界面并且可能只是出于特定菜肴的意图?
And how do I save a specific request in OrderActivity so that when I return to an earlier Activity I do not lose the saved data in the ListView? 以及如何在OrderActivity中保存特定的请求,以便当我返回到较早的Activity时,不会丢失ListView中保存的数据?
I managed to solve the problem of transmitting data from one Activity to the second Activity and showing it on the ListView, I do not know how to save that data, say on the example of SharedPreferences? 我设法解决了将数据从一个Activity传输到第二个Activity并将其显示在ListView上的问题,例如在SharedPreferences的示例中,我不知道如何保存该数据?
If I click the back button in my second Activity, my list becomes empty. 如果在第二个活动中单击“后退”按钮,则列表将为空。
I understand that the fault is on onResume()
because I am killing the second Activity, when I come back to the first, is that so? 我知道错误在于
onResume()
因为我要杀死第二个Activity,回到第一个Activity时,是这样吗?
How to solve the problem? 如何解决问题?
FirstActivity: FirstActivity:
public class DinnerDetailActivity extends AppCompatActivity {
public static final String EXTRA_DINNER = "dinner";
private final int requestCode = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_obiady_domowe_detail);
int dinner = (Integer) getIntent().getExtras().get(EXTRA_DINNER);
String dinnerName = Dinner.dinn[dinner].getName();
TextView textView = (TextView) findViewById(R.id.dinner_text);
textView.setText(dinnerName);
int dinnerImage = Dinner.dinn[dinner].getImageResourceId();
ImageView imageView = (ImageView) findViewById(R.id.dinner_image);
imageView.setImageDrawable(getResources().getDrawable(dinnerImage));
imageView.setContentDescription(dinnerName);
Toolbar myChildToolbar = (Toolbar)
findViewById(R.id.my_child_toolbar_obiady_detail);
setSupportActionBar(myChildToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
/* @Override
protected void onResume() {
SharedPreferences sharedPref = getSharedPreferences("KEY", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("KEY", listItems.add();
editor.apply();
Toast.makeText(getApplicationContext(), "Save!", Toast.LENGTH_SHORT).show();
super.onResume();
}
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
TextView textView = (TextView) findViewById(R.id.dinner_text);
CharSequence dinnerName = textView.getText();
MenuItem menuItem = menu.findItem(R.id.action_share);
ShareActionProvider shareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, dinnerName);
shareActionProvider.setShareIntent(intent);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_create_order:
Intent intent = new Intent(this, TopFragment.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
//Click button, and add dinnerName to SecondActivity ListView //单击按钮,然后将DinnerName添加到SecondActivity ListView
public void addInOrder(View view) {
int dinner = (Integer) getIntent().getExtras().get(EXTRA_DINNER);
String dinnerName = Dinner.dinn[dinner].getName();
Intent intent1 = new Intent(this, CreateYourOrderActivity.class);
intent1.putExtra("OK", dinnerName);
startActivityForResult(intent1, requestCode);
}
}
Second Activity: 第二项活动:
public class CreateYourOrderActivity extends AppCompatActivity {
private ListView listView;
private ArrayAdapter<String> adapter;
private ArrayList<String> listItems;
private String dinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zloz_zamowienie);
Toolbar myChildToolbar = (Toolbar)
findViewById(R.id.my_child_toolbar);
setSupportActionBar(myChildToolbar);
ActionBar ab = getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(true);
textView = (TextView) findViewById(R.id.text_view);
}
/*
@Override
protected void onResume() {
SharedPreferences sharedPref = getSharedPreferences("KEY", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("KEY", obiad);
editor.apply();
listItems.add(obiad);
adapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), "Save!", Toast.LENGTH_SHORT).show();
super.onResume();
}
*/
/* @Override
public void onBackPressed() {
SharedPreferences sharedPref = getSharedPreferences("KEY", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("KEY", listItems.get(0).toString());
editor.apply();
Toast.makeText(getApplicationContext(), "Save!", Toast.LENGTH_SHORT).show();
}*/
public void saveInfo(View view) {
}
public void openInfo(View view) {
SharedPreferences sharedPref = getSharedPreferences("KEY", Context.MODE_PRIVATE);
String obiadNames = sharedPref.getString("KEY", "");
textView.setText(obiadNames);
//listItems.add(obiadNames);
//adapter.notifyDataSetChanged();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_create_order:
Intent intent = new Intent(this, MainActivity.class);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Intent intent = getIntent();
if(resultCode == RESULT_OK) {
if(requestCode == 1){
dinner = data.getExtras().getString("OK");
listView = (ListView) findViewById(R.id.listView);
listItems.add(dinner);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems);
listView.setAdapter(adapter);
// adapter.add(dinner);
adapter.notifyDataSetChanged();
finish();
}
}
}
So as you can see in the attached photos we have the first Activity, several dishes from which we go to the detailed Activity, where we have AddOrder Button. 因此,如您在所附照片中看到的那样,我们有第一个活动,从几道菜我们可以转到详细的活动,这里有AddOrder按钮。
I would like to click on this button to add the name of my specific dish in the 3 Activities that you see in the pictures. 我想单击此按钮以在您在图片中看到的3个活动中添加我的特定菜肴的名称。
This is to be added as a ListView. 这将被添加为ListView。
Also, I would like to have the names of dishes not gone when I return to 1 Activity. 另外,当我返回1 Activity时,我不想让菜名消失。
SharedPreferences are for simple values. SharedPreferences用于简单值。
You'll need a proper database (Sqlite, Realm, or other) with which you can persistently store and query your data across the entire application without passing entire objects across Intent boundaries. 您将需要一个适当的数据库(Sqlite,Realm或其他数据库),使用该数据库可以在整个应用程序中持久存储和查询数据,而无需跨Intent边界传递整个对象。
More specifically, you need to replace the Dinner.dinn
array 更具体地说,您需要替换
Dinner.dinn
数组
To get specific items when you go to a detail view, you can pass the database ID of the object, then query it later. 要在进入详细信息视图时获取特定的项目,可以传递对象的数据库ID,然后在以后查询。
When you add a new item and go back to the list, you will update the adapter with all the database entries 添加新项并返回列表时,将使用所有数据库条目更新适配器
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.