【发布时间】:2018-03-23 20:21:44
【问题描述】:
我做错了什么?我无法将我的数据从我在列表中单击它的位置传输到单击打开的详细视图活动。
这是我单击按钮时的代码...由于列表显示了项目,因此数据在此处可用。
RecipeRepo repo = new RecipeRepo(this);
final ArrayList<HashMap<String, String>> recipeList = repo.getRecipeList();
if(recipeList.size()!=0) {
ListView lv = (ListView) findViewById(R.id.list);//getListView();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
recipe_Id = (TextView) view.findViewById(R.id.recipe_Id);
String recipeId = recipe_Id.getText().toString();
Intent objIndent = new Intent(getApplicationContext(),RecipeDetail.class);
objIndent.putExtra("recipe_Id", Integer.parseInt( recipeId));
startActivity(objIndent);
}
});
ListAdapter adapter = new SimpleAdapter( SousChef.this,recipeList, R.layout.view_recipe_entry, new String[] { "id","name"}, new int[] {R.id.recipe_Id, R.id.recipe_list_name});
lv.setAdapter(adapter);
}else {
Toast.makeText(this, "No recipe!", Toast.LENGTH_SHORT).show();
}
就像我说的,我现在数据可用,因为列表视图显示了每个项目的 ID 和名称。
使用以下代码单击它后...活动为空白,下面项目中的 toast 出现 ID 0
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipe_detail);
btnEdit = (Button) findViewById(R.id.detail_edit);
btnClose = (Button) findViewById(R.id.detail_close);
textName = (EditText) findViewById(R.id.detail_recipe_name);
textIngredients = (EditText) findViewById(R.id.detail_recipe_ingredients);
textInstruct = (EditText) findViewById(R.id.detail_recipe_instruct);
textCookTemp = (EditText) findViewById(R.id.detail_cook_temp);
textCookTime = (EditText) findViewById(R.id.detail_recipe_cooktime);
textServes = (EditText) findViewById(R.id.detail_recipe_servings);
btnEdit.setOnClickListener(this);
btnClose.setOnClickListener(this);
_Recipe_Id =0;
Intent intent = getIntent();
_Recipe_Id = intent.getIntExtra("recipe_Id", 0);
RecipeRepo repo = new RecipeRepo(this);
Recipe recipe = new Recipe();
recipe = repo.getRecipeById(_Recipe_Id);
textName.setText(recipe.name);
Toast.makeText(this, "Recipe id is " + recipe.recipe_Id, Toast.LENGTH_SHORT).show();
textIngredients.setText(recipe.ingredients);
textInstruct.setText(recipe.instructions);
textCookTemp.setText(recipe.cooktemp);
textCookTime.setText(recipe.cooktime);
textServes.setText(recipe.serves);
从我读过的所有内容来看,这应该是可行的,但我必须遗漏一些东西。我也没有在 logCat 或任何东西中产生任何错误。
【问题讨论】:
-
经过更多研究后,我发现 _Recipe_Id 正在读取正确的数字,但它没有被转移到配方中。因此它给我的 recipe.recipe_id 一个数字 0。想法?
-
好的,问题出在我的 getRecipeById 代码中,关于从哪一列读取的一个简单错误...问题已通过将其更改为从 ID 列读取来解决
-
如果问题解决了,您可以添加自己的答案并接受。
-
好吧,我可以在几天内接受它,哈哈
标签: java android database sqlite data-transfer