两个带有文件名和文本视图的文本视图,从sdcard到listview

时间:2013-12-16 10:51:14

标签: listview datetime android-listview arraylist adapter

我的应用程序是我录制声音并在列表中显示录制文件的位置。我想创建一个包含两个textview的列表视图。我从SD卡指定了一个文件夹,其中包含我使用应用程序录制的音频文件,并在列表视图中显示文件。列表中的第一个textview是文件名,第二个文本是创建的时间或日期。我不知道如何在列表中添加时间。

我的代码如下

   public void onCreate(Bundle savedInstanceState)
   {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    recordstart = (Button)findViewById(R.id.button1);
    recordstop  = (Button)findViewById(R.id.button3);

    recording = false;

    recordstart.setOnClickListener(new OnClickListener() {




        public void onClick(final View v) {
            // TODO Auto-generated method stub


    AlertDialog.Builder alert = new AlertDialog.Builder(recordsound.this);

            alert.setTitle("Add Name");


            // Set an EditText view to get user input 
        final EditText input = new EditText(recordsound.this);
            alert.setView(input);

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
               value = input.getText().toString();
              // Do something with value!

               System.out.println("value is"+ value);

              if(value==""&&value==null){

                  recordstart.setVisibility(View.INVISIBLE);
                  listView.setVisibility(View.INVISIBLE);

                  recordstop.setVisibility(View.VISIBLE);

 Toast.makeText(getApplicationContext(),"Please enter the name  " +itemValue , Toast.LENGTH_LONG).show();

                  recordint=1;

              }else{

                  recordstart.setVisibility(View.INVISIBLE);
                  listView.setVisibility(View.INVISIBLE);

                  recordstop.setVisibility(View.VISIBLE);

                  recordint=2;

              }


                Time today = new Time(Time.getCurrentTimezone());
                today.setToNow();


              timearray.add(today.format("%k:%M:%S"));

              startRecording(v);

              }
            });

    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
                // Canceled.
              }
            });

            alert.show();

            startRecording(v);


        }
    });

    recordstop.setOnClickListener(new OnClickListener() {

        public void onClick(final View v) {


            stopRecording(v);


        }
    });

   String cardRootnew = Environment.getExternalStorageDirectory().getAbsolutePath() + "/newrecorder/"; 
    dataPathnew = cardRootnew;

    File folder1 = new File(dataPathnew);
    boolean success = false;
    if (!folder1.exists()) {
        success = folder1.mkdir();
    }

    if (!success) {
    } else { 
    }

     listView = (ListView) findViewById(R.id.mylist);
    myList = new ArrayList<String>();



    Time today = new Time(Time.getCurrentTimezone());
    today.setToNow();

    String cardRoot = Environment.getExternalStorageDirectory().getAbsolutePath()+ "/newrecorder/" ; 

    dataPath = cardRoot;

    File directory =new File(dataPath);
    File file = new File( dataPath );
    File list[] = file.listFiles();

    for( int i=0; i< list.length; i++)
    {
            myList.add( list[i].getName() );


    }



     adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, android.R.id.text1, myList);


    listView.setAdapter(adapter); //Set all the file in the list.


 // ListView Item Click Listener
listView.setOnItemClickListener(new OnItemClickListener() {

      @Override
      public void onItemClick(AdapterView<?> parent, View view,
         int position, long id) {

       // ListView Clicked item index
       int itemPosition     = position;

       // ListView Clicked item value
        itemValue    = (String) listView.getItemAtPosition(position);
        String filepath=dataPath + itemValue;

        Intent intent = new Intent(recordsound.this, FilesActivity.class);

        intent.putExtra("filepath", filepath);


        intent.putExtra("itemvalue", itemValue);
        startActivity(intent);

        // Show Alert 
        Toast.makeText(getApplicationContext(),
          "Playing  " +itemValue , Toast.LENGTH_LONG)
          .show();

      }

});

}

void playshit()
{

    File sound = new File(dataPath + itemValue);

    Uri soundUri = Uri.fromFile(sound);
    MediaPlayer mediaPlayer = MediaPlayer.create(this, soundUri);
    mediaPlayer.start();

}


private SeekBar getSeekBar(int id) {
    SeekBar seekBar = (SeekBar) findViewById(id);
    seekBar.setMax(MIDDLE * 2);
    seekBar.setProgress(MIDDLE);
    seekBar.setOnSeekBarChangeListener(this);
    return seekBar;
}

public void startRecording(View view) {


    if(recordint==1){

    if (!recording) {
        extAudioRecorder = ExtAudioRecorder.getInstanse(false);


        Time today = new Time(Time.getCurrentTimezone());
        today.setToNow();



        extAudioRecorder.setOutputFile(dataPathnew +String.valueOf(today.format("%k:%M:%S")));
        extAudioRecorder.prepare(); 
        extAudioRecorder.start();
        recording = true;


    }

    }else if(recordint==2){


        if (!recording) {
            extAudioRecorder = ExtAudioRecorder.getInstanse(false);

            Time today = new Time(Time.getCurrentTimezone());
            today.setToNow();
            extAudioRecorder.setOutputFile(dataPathnew +value+String.valueOf(today.format("%k:%M:%S")));
            extAudioRecorder.prepare(); 
            extAudioRecorder.start();
            recording = true;




            if(timearray!=null){
            System.out.println("today.format is"+ timearray.size());

            }
        }

    }

}

public void stopRecording(View view) {
    if (recording) {
        extAudioRecorder.stop();
        extAudioRecorder.release();
        recording = false;
    }

    Intent intent = getIntent();
    finish();
    startActivity(intent);

}

public void playRecording(View view) {
    File sound = new File(dataPathnew + "output.wav");
    Uri soundUri = Uri.fromFile(sound);
    MediaPlayer mediaPlayer = MediaPlayer.create(this, soundUri);
    mediaPlayer.start();
}

public void onProgressChanged(SeekBar seekBar, int progress,
        boolean fromUser) {
    if (seekBar.equals(rateBar)) {
        rate = progress - MIDDLE;
    } else if (seekBar.equals(tempoBar)) {
        tempo = progress - MIDDLE;
    } else if (seekBar.equals(pitchBar)) {
        pitch = progress - MIDDLE;
    }
}

public void onStartTrackingTouch(SeekBar seekBar) {        
}

public void onStopTrackingTouch(SeekBar seekBar) {        
}

请向我推荐一些关于如何在listview中为其他文本视图添加时间的想法。

1 个答案:

答案 0 :(得分:0)

Sandeep,您正在将文件名存储在myList数组中,就像您需要将文件的时间或日期存储到另一个Array中一样,并将两个数组传递到适配器并从baseAdapter类中获取文件和日期根据职位,它如此简单..

adapter = new ArrayAdapter(这个,             android.R.layout.simple_list_item_1,android.R.id.text1,myList,myDates);

在适配器类中,根据参数更改构造函数,即它。