我有以下XML布局:
目前我只能点击EditText下面的“添加”按钮,即使我已经从扩展ListActivity超类中实现并覆盖了'OnListViewClick'方法。
我认为当使用带有ListView的按钮时,这是一个焦点问题。
希望有人能告诉我如何设置XML布局,以便点击按钮和ListView。
OnItemListClick方法代码:
package com.example.flybase2;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
public class ShoppingList extends ListActivity implements OnClickListener {
Button AddItem;
ListView showItems;
SimpleCursorAdapter cursorAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.shoppinglistlayout);
AddItem = (Button) findViewById(R.id.btnAddItem);
showItems = (ListView)findViewById(android.R.id.list);
AddItem.setOnClickListener(this);
setList();
}
@Override
public void onClick(View clickedAdd) {
AlertDialog.Builder builder = new AlertDialog.Builder(ShoppingList.this);
builder.setTitle("Enter Item Details:");
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
final EditText titleBox = new EditText(this);
titleBox.setHint("Item Name:");
layout.addView(titleBox);
final EditText quantityBox = new EditText(this);
quantityBox.setHint("Item Quantity");
layout.addView(quantityBox);
final EditText priceBox = new EditText(this);
priceBox.setHint("Item Price.");
layout.addView(priceBox);
builder.setView(layout);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
try
{
Editable valueItem = titleBox.getText();
Editable valueAmount = quantityBox.getText();
Editable valuePrice = priceBox.getText();
String itemDescription = valueItem.toString();
String s = valueAmount.toString();
int itemAmount = Integer.parseInt(s);
String a = valuePrice.toString();
int itemPrice = Integer.parseInt(a);
DBHandlerShop addItem = new DBHandlerShop(ShoppingList.this, null, null);
addItem.open();
addItem.insertItems(itemDescription, itemAmount, itemPrice);
addItem.close();
}
catch(Exception e)
{
Dialog e1 = new Dialog(ShoppingList.this);
e1.setTitle("Item unsuccesfully added");
TextView txt = new TextView(ShoppingList.this);
txt.setText("Success");
e1.setContentView(txt);
e1.show();
}
finally
{
Dialog e = new Dialog(ShoppingList.this);
e.setTitle("Item succesfully added.");
TextView txt = new TextView(ShoppingList.this);
txt.setText("Success");
e.setContentView(txt);
e.show();
setList();
}
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
builder.show();
}
@Override
protected void onListItemClick(ListView l, View v, int position, long idd) {
super.onListItemClick(l, v, position, idd);
final CharSequence[] items = {"Edit Appointment Details", "Delete Appointment"};
Builder alertDialogBuilder = new AlertDialog.Builder(ShoppingList.this);
alertDialogBuilder.setTitle("Appointment Options:");
alertDialogBuilder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals("Edit Appointment Details")) {
}
else if (items[item].equals("Delete Appointment")) {
}
}
});
alertDialogBuilder.show();
}
private void setList() {
DBHandlerShop DBShop = new DBHandlerShop(this, null, null);
DBHandlerShop searchItems = new DBHandlerShop(this, null, null);
searchItems.open();
Cursor cursor = searchItems.getItems();
startManagingCursor(cursor);
String [] from = new String [] {DBShop.KEY_ITEMSHOP, DBShop.KEY_ITEMNUM, DBShop.KEY_ITEMPRICE};
int [] to = new int [] {R.id.txtSetItem, R.id.txtSetAmount, R.id.txtSetPrice};
cursorAdapter = new SimpleCursorAdapter(this, R.layout.setshoppinglist, cursor, from, to);
showItems.setAdapter(cursorAdapter);
}
}
这是我的XML代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="84dp"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imgLink"
android:layout_width="78dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="@drawable/viewcon" />
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="75dp"
android:gravity="center"
android:text="Shopping List"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="35sp" />
</LinearLayout>
<TextView
android:id="@+id/txtItemDes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Item To Add To The List:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/inputAppointName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter A Name" >
<requestFocus />
</EditText>
<Button
android:id="@+id/btnAddItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add" />
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
答案 0 :(得分:1)
在聊天中,您发布了ListView中行的XML。行布局中的一个LinearLayouts是可点击的(android:clickable="true"
),这将消耗触摸事件,阻止其到达onListItemClick()
。只需删除此行即可。
<强>加成强>
我建议采用略有不同的方法。很明显,您需要一个可检查的自定义行布局(通过单击行上的任意位置)。我在前一段时间发布了详细的答案:CheckedTextView checkmark in ListView row not showing up,此处有一个vague tutorial(您需要点击指向“Begemot”库的链接。)