0

我检查了其他具有几乎相同标题的类似标签。这些答案不相关

在数组的一个位置设置元素时,两个元素具有相同的值。

公共类 LogActivity 扩展 Activity {

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list);
    startStopButton = (Button) findViewById(R.id.btnStart);
    loggingStatusText = (TextView) findViewById(R.id.logStatusText);
    mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    sensorList = mSensorManager.getSensorList(Sensor.TYPE_ALL);
    sensorValues=new ArrayList<float[]>(sensorList.size());
    sensorValsArray=new float[sensorList.size()][];
    sensorNameList = new ArrayList<String>();
    selectedSensorNames = new ArrayList<String>();
    for (Sensor itemSensor : sensorList)
    {
        if (itemSensor != null)
        {
            sensorNameList.add(itemSensor.getName());
        }
    }
    showSensorList();
}



private void showSensorList()
{
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setIcon(R.drawable.ic_launcher);
    builder.setMultiChoiceItems((CharSequence[]) sensorNameList
            .toArray(new CharSequence[sensorNameList.size()]),
            new boolean[sensorNameList.size()],
            new DialogInterface.OnMultiChoiceClickListener()
            {
                public void onClick(DialogInterface dialog,
                        int whichButton, boolean isChecked)
                {
                    if (isChecked)
                    {
                        if (!selectedSensorNames.contains(sensorNameList
                                .get(whichButton)))
                            selectedSensorNames.add(sensorNameList
                                    .get(whichButton));
                    } else
                    {
                        if (selectedSensorNames.contains(sensorNameList
                                .get(whichButton)))
                        {
                            selectedSensorNames.remove(sensorNameList
                                    .get(whichButton));
                        }
                    }
                }

            });
    builder.setPositiveButton("Ok", new DialogInterface.OnClickListener()
    {
        public void onClick(DialogInterface dialog, int whichButton)
        {               
            listeners=new SensorEventListener[selectedSensorNames.size()];
            float[] tempVals = new float[] { 0, 0, 0 };
            for (int i = 0; i < selectedSensorNames.size(); i++)
            {
                sensorValsArray[i]=tempVals;
            }               
            showRateList();
        }
    });
    builder.setCancelable(false);
    builder.create().show();

}





void registerSensors()
{
    for (Sensor sensor : sensorList)
    {
        if (selectedSensorNames.contains(sensor.getName()))
        {
            mSensorManager.registerListener(listeners[selectedSensorNames.indexOf(sensor.getName())], sensor, selectedDelay);
        }
    }
}

class SchedulerTask extends TimerTask
{
    /*
     * The task to run should be specified in the implementation of the
     * run() method
     */
    public void run()
    {
        logSensorData();
    }
}

private void createLog(String fileName)
{
    File root = getExternalFilesDir(null);// Get the Android external
    // storage directory

    Date cDate = new Date();
    String bstLogFileName = fileName;
    bstLogFile = new File(root, bstLogFileName);// Construct a new file for
    // using the specified
    // directory and name
    FileWriter bstLogWriter;
    logScheduler = new Timer();// Create a new timer for updating values
    // from content provider
    logScheduler.schedule(new SchedulerTask(),
            LOG_TASK_DELAY_IN_MILLISECONDS,
            getLogPeriodInMilliSeconds(selectedDelay));

}

public void logSensorData()
{
    Date stampDate = new Date();
    String LogPack ="\r\n";
    for (int count=0;count<selectedSensorNames.size();count++)
    {
        LogPack += sensorValsArray[count][0] + "," + sensorValsArray[count][1] + "," + sensorValsArray[count][2] + ",";
    }
    LogPack += "\r\n";

    try
    {
        F_StreamWriter.write(LogPack);
        F_StreamWriter.flush();
    }

    catch (IOException e)
    {
    }

    catch (NullPointerException e)
    {
    }
}



public void startStopLog(View v)
{
    if (startStopButton.getText().equals("Start"))
    {
        createSensorListeners();
        registerSensors();
        showFilenameDialog();           
    } else if (startStopButton.getText().equals("Stop"))
    {
        stopLog();
    }

}

public void startLog(String fileName)
{
    createLog(fileName);
}



public void stopLog()
{
    logScheduler.cancel();
    logScheduler.purge();       
    for(int i=0;i<listeners.length;i++)
        mSensorManager.unregisterListener(listeners[i]);
}

private void showFilenameDialog()
{
    final Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.custom_text_input_dialog);
    dialog.setCancelable(true);
    final EditText fileNameInput = (EditText) dialog
            .findViewById(R.id.fileNameText);
    Button button = (Button) dialog.findViewById(R.id.okButton);
    button.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
                startLog(nameInput);
                dialog.dismiss();
        }
    });
    dialog.show();

}


private void createSensorListeners()
{
    listeners=new SensorEventListener[selectedSensorNames.size()];      
    for (int i = 0; i < selectedSensorNames.size(); i++)
    {
        listeners[i]=new SensorEventListener()
        {

            @Override
            public void onSensorChanged(SensorEvent event)
            {
                sensorValsArray[selectedSensorNames.indexOf(event.sensor.getName())]=event.values; 

            }

            @Override
            public void onAccuracyChanged(Sensor sensor, int accuracy)
            {


            }
        };
    }
}

}

当 index 为 0 时,执行 set 命令时,也会改变 index 位置 '1' 处的值。谁能帮我这个?

提前致谢, Dheepak

4

1 回答 1

2

当 index 为 0 时,执行 set 命令时,也会改变 index 位置 '1' 处的值。谁能帮我这个?

你肯定弄错了它是什么原因造成的。在 ArrayList 的一个位置设置值不会神秘地导致另一个位置的值发生变化。它根本不像那样工作。

您观察到的效果将是由于其他原因:

  • 也许的价值index不是你所期望的
  • 也许 的价值event.values不是你所期望的。(也许你在创建Event对象的方式上犯了一个错误,它们都共享一个float[]对象。)
  • 也许位置 1 的值已经是那个值
  • 也许你有多个线程更新sensorValues列表。
于 2012-08-24T12:14:47.853 回答