无法获取notifyDataSetChanged()来更新我的列表视图
问题描述:
我认识到关于已经在stackoverflow上的notifyDataSetChanged()方法有很多问题和答案,但我已经回顾了其中的大部分内容,并且无法弄清楚可能会出现什么问题这里。我试图让我的列表视图在用户点击“Add Ingredient Button”时动态添加更多行。它会在第一次点击后添加第一种成分,但随后的任何点击都不会导致对列表视图的任何更改。任何帮助表示赞赏。无法获取notifyDataSetChanged()来更新我的列表视图
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_recipe);
ButterKnife.bind(this);
mAddInstructionsButton.setOnClickListener(this);
mAddIngredientButton.setOnClickListener(this);
adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, ingredientList);
mListView.setAdapter(adapter);
}
public void onClick(View v) {
if(v == mAddIngredientButton) {
if(mIngredientName.getText().toString().trim().equalsIgnoreCase("") || mIngredientMeasurement.getSelectedItem().toString().trim().equalsIgnoreCase("") || mIngredientCount.getText().toString().trim().equalsIgnoreCase("")) {
Toast answerStatus = Toast.makeText(NewRecipeActivity.this, "Fill out all fields", Toast.LENGTH_LONG);
answerStatus.show();
} else {
String ingredient = createIngredientString();
ingredientList.add(ingredient);
adapter.notifyDataSetChanged();
clearIngredientInputs();
Log.i("NewRecipeActivity", "List includes: " + ingredientList);
}
}
答
你需要新的成分添加到适配器的列表,使用方法
adapter.add(ingredient)
这样
String ingredient = createIngredientString();
adapter.add(ingredient);
adapter.notifyDataSetChanged();
答
哦,孩子,我感到我很傻这一个。代码一直在工作,就像它在我的问题中发布一样。我的活动上的listview元素的高度设置为52像素,这样任何添加到列表视图的行都不会出现。 花了大约4小时阅读文章,并改变了Java文件中的各种东西,这只是显示元素的高度问题。 提示难过的长号声音
显示错误Logcat。 – KeLiuyue
没有错误。 Logcat目前只显示我的成分列表ListArray肯定会被添加到:21476-21476/com.epicodus.myrestaurants I/NewRecipeActivity:列表包括:[3 tsp dsf] 10-14 01:46:00.804 21476-21476/com .epicodus.myrestaurants我/ NewRecipeActivity:列表包括:[3 tsp dsf,4 tsp fdg] 10-14 02:08:25.729 21476-21476/com.epicodus.myrestaurants I/NewRecipeActivity:列表包括:[3 tsp dsf, I/NewRecipeActivity:列表包括:[3 tsp dsf,4 tsp fdg,4 tsp gdfg,6 tsp dsf] [4 tsp fdg,4 tsp gdfg] 10-14 02:08:31.449 21476-21476/com.epicodus.myrestaurants –
您可以尝试[https://stackoverflow.com/questions/3669325/notifydatasetchanged-example](https://stackoverflow.com/questions/3669325/notifydatasetchanged-example) – KeLiuyue