Android 更改传感器事件时,ListItem Click事件不会触发或响应非常慢
我有一个应用程序,当设备旋转时,即onSensorChanged()时,我试图显示一个ListView,其中包含一些通过自定义列表适配器填充的动态数据。当设备旋转时,ListView中的数据将在ListView中相应地动态更新。我能够生成动态数据,但当单击ListItem时,似乎无法识别单击事件 为什么会这样?单击ListItem时,我想执行另一个活动,详细说明单击的列表项的数据 这是我的密码Android 更改传感器事件时,ListItem Click事件不会触发或响应非常慢,android,events,listview,sensors,Android,Events,Listview,Sensors,我有一个应用程序,当设备旋转时,即onSensorChanged()时,我试图显示一个ListView,其中包含一些通过自定义列表适配器填充的动态数据。当设备旋转时,ListView中的数据将在ListView中相应地动态更新。我能够生成动态数据,但当单击ListItem时,似乎无法识别单击事件 为什么会这样?单击ListItem时,我想执行另一个活动,详细说明单击的列表项的数据 这是我的密码 public void onSensorChanged(SensorEvent event) {
public void onSensorChanged(SensorEvent event) {
TextView txtangle = (TextView) findViewById(R.id.txtangle);
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
// Log.v("ACCELEROMETER", "ACCELEROMETER");
mGravity = event.values;
}
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
// Log.v("MAGNETIC_FIELD", "MAGNETIC_FIELD");
mGeomagnetic = event.values;
}
if (mGravity != null && mGeomagnetic != null) {
float first[] = new float[9];
float second[] = new float[9];
boolean success = SensorManager.getRotationMatrix(first, second,
mGravity, mGeomagnetic);
if (success) {
float orientation[] = new float[3];
SensorManager.getOrientation(first, orientation);
azimuth = orientation[0]; // orientation contains: azimut, pitch
// and roll
}
}
mycallback(event);
}
public void mycallback(SensorEvent event) {
ListView propertylistview = (ListView) findViewById(R.id.listview);
PropertiesArray = new ArrayList<Propety>(4);
PropertiesArray.add(new Propety("Banjara Hills",
"Price: 100000"));
PropertiesArray.add(new Propety("Jubilee Hills",
"Price: 200000"));
PropertiesArray.add(new Propety("Film Nagar", "Price: 70000"));
PropertiesArray.add(new Propety("Kukatpally", "Price: 50000"));
PropertiesArray.add(new Propety("Jubilee Hills",
"Price: 200000"));
PropertiesArray.add(new Propety("Film Nagar", "Price: 70000"));
customadapter customlistview = new customadapter(
PropertiesArray); // customadapter is the Custom List Adapter
propertylistview.setAdapter(customlistview);
} //This is the hardcoded data.
传感器更改时的公共无效(传感器事件){
TextView txtangle=(TextView)findViewById(R.id.txtangle);
if(event.sensor.getType()==sensor.TYPE\u加速计){
//对数v(“加速计”、“加速计”);
mGravity=event.values;
}
if(event.sensor.getType()==sensor.TYPE\u磁场){
//Log.v(“磁场”、“磁场”);
mGeomagnetic=event.values;
}
if(mGravity!=null&&mGeomagnetic!=null){
先浮动[]=新浮动[9];
第二个浮点[]=新浮点[9];
布尔成功=SensorManager.getRotationMatrix(第一,第二,
MG航空公司(磁性);
如果(成功){
浮动方向[]=新浮动[3];
SensorManager.getOrientation(第一,方向);
方位角=方向[0];//方向包含:方位角、俯仰角
//滚
}
}
mycallback(事件);
}
公共无效mycallback(SensorEvent事件){
ListView propertylistview=(ListView)findViewById(R.id.ListView);
PropertiesArray=新的ArrayList(4);
房地产Array.add(新房地产(“班吉拉山”),
“价格:100000”);
房地产Array.add(新房地产(“朱比利山”),
“价格:200000”);
房地产数组。添加(新房地产(“Film Nagar”,“价格:70000”);
添加(新物业(“Kukatpally”,“价格:50000”);
房地产Array.add(新房地产(“朱比利山”),
“价格:200000”);
房地产数组。添加(新房地产(“Film Nagar”,“价格:70000”);
customadapter customlistview=新的customadapter(
PropertiesArray);//customadapter是自定义列表适配器
setAdapter(customlistview);
}//这是硬编码的数据。
我的customadapter类
public class customadapter extends BaseAdapter {
LayoutInflater inflater;
ArrayList<Propety> PropertiesArray;
public customadapter(ArrayList<Propety> PropertiesArray) {
this.PropertiesArray=PropertiesArray;
}
@Override
public int getCount() {
return PropertiesArray.size();
}
@Override
public Object getItem(int position) {
return PropertiesArray.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(final int position, View View, final ViewGroup parent) {
if (View == null) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View = inflater.inflate(R.layout.customlistview, parent, false);
}
final Propety ListArray = PropertiesArray.get(position);
TextView tvPropertyName = (TextView) View.findViewById(R.id.tvCity);
tvPropertyName.setText(ListArray.getName());
TextView tvPrice = (TextView) View.findViewById(R.id.tvprice);
tvPrice.setText(ListArray.getPrice());
ImageView imgProperty = (ImageView) View.findViewById(R.id.list_image);
imgProperty.setImageResource(R.drawable.ic_launcher);
View.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(parent.getContext(), "view clicked: "+ ListArray.getName(), Toast.LENGTH_SHORT).show();
} // This is not firing. Toast is not being displayed.
});
return View;
}
}
公共类customadapter扩展了BaseAdapter{
充气机;
阵列列表属性阵列;
公共customadapter(ArrayList PropertiesArray){
这个.PropertiesArray=PropertiesArray;
}
@凌驾
public int getCount(){
返回属性array.size();
}
@凌驾
公共对象getItem(int位置){
返回属性array.get(位置);
}
@凌驾
公共长getItemId(int位置){
//TODO自动生成的方法存根
返回位置;
}
@凌驾
公共视图getView(最终整型位置、视图视图、最终视图组父视图){
如果(视图==null){
LayoutInflater充气器=LayoutInflater.from(parent.getContext());
视图=充气机。充气(R.layout.customlistview,父级,false);
}
最终属性ListArray=PropertiesArray.get(位置);
TextView tPropertyName=(TextView)View.findViewById(R.id.tvCity);
tvPropertyName.setText(ListArray.getName());
TextView tvPrice=(TextView)View.findViewById(R.id.tvPrice);
setText(listary.getPrice());
ImageView imgProperty=(ImageView)View.findViewById(R.id.list\u image);
imgProperty.setImageResource(R.drawable.ic_启动器);
View.setOnClickListener(新的View.OnClickListener(){
@凌驾
公共void onClick(视图v){
Toast.makeText(parent.getContext(),“单击视图:”+listary.getName(),Toast.LENGTH_SHORT).show();
}//未触发。未显示Toast。
});
返回视图;
}
}
我希望在更换传感器或旋转设备时更新此数据。但这似乎是一个不正确的实现
谁能纠正我或建议一个好的方法。我是android的新手。
这对我来说很难!!
任何帮助都将不胜感激。我也不是安卓专家,但每当我遇到此类问题时,我都会使用Log.d(String tag,String msg)来帮助我找出问题所在。因此,我建议您在代码中大量使用Log.d语句,并使用输出来解决问题。有人能提出任何建议吗!!!