0

这是我用来填充 a 的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      
    }
}
4

5 回答 5

3

看起来android:inputType可能会导致这个问题,即使你使用TextView. android:inputType我有同样的问题,从s 中删除属性TextView解决了这个问题。

于 2011-05-17T19:52:57.233 回答
1

嗨,varada,我正在使用您的代码,我暗示您的代码运行良好,您的代码中进行了以下更改

  1. 请在您的表中提供 _id 主键字段。
  2. 并使用此代码,它工作正常,并在项目点击时给出 toast 中的位置

    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" />
    

于 2013-02-20T07:25:16.240 回答
0

我也面临同样的问题。我把它改成了OnClickListner.

rowView.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        //give ur code here
    }
});

这里rowView对应于ListView.

于 2011-03-25T06:28:16.700 回答
0

它发生在实际视图由于您的行文件中的其他视图而没有获得焦点时,请检查我已经在这里回答过,可能会有所帮助。

于 2013-02-20T07:55:40.893 回答
0

如果您正在实现OnItemClickListenerandonItemClick是自动生成的,您应该在其中编写操作:

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);

于 2011-03-25T06:42:33.333 回答