我有一个应用程序,当设备被旋转时,即onSensorChanged()时,我试图通过自定义列表适配器显示其中填充了一些动态数据的ListView。旋转设备时,ListView中的数据将相应地动态更新ListView中的数据。我能够生成动态数据,但是当单击ListItem时,似乎无法识别click事件。
为什么会这样?单击ListItem时,我想执行另一个详细说明单击的列表项数据的活动。
这是我的代码,
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;
}
}
我希望每当更换传感器或旋转设备时都会更新此数据。但这似乎是一个不正确的实施。
任何人都可以纠正我或建议一个好的方法。我是android的新手。 这对我来说很难!! 任何帮助将不胜感激。
答案 0 :(得分:0)
我也不是机器人专家,但每当我遇到这类问题时,我都会使用the Android logging mechanism ,即Log.d(String tag, String msg)
来帮我弄清楚出了什么问题。因此,我建议您通过代码大量放入一大堆Log.d语句,并使用输出来解决问题。