我有一个ListView
,其中包含数组中的项目,即String[] statesList = {"Adult 1", "Adult 2","Adult 3","Child 1","Child 2","Child 1","Child 2","Child 2"};
现在我想隐藏TextView
如果该项目与成人分开点击。
这是我为填充listview中的数据而编写的函数
protected void onCreate(Bundle savedInstanceState) {
showPassengerListView();
}
private void showPassengerListView() {
String[] passengerList ={"Adult 1", "Adult 2","Adult 3","Child 1","Child 2","Child 1","Child 2","Child 2"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, android.R.id.text1, passengerList);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if((passengerList[position].contains("Child"))|| (passengerList[position].contains("Infant"))){
dateofBirth.setVisibility(View.VISIBLE);
}else{
dateofBirth.setVisibility(View.GONE);
}
}
});
**passengerInformationPopup()**
}
public void passengerInformationPopup() {
final Dialog dialog= new Dialog(IweenBookingPage.this,R.style.Dialog_Fullscreen);
dialog.setContentView(R.layout.passenger_details_dialog);
String[] tittlearray ={"Mr.","Mrs.","Ms"};
Spinner tittleSpinner = (Spinner) dialog.findViewById(R.id.Tittle);
dateofBirth = (TextView)dialog.findViewById(R.id.dateofBirth);
dateofBirth.setClickable(true);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, tittlearray);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
tittleSpinner.setAdapter(dataAdapter);
dialog.show();
}
我得到例外:
08-16 15:51:55.727: E/AndroidRuntime(15553): FATAL EXCEPTION: main
08-16 15:51:55.727: E/AndroidRuntime(15553): java.lang.NullPointerException
08-16 15:51:55.727: E/AndroidRuntime(15553): at com.android.iweentravel.Book$5.onItemClick(Book.java:184)
08-16 15:51:55.727: E/AndroidRuntime(15553): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
08-16 15:51:55.727: E/AndroidRuntime(15553): at android.widget.AbsListView.performItemClick(AbsListView.java:1114)
08-16 15:51:55.727: E/AndroidRuntime(15553): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2937)
08-16 15:51:55.727: E/AndroidRuntime(15553): at android.widget.AbsListView$1.run(AbsListView.java:3695)
08-16 15:51:55.727: E/AndroidRuntime(15553): at android.os.Handler.handleCallback(Handler.java:615)
08-16 15:51:55.727: E/AndroidRuntime(15553): at android.os.Handler.dispatchMessage(Handler.java:92)
08-16 15:51:55.727: E/AndroidRuntime(15553): at android.os.Looper.loop(Looper.java:153)
08-16 15:51:55.727: E/AndroidRuntime(15553): at android.app.ActivityThread.main(ActivityThread.java:4987)
08-16 15:51:55.727: E/AndroidRuntime(15553): at java.lang.reflect.Method.invokeNative(Native Method)
08-16 15:51:55.727: E/AndroidRuntime(15553): at java.lang.reflect.Method.invoke(Method.java:511)
08-16 15:51:55.727: E/AndroidRuntime(15553): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
08-16 15:51:55.727: E/AndroidRuntime(15553): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
08-16 15:51:55.727: E/AndroidRuntime(15553): at dalvik.system.NativeStart.main(Native Method)
如何检查从ListView中单击了哪个项目?在单击项目的基础上,我必须显示/隐藏TextView。主要问题是如何识别。这些项目在ListView中是动态的。但是有成人,儿童,婴儿等。
答案 0 :(得分:1)
您可以隐藏像这样的TextView
TextView.setVisibility(View.GONE)
或
TextView.setVisibility(View.INVISIBLE)
取决于你想做什么。
识别onItemClick()方法中的项目的一种简单方法是:
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// for example like that
if(passengerlist[position].contains("Adult)") // do something
// or like that
TextView tv = (TextView) view.findViewById(R.id.text1);
if(tv.getText().toString().contains("Adult)") // do something
}
});
答案 1 :(得分:0)
在你的{{1st}}尝试这样的事情:
onItemClick