为什么我添加了数据,主页不显示?
//主页代码 public class MainActivity extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) { ······ //自定义标题栏 titleBarLayout = findViewById(R.id.title_add_layout); ······ //自定义标题栏添加按钮监听 titleBarLayout.addBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //将类型列表传入,跳转到添加页面 Intent intent = new Intent(MainActivity.this,AddDishActivity.class); intent.putExtra("types",groupList); startActivityForResult(intent,0); } }); } //获取从添加页返回的数据 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode==0) { if (resultCode==1) { //将数据添加到数据库 Dish dish = data.getParcelableExtra("dish"); ContentValues values = new ContentValues(); values.put("dish_id", dish.getDishId()); values.put("dish_type", dish.getDishType()); values.put("dish_name", dish.getDishName()); getContentResolver().insert(Uri.parse("content://com.imooc.menuprovider"), values); } } } ······ } //添加页代码 public class AddDishActivity extends AppCompatActivity { private Spinner typeSpinner; private EditText dishNameEt; private Button saveDishNameBtn; private ArrayList<String> typeList; private Dish dish; private String newDishName; private TitleBarLayout titleBarLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_add_dish); titleBarLayout = findViewById(R.id.title_back_layout); titleBarLayout.addBtnGone(); typeSpinner = findViewById(R.id.type_spinner); dishNameEt = findViewById(R.id.dish_name_et); saveDishNameBtn = findViewById(R.id.save_dishName_btn); typeList = getIntent().getStringArrayListExtra("types"); ArrayAdapter mSpinnerAdapter = new ArrayAdapter<>(this,android.R.layout.simple_spinner_item,typeList); mSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); typeSpinner.setAdapter(mSpinnerAdapter); typeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { //新建对象,从spinner获取id和type dish = new Dish(); dish.setDishId(i+1); dish.setDishType(typeList.get(i+1)); } @Override public void onNothingSelected(AdapterView<?> adapterView) { } }); //保存按钮的监听事件 saveDishNameBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { newDishName = dishNameEt.getText().toString().trim(); dish.setDishName(newDishName); if (!newDishName.isEmpty()) { Intent intent = new Intent(); intent.putExtra("dish",dish); setResult(1,intent); Toast.makeText(AddDishActivity.this, "保存成功", Toast.LENGTH_SHORT).show(); } } }); //返回按钮的监听事件 titleBarLayout.backBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { finish(); } }); } }
7
收起
正在回答 回答被采纳积分+1
2回答
3.Android 高级应用与Kotlin综合实战
- 参与学习 人
- 提交作业 116 份
- 解答问题 1012 个
本阶段是提升项目经验的必备,除Android开发的高级控件,还有Android官方大力推荐的开发语言Kotlin,未来Android发展的方向,最后使用Kotlin来开发热门电商项目。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星