更新我的列表视图白色新录音机音频

时间:2017-04-21 18:22:15

标签: java android listview

我使用listview显示我的唱片音频,但是当我录制一个新音频时 我想刷新listview。 但它不起作用。 我还没有使用android工作室的经验,对不起,如果我没有做到的话 具体 你能救我吗?

    public class MainActivity extends AppCompatActivity {

        private Button mRecordBtn;
        private Button mAtualizarBtn;
        private TextView mRecordLabel;
        private MediaRecorder mRecorder;
        private String mFileName = null;
        private static final String LOG_TAG = "Record_log";
        //private StorageReference mStorage;
        private ProgressDialog mProgress;

        ListView lv;
        String[] items;
        FetchSongs fs;
        ArrayList<File> mySongs;
        ProgressDialog dialog;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.activity_main);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
            lv = (ListView) findViewById(R.id.listView);
            fs = new FetchSongs();
            dialog = new ProgressDialog(this);
            dialog.setMessage("Please wait, Fetching Songs...");
            dialog.setCancelable(true);
            dialog.show();


            while (fs.getfetchstatus() != true) {
                mySongs = fs.findSongs(Environment.getExternalStorageDirectory());

            }
            if (mySongs != null) {
                dialog.dismiss();
            }

            mySongs = fs.getsonglist();

            items = new String[mySongs.size()];
            for (int i = 0; i < mySongs.size(); i++) {
                items[i] = mySongs.get(i).getName().toString().replace(".3gp", "");


            }


            final ArrayAdapter<String> adp = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, items);
            lv.setAdapter(adp);
            adp.notifyDataSetChanged();
            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    Intent intent = new Intent(getApplicationContext(), Player.class);
                    intent.putExtra("pos", i);
                    adp.notifyDataSetChanged();

                    startActivity(intent);
                    finish();

                }
            });

            //recorder
            mAtualizarBtn = (Button) findViewById(R.id.atualizarBtn);
            mRecordLabel = (TextView) findViewById(R.id.recordLabel);
            mRecordBtn = (Button) findViewById(R.id.recordBtn);

            mProgress = new ProgressDialog(this);
            //mFileName = getExternalCacheDir().getAbsolutePath();

            Long tsLong = System.currentTimeMillis()/1000;
            String ts = tsLong.toString();
            mFileName = Environment.getExternalStorageDirectory().getAbsolutePath() + "/recording-"+ts+".mp3";
            adp.notifyDataSetChanged();

            lv.setAdapter(adp);
            adp.notifyDataSetChanged();
            //mFileName += "/record_audio.MP3";

            mRecordBtn.setOnTouchListener(new View.OnTouchListener()
            {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent)
                {

                    if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {

                        startRecording();
                        mRecordLabel.setText("GRAVANDO");
                        adp.notifyDataSetChanged();


                    } else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {


                        stopRecording();
                        mRecordLabel.setText("Feche o app para " +
                                "atualizar a lista");


                        lv.setAdapter(adp);
                        adp.notifyDataSetChanged();



                    }

                    return false;
                }


            });
        }

        private void startRecording()
        {


            Long tsLong = System.currentTimeMillis()/1000;
            String ts = tsLong.toString();


            mRecorder = new MediaRecorder();
            mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
            mRecorder.setOutputFile(mFileName);
            mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
            mFileName = Environment.getExternalStorageDirectory().getAbsolutePath() + "/recording-"+ts+".mp3";

            try
            {
                mRecorder.prepare();

            } catch (IOException e)
            {
                Log.e(LOG_TAG, "prepare() failed");
            }

            mRecorder.start();

        }

        private void stopRecording()
        {

            mRecorder.stop();
            mRecorder.release();
            mRecorder = null;

        }

1 个答案:

答案 0 :(得分:0)

如果您希望在列表视图中刷新新记录,则必须在录制音频/视频后每次运行这些代码。   while(fs.getfetchstatus()!= true){                 mySongs = fs.findSongs(Environment.getExternalStorageDirectory());

        }
        if (mySongs != null) {
            dialog.dismiss();
        }

        mySongs = fs.getsonglist();

        items = new String[mySongs.size()];
        for (int i = 0; i < mySongs.size(); i++) {
            items[i] = mySongs.get(i).getName().toString().replace(".3gp", "");

所以复制此代码并将其粘贴到最后的private void stopRecording(){}中。 可能它会起作用......