我正在增強我的應用程序從一個簡單的ListActivity到一個更復雜的應用程序,除了ListView我還有一個搜索文本和一個Button。
在簡單的版本中,當我點擊該項目時,監聽器就會獲取它。
更復雜的是,當我點擊該項目時,什麼也沒有發生,並且在調試模式下,我沒有碰到聽衆。不能點擊一個ListView項目
在下面的兩個例子中,我剝離了所有不必要的邏輯,僅僅足以重新創建問題。
工作代碼:
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends ListActivity {
String[] eventList = {"Queen", "Chicago", "Kinks"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Define event list
setListAdapter(new ArrayAdapter<String>
(this, R.layout.activity_main,
R.id.lvEventList, eventList));
}
// Process List Item selection
protected void onListItemClick(ListView l, View v, int position, long id){
Context context = MainActivity.this;
Toast.makeText(context, "item " + position + " clicked", Toast.LENGTH_LONG).show();
}
}
和相應的佈局
<?xml version="1.0" encoding="utf-8"?>
<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=".EventList">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/lvEventList"
android:textSize="16sp"
android:text="@+id/txtEventName"
/>
</RelativeLayout>
不工作的代碼
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
String[] eventList = {"Queen", "Chicago", "Kinks"};
ListView listView;
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Define event list
listView = (ListView)findViewById(R.id.lvEventList);
adapter = new ArrayAdapter<String>(this, R.layout.custom_list_text,
eventList);
listView.setAdapter(adapter);
}
// Process List Item selection
protected void onListItemClick(ListView l, View v, int position, long id){
Context context = MainActivity.this;
Toast.makeText(context, "item " + position + " was clicked", Toast.LENGTH_LONG).show();
}
}
和2個對應的佈局:
<?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"
android:padding="4dp"
android:textColor="#000000"
android:focusableInTouchMode="true">
<!--android:background="#eef"-->
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="100"
android:layout_margin="4dp">
listView
<Button
android:id="@+id/btnSearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Search"
android:layout_weight="70">
<!--<requestFocus/>-->
</Button>
<EditText
android:id="@+id/txtSearch"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:ems="10"
android:layout_weight="30"
android:background="#ffffff">
</EditText>
</LinearLayout>
<ListView
android:id="@+id/lvEventList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="2dp"
android:layout_margin="4dp"
android:footerDividersEnabled="true"
/>
</LinearLayout>
和(擴展簡單的項目列表1複印件)
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:textColor="#000000"
android:background="#ffffff"
android:clickable="true"
android:contextClickable="true" />
任何想法,爲什麼?
你的第二個XML在Button上方有一個隨機的'listView'字...這不起作用 –