所以我在过去的10个小时里一直在处理这个问题,我根本无法绕过它。我确实看到这类问题的大多数答案都是将列表视图的子项设置为不可点击且不可聚焦。
我已经尝试了很多这样的变化,即使我很惊讶它并没有解决我的问题。
所以这里有一些代码:
public class IngredientEdit extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ingredient_edit);
}
@Override
protected void onStart() {
super.onStart();
@SuppressWarnings("unchecked")
ArrayList<Ingredient> arrayList = (ArrayList<Ingredient>)this.getIntent().getSerializableExtra("RECORDS");
ListViewBaseAdapter listViewBaseAdapter = new ListViewBaseAdapter(arrayList);
final ListView listView = (ListView)this.findViewById(R.id.activity_ingredient_edit_listview);
listView.setAdapter(listViewBaseAdapter);
listView.setClickable(true);
listView.setFocusable(true);
listView.setFocusableInTouchMode(true);
listView.setItemsCanFocus(true);
Log.v("SCHEMA", "BEFORE THE CLICK");
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view ,int position, long id) {
Log.v("SCHEMA", "onItemClick fired!");
Ingredient myIngredient = new Ingredient();
myIngredient = (Ingredient)listView.getItemAtPosition(position);
Intent intent = new Intent(IngredientEdit.this,IngredientEdit2.class);
intent.putExtra("INGREDIENT", myIngredient.getName());
IngredientEdit.this.startActivity(intent);
}
});
}
public class ListViewBaseAdapter extends BaseAdapter {
private ArrayList<Ingredient> arrayList;
public ListViewBaseAdapter(ArrayList<Ingredient> arrayList) {
this.arrayList = arrayList;
}
@Override
public int getCount(){
return this.arrayList.size();
}
@Override
public Object getItem(int position){
return this.arrayList.get(position);
}
@Override
public long getItemId(int position){
return (long)position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_listview,null);
TextView nameTextView = (TextView)convertView.findViewById(R.id.layout_listview_ingredient_name);
nameTextView.setText(this.arrayList.get(position).getName());
nameTextView.setClickable(false);
nameTextView.setFocusable(false);
nameTextView.setFocusableInTouchMode(false);
TextView typeTextView = (TextView)convertView.findViewById(R.id.layout_listview_ingredient_type);
typeTextView.setText(this.arrayList.get(position).getType());
typeTextView.setClickable(false);
typeTextView.setFocusable(false);
typeTextView.setFocusableInTouchMode(false);
TextView descTextView = (TextView)convertView.findViewById(R.id.layout_listview_ingredient_desc);
descTextView.setText(this.arrayList.get(position).getDesc());
descTextView.setClickable(false);
descTextView.setFocusable(false);
descTextView.setFocusableInTouchMode(false);
convertView.setClickable(false);
convertView.setFocusable(false);
convertView.setFocusableInTouchMode(false);
return convertView;
}
@Override
public boolean areAllItemsEnabled()
{
return true;
}
@Override
public boolean isEnabled(int arg0)
{
return true;
}
}
}
这个课程基本上是我的listview活动,我只是想进入下一个活动。
我也应该为此发布一些XML代码......
以下是listview布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false"
android:focusable="false" >
<LinearLayout
android:id="@+id/layout_listview_ingredient_nametype"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:clickable="false"
android:focusable="false"
>
<TextView android:id="@+id/layout_listview_ingredient_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:clickable="false"
android:focusable="false" />
<TextView android:id="@+id/layout_listview_ingredient_type"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:clickable="false"
android:focusable="false"/>
</LinearLayout>
<TextView android:id="@+id/layout_listview_ingredient_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/layout_listview_ingredient_nametype"
android:clickable="false"
android:focusable="false"/>
</RelativeLayout>
这是通过以下方式实现的:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".IngredientSearch" >
<ListView android:id="@+id/activity_ingredient_edit_listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true" />
</RelativeLayout>
我试图取下并取出的可点击和可聚焦按钮。各种组合都不起作用,但此时我预计会出现问题。
最后,我想你想看看我的成分课,因为我经常使用它:
public class Ingredient implements Serializable{
private static final long serialVersionUID = 5L;
private String name;
private String type;
private String desc;
public Ingredient() {
}
public void setName(String name){
this.name=name;
}
public String getName() {
return this.name;
}
public void setType (String type) {
this.type = type;
}
public String getType () {
return this.type;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getDesc() {
return this.desc;
}
}
更新:IngredientEdit2类:
public class IngredientEdit2 extends Activity {
private EditText nameEditText;
private Spinner typeSpinner;
private EditText descEditText;
protected boolean changesPending;
private AlertDialog unsavedChangesDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ingredient_insert);
}
@Override
protected void onStart() {
super.onStart();
this.nameEditText = (EditText)this.findViewById(R.id.ingredient_add_name_edittext);
this.typeSpinner = (Spinner)this.findViewById(R.id.ingredient_add_type_spinner);
this.descEditText = (EditText)this.findViewById(R.id.ingredient_add_desc_edittext);
//The following will add the String array to the spinner
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.ingredient_add_type_spinner_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
typeSpinner.setAdapter(adapter);
typeSpinner.setOnItemSelectedListener(new IngredientOnItemSelectedListener());
//End of spinner code
String ingredientName = null; //used to keep track of the selected ingredient
Bundle position = getIntent().getExtras();
if (position != null) {
ingredientName = position.getString("INGREDIENT");
this.nameEditText.setText(ingredientName);
}
IngredientDataSource ingredientDataSource = new IngredientDataSource(IngredientEdit2.this.getApplicationContext());
Ingredient myIngredient = new Ingredient();
myIngredient = ingredientDataSource.selectEditableIngredient(ingredientName);
String theType = myIngredient.getType(); //used to put the type in the spinner
String[] typeHelper = {"Meat","Poultry","Fish","Dairy","Vegetables","Fruit","Nuts","Spices/Herbs","Grains/Flours"};
for (int i=0; i<typeHelper.length; i++){
if (theType == typeHelper[i]) typeSpinner.setSelection(i);
}
descEditText.setText(myIngredient.getDesc());
}
protected void updateIngredient(){
Ingredient ingredient = new Ingredient();
ingredient.setName(IngredientEdit2.this.nameEditText.getText().toString());
ingredient.setType(String.valueOf(typeSpinner.getSelectedItem()));
ingredient.setDesc(IngredientEdit2.this.descEditText.getText().toString());
IngredientDataSource ingredientDataSource = new IngredientDataSource(IngredientEdit2.this.getApplicationContext());
if(ingredientDataSource.update(ingredient)>0) {
Toast toast = Toast.makeText(
IngredientEdit2.this.getApplicationContext(),
"Your new ingredient has been updated",
Toast.LENGTH_LONG);
toast.show();
}
}
}
所以我的问题是我无法点击任何项目。当我触摸它时,我看到它突出显示,但它没有发出咔哒声,并且LogCat中没有显示错误。显示的信息很好。有什么办法可以弄清楚为什么我实际上无法发送点击并让onItemClick做它应该做的事情?
干杯!