Android 无法为ListView选择行(setOnItemClickListener不工作)
我有一个自定义AlertDialog,其中有一个列表视图。在此listview中,setOnItemClickListener未运行 我想选择一行 主要活动代码:Android 无法为ListView选择行(setOnItemClickListener不工作),android,android-listview,Android,Android Listview,我有一个自定义AlertDialog,其中有一个列表视图。在此listview中,setOnItemClickListener未运行 我想选择一行 主要活动代码: public class MainActivity extends Activity implements OnClickListener{ private Button btnSearchFrmSrch, btnClearFrmSrch, btnCancelFrmSrch, btnAddFrmSrch; private ListVi
public class MainActivity extends Activity implements OnClickListener{
private Button btnSearchFrmSrch, btnClearFrmSrch, btnCancelFrmSrch, btnAddFrmSrch;
private ListView lViewFrmSrch;
private ArrayList<SearchItem> arrayList;
private ArrayAdapter<SearchItem> adapterForSearch;
private AlertDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void getAleart(View v){
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.search_product, null);
lViewFrmSrch = (ListView) view.findViewById(R.id.lViewFrmSrch2);
arrayList = new ArrayList<SearchItem>();
arrayList.add(new SearchItem("123", "123", "ABC", 12.9));
arrayList.add(new SearchItem("456", "456", "DEF", 99.9));
arrayList.add(new SearchItem("222", "666", "VVV", 546.2));
arrayList.add(new SearchItem("111", "666", "CCC", 39.9));
adapterForSearch = new SearchAdapter(this, R.layout.list_view_search,
arrayList);
lViewFrmSrch.setAdapter(adapterForSearch);
btnSearchFrmSrch = (Button) view.findViewById(R.id.btnSearchFrmSrch);
btnClearFrmSrch = (Button) view.findViewById(R.id.btnClearFrmSrch);
btnCancelFrmSrch = (Button) view.findViewById(R.id.btnCancelFrmSrch);
btnAddFrmSrch = (Button) view.findViewById(R.id.btnAddFrmSrch);
lViewFrmSrch.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int pos, long arg3) {
System.out.println("*#*#*#*#*#*#*#");
System.out.println(parent.getSelectedItem());
}
});
btnSearchFrmSrch.setOnClickListener(this);
btnClearFrmSrch.setOnClickListener(this);
btnCancelFrmSrch.setOnClickListener(this);
btnAddFrmSrch.setOnClickListener(this);
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Search Product");
alert.setView(view);
alert.setCancelable(false);
dialog = alert.create();
dialog.show();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnCancelFrmSrch:
dialog.dismiss();
break;
case R.id.btnAddFrmSrch:
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
布局代码:
<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=".MainActivity" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Click"
android:onClick="getAleart"
/>
自定义对话框代码:
<?xml version="1.0" encoding="utf-8"?>
自定义列表视图代码:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#BDBDBD"
android:shrinkColumns="*"
android:stretchColumns="*" >
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:gravity="center" >
<TextView
android:id="@+id/tViewItemCodeFrmLstViewSrch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_span="10"
android:gravity="left|center_vertical"
android:maxLines="1"
android:scrollHorizontally="true"
android:text="@string/item"
android:textColor="@color/black" />
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_span="22" >
<TextView
android:id="@+id/tViewItemNameFrmLstViewSrch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:focusable="false"
android:focusableInTouchMode="true"
android:gravity="left|center_vertical"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:textColor="@color/black" />
</HorizontalScrollView>
<TextView
android:id="@+id/tViewPriceFrmLstViewSrch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_span="8"
android:gravity="right|center_vertical"
android:textColor="@color/black" />
</TableRow>
定义不工作?没有显示正确的项目,或者甚至没有捕获单击事件?只有单击事件不起作用。。。。
<?xml version="1.0" encoding="utf-8"?>
<ListView
android:id="@+id/lViewFrmSrch2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</ListView>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:shrinkColumns="*"
android:stretchColumns="*" >
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:paddingBottom="5dp"
android:paddingTop="5dp" >
<Button
android:id="@+id/btnSearchFrmSrch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_span="10"
android:text="@string/search"
android:textSize="13sp" />
<Button
android:id="@+id/btnClearFrmSrch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_span="10"
android:text="@string/clear"
android:textSize="13sp" />
<Button
android:id="@+id/btnCancelFrmSrch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_span="10"
android:text="@string/cancel"
android:textSize="13sp" />
<Button
android:id="@+id/btnAddFrmSrch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_span="10"
android:text="@string/add"
android:textSize="13sp" />
</TableRow>
</TableLayout>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#BDBDBD"
android:shrinkColumns="*"
android:stretchColumns="*" >
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:gravity="center" >
<TextView
android:id="@+id/tViewItemCodeFrmLstViewSrch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_span="10"
android:gravity="left|center_vertical"
android:maxLines="1"
android:scrollHorizontally="true"
android:text="@string/item"
android:textColor="@color/black" />
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_span="22" >
<TextView
android:id="@+id/tViewItemNameFrmLstViewSrch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:focusable="false"
android:focusableInTouchMode="true"
android:gravity="left|center_vertical"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:textColor="@color/black" />
</HorizontalScrollView>
<TextView
android:id="@+id/tViewPriceFrmLstViewSrch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_span="8"
android:gravity="right|center_vertical"
android:textColor="@color/black" />
</TableRow>