Android自定义列表视图,setOnItemSelectedListener不起作用 [英] Android custom listview, setOnItemSelectedListener not working
问题描述
我刚刚开始Android开发,并且我正在努力获得一个带有复选框的自定义列表视图.我创建了一个扩展 Activity 的基类,创建了一个Adapter并覆盖了getView()方法以将复选框添加到列表视图.我假设我需要这样做,因为我需要与Obj C中的didSelectRowIndexAtPath
等效的东西来更新我的模型.请让我知道是否还有另一种方法!
I'm just beginning Android development, and I'm working to get a Custom listview with a checkbox working. I've created a base class that extends Activity, Created an Adapter and overrode the getView() method to add the checkbox to the listview. I'm assuming I need to do this because I need something equivalent to didSelectRowIndexAtPath
from Obj C to update my model. Please let me know if there's an alternate way of doing this too!
现在在我的基类中,我有以下代码-
Now in my base class, I have the following code -
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout);
setContentView(R.layout.facilityscreen);
/* Static Data source */
facilityModel = new FacilityDataModel[2];
facilityModel[0] = new FacilityDataModel();
facilityModel[1] = new FacilityDataModel();
facilityModel[0].setFacilityName("Test 1");
facilityModel[0].setFacilityID("Facid0001");
facilityModel[0].setChecked(false);
facilityModel[1].setFacilityName("Test 2");
facilityModel[1].setFacilityID("Facid0002");
facilityModel[1].setChecked(true);
facilityListView = (ListView) findViewById(R.id.facilityListView);
FacilityScreenAdapter adapter = new FacilityScreenAdapter(this, facilityModel);
facilityListView.setAdapter(adapter);
myPatBtn = (Button) findViewById(R.id.myPatBtn);
myPatBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
int i=0;
i++;
}});
facilityListView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
int i=0;
i++;
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
我现在的问题是根本没有调用setOnItemSelectedListener.现在已经为此苦苦挣扎了两个小时,我不知道为什么根本不会调用它.
My problem now is the setOnItemSelectedListener isn't getting called at all. Been struggling with this for a couple of hours now, and I can't figure out why it wouldn't get called at all.
非常感谢您的帮助!
谢谢,
Teja.
Thanks,
Teja.
推荐答案
我知道这是一个过时的答案,但是我打算写这个,以防其他一些具有相同问题"的人碰到这个页面:
I know this is an outdated answer but I'm going to write it just in case some other fellow who has the same "problem" bumps onto this page :
上述问题的解决方案不是问题,只是一个误会,它是在触发ListView.onItemSelected()事件时出现的:
The solution to the above problem which is not a problem but just a misunderstanding is that the ListView.onItemSelected() event is fired up, upon :
1)浏览模拟器交叉手柄或 2)就我的HTC-Hero而言,白色小滚球上的滚动动作.
1) Navigating through the emulators-cross handles or 2) as far-as my HTC-Hero is concerned, the rolling-action on the white little roller-ball.
您不必将活动明确地扩展到ListActivity.
You don't have to extend your activity explicitly to a ListActivity.
这是我的小代码,可从中检索电话号码 Listview项内的TextView控件. 当用户触摸列表项或使用
Here's my tiny little code which retrieves a phone number from a TextView control, inside a listview item. When the user either touches the list item or scrolls through the list with
myList.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long i)
{
TextView myPhone = (TextView)view.findViewById(R.id.txtphone);
MakeACall(myPhone.getText().toString());
}
});
myList.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int position, long i)
{
TextView myPhone = (TextView)view.findViewById(R.id.txtphone);
MakeACall(myPhone.getText().toString());
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
我希望这对您有所帮助...:)
I hope that was helpful... :)
这篇关于Android自定义列表视图,setOnItemSelectedListener不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!