简体   繁体   中英

onListItemClick won't start

I'm trying to put onListItemClick, but when I start my app nothing happens with onListItemClick. Can anyone help me with this?

Here is my code:

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

}

There is no error messages and search is working normally.

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

add these line below the above line

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

Updated:: sample code

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
}
}

register an OnItemClickListener() with setOnItemClickListener for your listview like this

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

您必须在代码中将侦听器注册为listview.setOnItemClickListener(this)

Try this

listview.setOnItemClickListener(new OnItemClickListener() {

     // Your Code Goes Here
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM