我有一个应用程序,当设备旋转时,即 onSensorChanged() 时,我试图显示一个 ListView,其中通过自定义列表适配器填充了一些动态数据。当设备旋转时,ListView 中的数据会相应地在 ListView 中动态更新。我能够生成动态数据,但是当单击 ListItem 时,似乎无法识别单击事件。
为什么会这样?单击 ListItem 时,我想执行另一个活动来详细说明单击的 List Item 的数据。
这是我的代码,
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.
我的自定义适配器类,
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;
}
}
我希望在更改传感器或旋转设备时更新此数据。但这似乎是一个不正确的实现。
任何人都可以纠正我或提出一个好的方法。我是安卓新手。这对我来说太难了!!任何帮助将不胜感激。