ListView setOnItemClickListener 不触发
ListView setOnItemClickListener Does Not Trigger
当我点击一个项目时点击侦听器不工作。
在此程序中,前两个数组列表使用 update() 更新。然后将此列表视图发送给其他 class 以制作列表视图。但是点击每个项目并没有烤任何东西。
public class BlockActivity extends Activity {
ListView lv;
ArrayList<String> contactnumber= new ArrayList<String>();
ArrayList<String> contactname= new ArrayList<String>();
public static SQLiteDatabase database;
SQLiteDatabase myDB= null;
CustomAdapter adapter;
ArrayList<String> contactnum= new ArrayList<String>();
ArrayList<String> contactnam= new ArrayList<String>();
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button pickContact = (Button) findViewById(R.id.button1);
update();
lv=(ListView) findViewById(R.id.listView1);
adapter = new CustomAdapter(this, contactnam, contactnum);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
/*new AlertDialog.Builder(view.getContext())
.setMessage("Something here")
.setNegativeButton("Close", null).show();*/
Toast.makeText(BlockActivity.this,
"Item in position " + position + " clicked", Toast.LENGTH_LONG).show();
}
});
pickContact.setOnClickListener(new OnClickListener() {
.
.
.
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="add" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
detail.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" android:focusableInTouchMode="false" android:focusable="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dip"
android:layout_weight="1" android:focusable="false" android:focusableInTouchMode="false">
<Button android:id="@+id/button2" android:layout_width="0dp" android:layout_height="wrap_content" android:text="Button"/>
<TextView
android:id="@+id/one"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="name"
android:textSize="20dip" android:layout_gravity="center" android:focusable="false" android:focusableInTouchMode="false"/>
<TextView
android:id="@+id/two"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="name"
android:textSize="20dip" android:layout_gravity="center" android:focusable="false" android:focusableInTouchMode="false"/>
</LinearLayout>
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="2dip" android:background="#ff7f7f"/>
如果您在 CustomAdapter class 中为 detail.xml 中的 Button 实现了 onClik Listener,请将其删除。
因为您不能同时点击列表视图的项目点击和自定义适配器的视图点击。
那是因为你的 Button
,在你的列表项 root layout
中添加 android:descendantFocusability="blocksDescendants"
detail.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dip"
android:layout_weight="1" android:focusable="false" android:focusableInTouchMode="false">
<Button android:id="@+id/button2" android:layout_width="0dp" android:layout_height="wrap_content" android:text="Button"/>
<TextView
android:id="@+id/one"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="name"
android:textSize="20dip" android:layout_gravity="center"/>
<TextView
android:id="@+id/two"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="name"
android:textSize="20dip" android:layout_gravity="center"/>
</LinearLayout>
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="2dip" android:background="#ff7f7f"/>
对于 detail.xml
中的 TextViews 和 Button 添加这段代码
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
列表视图中可以有多个可点击的视图。
最佳解决方案是:
1)在列表项的顶级容器布局中使用android:descendantFocusability="blocksDescendants"。
2) 现在列表视图中的任何视图需要可点击,使用以下属性:
android:focusable="false"
android:focusableInTouchMode="false"
3)使用片段界面或 activity 点击时将点击侦听器设置为视图和回调。
当我点击一个项目时点击侦听器不工作。
在此程序中,前两个数组列表使用 update() 更新。然后将此列表视图发送给其他 class 以制作列表视图。但是点击每个项目并没有烤任何东西。
public class BlockActivity extends Activity {
ListView lv;
ArrayList<String> contactnumber= new ArrayList<String>();
ArrayList<String> contactname= new ArrayList<String>();
public static SQLiteDatabase database;
SQLiteDatabase myDB= null;
CustomAdapter adapter;
ArrayList<String> contactnum= new ArrayList<String>();
ArrayList<String> contactnam= new ArrayList<String>();
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button pickContact = (Button) findViewById(R.id.button1);
update();
lv=(ListView) findViewById(R.id.listView1);
adapter = new CustomAdapter(this, contactnam, contactnum);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
/*new AlertDialog.Builder(view.getContext())
.setMessage("Something here")
.setNegativeButton("Close", null).show();*/
Toast.makeText(BlockActivity.this,
"Item in position " + position + " clicked", Toast.LENGTH_LONG).show();
}
});
pickContact.setOnClickListener(new OnClickListener() {
.
.
.
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="add" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
detail.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" android:focusableInTouchMode="false" android:focusable="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dip"
android:layout_weight="1" android:focusable="false" android:focusableInTouchMode="false">
<Button android:id="@+id/button2" android:layout_width="0dp" android:layout_height="wrap_content" android:text="Button"/>
<TextView
android:id="@+id/one"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="name"
android:textSize="20dip" android:layout_gravity="center" android:focusable="false" android:focusableInTouchMode="false"/>
<TextView
android:id="@+id/two"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="name"
android:textSize="20dip" android:layout_gravity="center" android:focusable="false" android:focusableInTouchMode="false"/>
</LinearLayout>
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="2dip" android:background="#ff7f7f"/>
如果您在 CustomAdapter class 中为 detail.xml 中的 Button 实现了 onClik Listener,请将其删除。
因为您不能同时点击列表视图的项目点击和自定义适配器的视图点击。
那是因为你的 Button
,在你的列表项 root layout
android:descendantFocusability="blocksDescendants"
detail.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dip"
android:layout_weight="1" android:focusable="false" android:focusableInTouchMode="false">
<Button android:id="@+id/button2" android:layout_width="0dp" android:layout_height="wrap_content" android:text="Button"/>
<TextView
android:id="@+id/one"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="name"
android:textSize="20dip" android:layout_gravity="center"/>
<TextView
android:id="@+id/two"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="name"
android:textSize="20dip" android:layout_gravity="center"/>
</LinearLayout>
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="2dip" android:background="#ff7f7f"/>
对于 detail.xml
中的 TextViews 和 Button 添加这段代码
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
列表视图中可以有多个可点击的视图。 最佳解决方案是:
1)在列表项的顶级容器布局中使用android:descendantFocusability="blocksDescendants"。
2) 现在列表视图中的任何视图需要可点击,使用以下属性:
android:focusable="false"
android:focusableInTouchMode="false"
3)使用片段界面或 activity 点击时将点击侦听器设置为视图和回调。