單擊某個項目時單擊偵聽器無法工作。ListView setOnItemClickListener不會觸發
在這個程序中前兩個arraylist使用update()更新。那麼這個列表視圖會發送到其他類以進行列表視圖。但點擊每個項目不會敬酒任何東西。
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"/>
列表項是「detail.xml」嗎? – JoeyJubb
在您的ListView中添加'android:focusable =「false」 android:clickable =「false」' –
放入項目佈局android:focusable =「false」 – Pavan