0

デバイスが回転したとき、つまり onSensorChanged() のときに、カスタム リスト アダプターを介して動的データが入力された ListView を表示しようとしているアプリケーションがあります。デバイスが回転すると、それに応じて ListView 内のデータが ListView 内で動的に更新されます。動的データを生成できますが、ListItem をクリックすると、クリック イベントが認識されないようです。

なぜこうなった?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. 

私の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;
}

}

センサーが変更されたり、デバイスが回転したりするたびに、このデータが更新されるようにします。しかし、これは正しくない実装のようです。

誰でも私を修正したり、良い方法を提案したりできますか. 私はアンドロイドの初心者です。これは私にはとても難しいです!! どんな助けでも大歓迎です。

4

1 に答える 1

0

私も Android の専門家ではありませんが、この種の問題が発生するたびに、Android のログ メカニズムを使用して、何が問題なのかを突き止めLog.d(String tag, String msg)ます。したがって、コード全体に Log.d ステートメントを大量に配置し、その出力を使用して問題を解決することをお勧めします。

于 2013-05-11T18:25:00.767 に答える