0

我正在尝试放置 onListItemClick,但是当我启动我的应用程序时,onListItemClick 没有任何反应。谁能帮我这个?

这是我的代码:

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    db = (new DatabaseHelper(this)).getWritableDatabase();
    searchText = (EditText) findViewById(R.id.searchText);
    employeeList = (ListView) findViewById(R.id.list);

    Button searchButton = (Button) findViewById(R.id.searchButton);
    searchButton.setOnClickListener(new OnClickListener() {
        public void onClick(View view) {
            search(employeeList);
        }
    });

}

public void search(View view) {
    // || is the concatenation operation in SQLite
    cursor = db
            .rawQuery(
                    "SELECT _id, firstName, lastName, title  FROM employee WHERE firstName || ' ' || lastName LIKE ?",
                    new String[] { "%" + searchText.getText().toString()
                            + "%" });
    adapter = new SimpleCursorAdapter(this, R.layout.employee_list_item,
            cursor, new String[] { "firstName", "lastName", "title" },
            new int[] { R.id.firstName, R.id.lastName, R.id.title });
    employeeList.setAdapter(adapter);

}




    public void onListItemClick(ListView parent, View view, int position, long id) {
        Intent intent = new Intent(this, EmployeeDetails.class);
        Cursor cursor = (Cursor) adapter.getItem(position);
        intent.putExtra("EMPLOYEE_ID", cursor.getInt(cursor.getColumnIndex("_id")));
        startActivity(intent);
    }

}

没有错误消息,搜索正常。

4

4 回答 4

1
employeeList = (ListView) findViewById(R.id.list);

在上面的行下面添加这些行

employeeList = (ListView) findViewById(R.id.list);
employeeList .setOnItemClickListener(this);

更新::示例代码

public class SampleActivity extends Activity implements OnItemClickListener{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(mWebView);
    }
    @Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
       // TODO Auto-generated method stub
}
}
于 2012-06-03T02:17:58.987 回答
0

像这样为您的列表视图注册一个OnItemClickListener()setOnItemClickListener

listView.setOnItemClickListener(new OnItemClickListener() {
    @Override public void onItemClick(AdapterView<?> adapter, View view, int pos, long id) {
        Intent intent = new Intent(this, EmployeeDetails.class);
        Cursor cursor = (Cursor) adapter.getItem(pos);
        intent.putExtra("EMPLOYEE_ID", cursor.getInt(cursor.getColumnIndex("_id")));
        startActivity(intent);                
    }
});
于 2012-06-03T00:54:04.963 回答
0

您必须像listview.setOnItemClickListener(this)在代码中一样注册您的侦听器。

于 2012-06-03T00:56:13.790 回答
0

试试这个

listview.setOnItemClickListener(new OnItemClickListener() {

     // Your Code Goes Here
});
于 2012-06-03T16:45:29.280 回答