这是我使用ListView
填充SimpleCursorAdapter
的代码。但是我无法在此代码中实现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
}
}
答案 0 :(得分:3)
即使您使用android:inputType
,TextView
似乎也可能导致此问题。我遇到了同样的问题,并从android:inputType
中删除了TextView
属性修复了问题。
答案 1 :(得分:1)
您好varada我正在使用您的代码我暗示您的代码它正常工作在您的代码中进行了以下更改
并使用此代码,它工作正常,并在吐司中给出位置 点击项目
public class MainActivity extends Activity implements OnItemClickListener
{
TextView output;
ListView empListView;
@SuppressWarnings("deprecation")
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SimpleCursorAdapter adapter2=null;
try {
Table1 employeeTable = new Table1(this);
employeeTable.open();
try {
// employeeTable.insert("Ankit", "Name");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
Cursor c = employeeTable.fetchAllEmployee();
if (c!= null)
{
adapter2 = new SimpleCursorAdapter(this,
R.layout.sendto,c,
new String[] {c.getColumnName(1),c.getColumnName(2)},
new int[] {R.id.EmployeeName, R.id.EmployeeDesignation});
}
employeeTable.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
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(), "position"+position, Toast.LENGTH_SHORT).show();
}
});
}
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
// TODO Auto-generated method stub
}
}``
3-sendto.xml
<TextView
android:id="@+id/EmployeeName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/EmployeeDesignation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
答案 2 :(得分:0)
我也面临同样的问题。我将其更改为OnClickListner
。
rowView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//give ur code here
}
});
此处rowView
对应于ListView
中的行。
答案 3 :(得分:0)
如果您正在实施OnItemClickListener
并且onItemClick
是自动生成的,那么您应该在其中编写操作:
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
if (view.equals(empListView))
{
Toast.makeText(getApplicationContext(),"1 item clicked ", Toast.LENGTH_SHORT).show();
}
}
还包括empListView.setOnItemClickListener(this);
答案 4 :(得分:0)
当实际视图由于您的行文件中的其他视图而无法获得焦点时会发生这种情况,请检查我已经回答here,这可能会有所帮助。