ListView 中的 OnItemClickListener() 不起作用
这是我用 SimpleCursorAdapter
填充 ListView
的代码。但我无法在此代码中实现 onClick
侦听器。实际上,代码运行正常,没有错误,但我在单击某个项目时没有得到任何输出...
public class Senditems extends Activity implements OnItemClickListener
{
TextView output;
DataHelper dh;
ListView empListView;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.sendto);
Table1 employeeTable = new Table1(this);
employeeTable.open();
Cursor c = employeeTable.fetchAllEmployee();
if (c!= null)
{
SimpleCursorAdapter adapter2 = new SimpleCursorAdapter(this,
R.layout.sendto,c,
new String[] {c.getColumnName(1),c.getColumnName(2)},
new int[] {R.id.EmployeeName, R.id.EmployeeDesignation});
empListView = (ListView)findViewById(R.id.Employee);
empListView.setAdapter(adapter2);
empListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Toast.makeText(getApplicationContext(), "1 item clicked ", Toast.LENGTH_SHORT).show();
}
});
}
employeeTable.close();
}
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
// TODO Auto-generated method stub
}
}
This is the code that I made to populate a ListView
with a SimpleCursorAdapter
. But I am not able to implement the onClick
listener in this code. Actually the code is working properly without errors, but I am not getting any output on clicking an item...
public class Senditems extends Activity implements OnItemClickListener
{
TextView output;
DataHelper dh;
ListView empListView;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.sendto);
Table1 employeeTable = new Table1(this);
employeeTable.open();
Cursor c = employeeTable.fetchAllEmployee();
if (c!= null)
{
SimpleCursorAdapter adapter2 = new SimpleCursorAdapter(this,
R.layout.sendto,c,
new String[] {c.getColumnName(1),c.getColumnName(2)},
new int[] {R.id.EmployeeName, R.id.EmployeeDesignation});
empListView = (ListView)findViewById(R.id.Employee);
empListView.setAdapter(adapter2);
empListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Toast.makeText(getApplicationContext(), "1 item clicked ", Toast.LENGTH_SHORT).show();
}
});
}
employeeTable.close();
}
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
// TODO Auto-generated method stub
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
当实际视图由于行文件中的其他视图而未获得焦点时,就会发生这种情况,请检查我已经回答此处,可能是这很有帮助。
Its happens when actual view is not taking focus due to others view in your row file, check I already answered here, may be it's helps.
我也面临同样的问题。我将其更改为
OnClickListner
。这里的 rowView 对应于 ListView 中的行。
I also faced the same issue. I changed it to
OnClickListner
.here
rowView
corresponds to the row within theListView
.如果您正在实现
OnItemClickListener
并且onItemClick
是自动生成的,您应该在其中编写操作:并且还包括
empListView.setOnItemClickListener(this);
If you are implementing the
OnItemClickListener
andonItemClick
is auto generated you should write the actions inside it:and also include
empListView.setOnItemClickListener(this);
您好,varada,我正在使用您的代码,我暗示您的代码在您的代码中所做的以下更改工作正常,
并使用此代码,它工作正常并给出吐司中的位置
单击项目
3-sendto.xml
<前><代码>
<文本视图
android:id="@+id/EmployeeDesignation"
安卓:layout_width =“wrap_content”
安卓:layout_height =“wrap_content”
android:text="TextView" //>
Hi varada i am working with your code i impliment your code it is working fine the following change made in your code
and use this code it is working fine and give the position in toast
on item click
3-sendto.xml
看起来,即使您使用
TextView
,android:inputType
也可能会导致此问题。我遇到了同样的问题,并从TextView
中删除属性android:inputType
解决了问题。Looks like
android:inputType
might lead to this problem, even if you use aTextView
. I had the same issue and removing the attributeandroid:inputType
from theTextView
s fixed the problem.