java - 我找不到从列表中选择项目的方法,每个项目由三个文本字段组成
问题描述
我查看了数十个示例和教程,并尝试重复相同的步骤,但它不起作用。
在我的活动中,我有四个 onClickListener,它们工作得非常好,并且与布局顶部的四个按钮相关。此外,当我尝试在同一布局的底部为 ListView 的项目设置 onItemClickListener 时,就会出现问题。这个 ListView 的每一项都由三个 textView 组成,我为它们编写了一个自定义适配器。它们完美地显示出来,但是当我单击它们时,什么也没有发生(永远无法达到 onItemClick 指令)。
这是布局,问题在于最后的“ingredients_list_view”:(顺便说一句,最后一项,@android:id/list”,只是因为否则我的活动中会出现编译错误,我还没有弄清楚为什么 - 否则我不需要它,而且它根本不应该显示或做任何事情)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/label_recipe_name"
android:layout_gravity="top"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textCapCharacters"
android:id="@+id/editTextTitle"
android:layout_gravity="top"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/label_servings"
android:layout_gravity="top"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textCapCharacters"
android:id="@+id/editTextServings"
android:layout_gravity="top"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/label_cake_size"
android:layout_gravity="top"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textCapCharacters"
android:id="@+id/editTextSize"
android:layout_gravity="top"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/btnEditDelete"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/button_edit"
android:id="@+id/btnEdit"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/button_delete"
android:id="@+id/btnDelete"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0"
android:id="@+id/btnSaveCancel"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/button_save"
android:id="@+id/btnSave"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/button_cancel"
android:id="@+id/btnCancel"/>
</LinearLayout>
</LinearLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ingredients_list_view"
android:divider="#48C"
android:dividerHeight="1dp"
android:layout_weight="1" />
<ListView
android:layout_width="match_parent"
android:layout_height="1dp"
android:id="@android:id/list"/>
</LinearLayout>
这是 listView 每个项目的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/ingredient_list_item"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:paddingLeft="6dip"
android:paddingTop="6dip"
android:textSize="22sp"
android:textStyle="bold" />
<EditText
android:id="@+id/quantity_list_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="6dip"
android:paddingTop="6dip"
android:textSize="22sp"
android:textStyle="bold" />
<EditText
android:id="@+id/unit_list_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="6dip"
android:paddingTop="6dip"
android:textSize="22sp"
android:textStyle="bold" />
</LinearLayout>
...这些是主要活动的相关部分:
public class Recipe extends ListActivity implements View.OnClickListener {
public static final String TABLE = "Recipes";
public static final String KEY_ID = "recipeID";
public static final String KEY_title = "title";
public static final String KEY_servings = "servings";
public static final String KEY_size = "size";
public int recipeID;
public String title;
public String servings;
public String size;
Button btnEdit, btnDelete, btnSave, btnCancel;
LinearLayout btnEditDelete, btnSaveCancel;
EditText editTextTitle;
EditText editTextServings;
EditText editTextSize;
int _Recipe_Id;
int editMode;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipe_view);
redraw();
}
@Override
public void onClick(View view) {
// These work perfectly fine (except for the last one)
RecipeDBOps repo = new RecipeDBOps(this);
ArrayList<HashMap<String, String>> recipeList = repo.getRecipeList(); // so far not used
if (view == findViewById(R.id.btnDelete)) {
MixDBOps.delete(_Recipe_Id);
RecipeDBOps.delete(_Recipe_Id);
finish();
}
else if (view == findViewById(R.id.btnEdit)) { // Changes the fields to "editable" and switches button
setEditMode();
}
else if (view == findViewById(R.id.btnCancel)) {
finish();
}
else if (view == findViewById(R.id.btnSave)){
Recipe recipe = RecipeDBOps.getRecipeById(_Recipe_Id);
recipe.title = editTextTitle.getText().toString();
recipe.servings = editTextServings.getText().toString();
recipe.size = editTextSize.getText().toString();
RecipeDBOps.update (recipe);
finish();
}
else if (view == findViewById(R.id.ingredients_list_view)){
System.out.println("OK2"); // Tried, but doesn't work
}
}
public void setWeight (LinearLayout ll, LinearLayout.LayoutParams visibility){...}
public void setReadOnlyMode(){...}
public void setEditMode(){...}
public void redraw(){
editTextTitle = (EditText) findViewById(R.id.editTextTitle);
editTextServings = (EditText) findViewById(R.id.editTextServings);
editTextSize = (EditText) findViewById(R.id.editTextSize);
btnEdit = (Button) findViewById(R.id.btnEdit);
btnDelete = (Button) findViewById(R.id.btnDelete);
btnSave = (Button) findViewById(R.id.btnSave);
btnCancel = (Button) findViewById(R.id.btnCancel);
btnEditDelete = (LinearLayout) findViewById(R.id.btnEditDelete);
btnSaveCancel = (LinearLayout) findViewById(R.id.btnSaveCancel);
setReadOnlyMode();
//
// Populate recipe fields
//
Intent intent = getIntent();
_Recipe_Id = intent.getIntExtra("recipe_ID", 0);
editMode = intent.getIntExtra("editMode", 0);
RecipeDBOps repo = new RecipeDBOps(this);
Recipe recipe;
recipe = repo.getRecipeById(_Recipe_Id);
if (recipe.title != null) {
editTextTitle.setText(recipe.title);
editTextServings.setText(recipe.servings);
editTextSize.setText(recipe.size);
}
//
// Populate recipe and ingredient fields
//
ArrayList<Entry> arrayOfEntries = new ArrayList<Entry>();
EntryAdapter adapter = new EntryAdapter(this, arrayOfEntries);
ListView listView = (ListView) findViewById(R.id.ingredients_list_view);
listView.setAdapter(adapter);
////////////////////////////////////////////////////////////////////////////////////
// This is the part that does not work
listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ListView clickedObj = (ListView) parent.getItemAtPosition(position);
// Once I am able to activate this onItemClick I will write the rest of the code to deal with clickdObj
System.out.println("OK1");
}});
////////////////////////////////////////////////////////////////////////////////////
HashMap<String, String> h;
MixDBOps repoMix = new MixDBOps(this); // repo represents the DB
ArrayList<HashMap<String, String>> entryList = repoMix.getMixById(_Recipe_Id); // recipeList is the output of the SQL query
Entry[] ent = new Entry[entryList.size()];
for (int i = 0; i < entryList.size(); i++) {
h = entryList.get(i);
ent[i] = new Entry();
ent[i].recipe_name = recipe.title;
ent[i].ingredient_name = h.get("ingredient_name"); // temporarily replaced by spinner
ent[i].quantity= Integer.parseInt(h.get("quantity"));
ent[i].unit_name= h.get("unit_name");
adapter.add(ent[i]);
}
btnEdit = (Button) findViewById(R.id.btnEdit);
btnEdit.setOnClickListener(this);
btnDelete = (Button) findViewById(R.id.btnDelete);
btnDelete.setOnClickListener(this);
btnSave = (Button) findViewById(R.id.btnSave);
btnSave.setOnClickListener(this);
btnCancel = (Button) findViewById(R.id.btnCancel);
btnCancel.setOnClickListener(this);
if (editMode == 1){
setEditMode();
}
}
}
有人可以帮助我吗?
解决方案
在列表视图项目的每个编辑文本中添加两个属性
android:focusable="false"
android:clickable="false"
推荐阅读
- python - 如何在 jupyter notebook 中逐行绘制 4000 张图像?
- python - 为什么我要使用 function: 'items' 来处理 Python 中的嵌套字典?
- amazon-web-services - AppSync Resolver 不会为可空数组字段返回空值,但 [null]
- node.js - 简单的 Nodejs MongoDB Express 迭代/循环集合
- javascript - 在叠加层内平滑滚动
- html - Rad 向导控件中的两个验证组
- python - 循环以及如何检查输入是否为数字
- linux - 从已安装的 cd 光盘递归下载文件的问题
- regex - 正则表达式 PCRE 字段提取
- c++ - 如何将 C++ 连接到 Matlab