嘿,我正在开发一个AudioRecorder应用程序(我是Android开发的新手)。一个“Tab”用于记录文件,另一个用于播放。问题是列表的适配器在活动开始时捕获数据而不是在记录之后捕获数据。所以我尝试让Swiperefreshlayout刷新onRefresh()方法中的数据。但我不能让它工作!我认为问题是由此引起的
for (String file:list){
if (file.endsWith(".3pg")){
values.add(file);
adapter.notifyDataSetChanged();
}
}
在onRefresh()中。
FragmentAllRecords.java:
package com.julian.recorder;
import android.app.LauncherActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.ListFragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.zip.Inflater;
/**
* A simple {@link Fragment} subclass.
*/
public class FragmentAllRecords extends ListFragment implements SwipeRefreshLayout.OnRefreshListener {
private String path;
private SwipeRefreshLayout mSwipeLayout;
private String musicURI;
private MediaPlayer mp;
private CardView mMediaCard;
private ProgressBar mMediaProgress;
private ArrayAdapter<String> adapter;
private ArrayList<String> values;
private String[] list;
public FragmentAllRecords() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View p= inflater.inflate(R.layout.fragment_fragment_all_records, container, false);
mSwipeLayout=(SwipeRefreshLayout)p.findViewById(R.id.swipelayout);
mSwipeLayout.setOnRefreshListener(this);
mMediaCard=(CardView)p.findViewById(R.id.mediaCard);
mMediaCard.setVisibility(View.GONE);
mp=new MediaPlayer();
// Use the current directory as title
path = Environment.getExternalStorageDirectory()
+File.separator
+Environment.DIRECTORY_MUSIC
+File.separator;
if (getActivity().getIntent().hasExtra("path")) {
path = getActivity().getIntent().getStringExtra("path");
}
// Read all files sorted into the values-array
values = new ArrayList<String>();
File dir = new File(path);
list = dir.list();
if (list != null) {
for (String file : list) {
if (!file.endsWith(".3pg")) {
values.add(file);
}
}
}
Collections.sort(values);
// Put the data into the list
adapter = new ArrayAdapter<String>(getActivity(),
R.layout.recording_list_item, R.id.listText, values);
setListAdapter(adapter);
return p;
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
mMediaCard.setVisibility(View.VISIBLE);
int duration=mp.getDuration();
int currposition=mp.getCurrentPosition();
musicURI=path
+values.get(position);
mp.getDuration();
try {
mp.setDataSource(musicURI);
} catch (IOException e) {
e.printStackTrace();
}
try {
mp.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mp.start();
}
@Override
public void onRefresh() {
values.clear();
adapter.notifyDataSetChanged();
for (String file:list){
if (file.endsWith(".3pg")){
values.add(file);
adapter.notifyDataSetChanged();
}
}
mSwipeLayout.setRefreshing(false);
}
}
fragment_fragment_all_records.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context="com.julian.recorder.FragmentAllRecords">
<android.support.v4.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/swipelayout">
<ListView android:id="@android:id/list" android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.v4.widget.SwipeRefreshLayout>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="72dp"
android:layout_gravity="bottom"
android:visibility="visible"
android:elevation="20dp"
android:id="@+id/mediaCard">
<Button
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_gravity="center"
android:id="@+id/mStop"
android:src="@drawable/pause_circle"
android:background="@drawable/pause_circle"
android:visibility="visible"/>
<Button
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_gravity="center"
android:id="@+id/mResume"
android:visibility="gone"
android:background="@drawable/play_circle"/>
<Button
android:layout_width="36dp"
android:background="@drawable/skip_next"
android:layout_height="36dp"
android:layout_gravity="center"
android:layout_marginLeft="64dp"
android:id="@+id/mNext" />
<Button
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_gravity="center"
android:id="@+id/mPrevious"
android:layout_marginRight="64dp"
android:background="@drawable/skip_previous"/>
</android.support.v7.widget.CardView>
(也许这也是必需的。不知道......)Mainactivity.java :(我称之为RecordingActivity)
package com.julian.recorder;
import android.app.Activity;
import android.content.Context;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.view.View;
import android.widget.Button;
import android.support.v4.view.PagerAdapter;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.media.AudioRecord;
import android.widget.Toast;
import android.widget.ArrayAdapter;
import android.app.ListActivity;
import android.support.v4.app.FragmentStatePagerAdapter;
import com.getbase.floatingactionbutton.FloatingActionButton;
import com.getbase.floatingactionbutton.FloatingActionsMenu;
import com.getbase.floatingactionbutton.AddFloatingActionButton;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import it.neokree.materialtabs.MaterialTab;
import it.neokree.materialtabs.MaterialTabHost;
import it.neokree.materialtabs.MaterialTabListener;
public class RecordingActivity extends ActionBarActivity implements MaterialTabListener{
private Toolbar toolbar;
MaterialTabHost tabHost;
ViewPager pager;
ViewPagerAdapter pagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recording);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
tabHost = (MaterialTabHost) this.findViewById(R.id.tabHost);
pager = (ViewPager) this.findViewById(R.id.pager);
pagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
pager.setAdapter(pagerAdapter);
pager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
tabHost.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < pagerAdapter.getCount(); i++) {
tabHost.addTab(
tabHost.newTab()
.setText(pagerAdapter.getPageTitle(i))
.setTabListener(this)
);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_recording, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
return super.onOptionsItemSelected(item);
}
@Override
public void onTabSelected(MaterialTab materialTab) {
pager.setCurrentItem(materialTab.getPosition());
}
@Override
public void onTabReselected(MaterialTab materialTab) {
}
@Override
public void onTabUnselected(MaterialTab materialTab) {
}
}
答案 0 :(得分:1)
我认为你在file.endsWith(&#34; .3pg&#34;)提供文件扩展名时犯了一个错误,这应该是&#34; .3gp&#34;这是生成的音频文件的正确扩展名。