我有一个listview,从SQLite数据库中获取数据。我想让用户通过点击项目来更改字段的值。但是使用以下代码,不会触发click事件。有人可以帮忙吗?谢谢!
public class BDXDeviceActivity extends Activity {
private DeviceListControl deviceListControl;
private ListView listViewDevice;
private DeviceListAdapter2 deviceListAdapter2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// using a custom layout, otherwise a system default of List
setContentView(R.layout.activity_device_list);
listViewDevice = (ListView) findViewById(R.id.listViewDevice);
deviceListControl = new DeviceListControl(this);
// setListAdapter(new DeviceListAdapter2(this, deviceListControl.getDevices()));
deviceListAdapter2 = new DeviceListAdapter2(this, deviceListControl.getDevices());
listViewDevice.setAdapter(deviceListAdapter2);
listViewDevice.setClickable(true);
listViewDevice.setOnItemClickListener(onItemClickListener);
}
AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getBaseContext(), "clicked", Toast.LENGTH_SHORT).show();
DeviceName deviceName = (DeviceName) listViewDevice.getAdapter().getItem(position);
deviceListControl.deleteDevice(deviceName.getId());
// after delete, reread the list adapter and get names list via namechange
deviceListAdapter2 = new DeviceListAdapter2(getBaseContext(), deviceListControl.getDevices());
listViewDevice.setAdapter(deviceListAdapter2);
// Display success information
Toast.makeText(getApplicationContext(), "Deleted!", Toast.LENGTH_LONG).show();
}
};
...布局文件
<EditText
android:id="@+id/txtSearchDevice"
android:layout_width="200dp"
android:layout_height="30dp"
android:background="#f7f5b1a3"
android:focusable="false"
/>
<Button
android:layout_width="50dp"
android:layout_height="wrap_content"
android:id="@+id/btnSearchDevice"
android:textColor="#ff151515"
android:background="@android:drawable/ic_menu_search"
android:focusable="false"/>
</LinearLayout>
<ListView
android:id="@+id/listViewDevice"
android:layout_width="match_parent"
android:layout_height="300dp">
</ListView>