J'ai une tâche asyncfichier mp3 via AsyncTask et avoir la barre de progression listview android
nouvelle DownloadFile() execute (url2.toString()).
Chaque fois que je trouve un fichier mp3 sur url par exemple www.xyz.com/preview.mp3
J'appelle la tâche async ci-dessus
nouvelle DownloadFile(). Execute (url2.toString())
Ensuite, j'ai aussi une barre de progression dans une vue de liste, Mais maintenant la barre de progression n'est pas mise à jour J'utilise un adaptateur personnalisé et j'ai gonflé la vue correctement.
public class CopyOfDownloadsListActivity extends ListActivity {
/** Called when the activity is first created. */
// static ArrayList<String> pthreads = new ArrayList<String>();
static ImageView bt;
static ProgressBar pb;
static ListView allList;
static TextView tv;
String fileName;
String mp3URL;
URL url2;
int myProgress;
static int filecount = 0;
MyCustomAdapter adapter;
private class DownloadFile extends AsyncTask<String, Integer, Void>{
int count = 0;
ProgressDialog dialog;
int myProgess = 0;
ProgressBar progressBar;
@Override
protected Void doInBackground(String... u) {
try {
URL ul = new URL(u[0]);
Log.i("UI",ul.toString());
// int len = CopyOfMusicDownloader.mp3urls.size();
// URL url2 = new URL(CopyOfMusicDownloader.mp3urls.get(len-1));
HttpURLConnection c = (HttpURLConnection) ul.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
int lengthOfFile = c.getContentLength();
String PATH = Environment.getExternalStorageDirectory()
+ "/download/";
Log.v("", "PATH: " + PATH);
File file = new File(PATH);
file.mkdirs();
fileName = "Track";
filecount++;
fileName = fileName + Integer.toString(filecount) + ".mp3";
File outputFile = new File(file, fileName);
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
myProgress = (int)(len1*100/lengthOfFile);
//myProgress = (int)(len1);
Log.i("My Progress", Integer.toString(myProgress));
publishProgress(myProgress);
// publishProgress((int)(len1*100/lengthOfFile));
fos.write(buffer, 0, len1);
}
fos.close();
is.close();
}catch (IOException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute() {
}
@Override
protected void onPreExecute() {
setListAdapter(new MyCustomAdapter(CopyOfDownloadsListActivity.this, R.layout.row, CopyOfMusicDownloader.mp3urls));
}
@Override
protected void onProgressUpdate(Integer... values) {
pb.setProgress(count);// HERE IS THE PROBLEM
count++;
Log.i("Values", Integer.toString(values[0]));
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
int len = CopyOfMusicDownloader.mp3urls.size();
try {
url2 = new URL(CopyOfMusicDownloader.mp3urls.get(len-1));
new DownloadFile().execute(url2.toString());
Log.i("url",url2.toString());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater myMenuInflater = getMenuInflater();
myMenuInflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case(R.id.browsermenu):
Intent i = new Intent(CopyOfDownloadsListActivity.this, MusicDownloader.class);
startActivity(i);
break;
case(R.id.downloaderrmenu):
break;
case(R.id.playermenu):
Intent j = new Intent(CopyOfDownloadsListActivity.this, Players.class);
startActivity(j);
break;
}
return true;
}
public class MyCustomAdapter extends ArrayAdapter<String> {
public MyCustomAdapter(Context context, int textViewResourceId, ArrayList<String> pthreads) {
super(context, textViewResourceId, pthreads);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.row, parent, false);
bt =(ImageView)row.findViewById(R.id.cancel_btn);
tv =(TextView)row.findViewById(R.id.filetext);
pb = (ProgressBar)row.findViewById(R.id.progressbar_Horizontal);
return row;
}
}
}
Pourriez-vous mettre cela sous la forme d'une question? Je ne suis pas vraiment sûr de ce que vous demandez. – Phil