2016-06-30 7 views
0

Ich versuche, den Fortschrittsbalken Download in listview und ich habe es getan. Hier ist mein Code AdapterLv.classWenn listView scroll listView.getChildAt (int index) gibt NULL (Android)

public class AdapterPr extends BaseAdapter { 
Activity activity; 
ArrayList<String> listUrl; 
LayoutInflater layoutInflater; 
Clickdownload clickdownload; 
ListView lstView; 
private Handler handler= new Handler(); 
private String NameOfFolder="/DownLoadList1"; 

public AdapterPr(Activity activity, ArrayList<String> listUrl, ListView lstView) { 
    this.activity = activity; 
    this.listUrl = listUrl; 
    layoutInflater = LayoutInflater.from(activity); 
    this.lstView = lstView; 
} 

@Override 
public int getCount() { 
    return listUrl.size(); 
} 

@Override 
public Object getItem(int i) { 
    return listUrl.get(i); 
} 

@Override 
public long getItemId(int i) { 
    return 0; 
} 

@Override 
public View getView(final int i, View view, ViewGroup viewGroup) { 
    View v = layoutInflater.inflate(R.layout.item, viewGroup, false); 
    Button download = (Button) v.findViewById(R.id.download); 
    TextView tvname = (TextView) v.findViewById(R.id.tvName); 
    tvname.setText("Itemt" + i); 
    download.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 

     startDownload(i,listUrl.get(i)); 
     } 
    }); 
    return v; 
} 

private void updateStatus(int index, int Status) { 

    View v = lstView.getChildAt(index - lstView.getFirstVisiblePosition()); 
    ProgressBar progress = (ProgressBar) v.findViewById(R.id.progressBar2); 
    progress.setProgress(Status); 
    TextView txtStatus = (TextView) v.findViewById(R.id.textView); 
    txtStatus.setText("Load : " + String.valueOf(Status) + "%"); 
} 
public String subName(String data) { 
    return data.substring(data.lastIndexOf("/") + 1); 
} 
public void startDownload(final int position,final String urlDownload) { 
    Runnable runnable = new Runnable() { 
     int Status = 0; 
     public void run() { 
      int count = 0; 
      try { 
       URL url = new URL(urlDownload); 
       URLConnection conexion = url.openConnection(); 
       conexion.connect(); 

       int lenghtOfFile = conexion.getContentLength(); 
       Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile); 
       String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + NameOfFolder; 
       File dir = new File(file_path); 
       if (!dir.exists()) { 
        dir.mkdir(); 
       } 
       File file = new File(dir, subName(urlDownload)); 
       InputStream input = new BufferedInputStream(url.openStream()); 
       OutputStream output = new FileOutputStream(file); 
       byte data[] = new byte[1024]; 
       long total = 0; 
       while ((count = input.read(data)) != -1) { 
        total += count; 
        Status = (int)((total*100)/lenghtOfFile); 
        output.write(data, 0, count); 
        handler.post(new Runnable() { 
         public void run() { 
          updateStatus(position,Status); 
         } 
        }); 
       } 
       output.flush(); 
       output.close(); 
       input.close(); 
      } catch (Exception e) {} 
     } 

    }; 
    new Thread(runnable).start(); 
}} 

Aber wenn ich Listview Scrollen, wenn das Element herunterladen auszublenden (nicht im Bildschirm angezeigt) es zeigen Fehler Nullpointer in

ProgressBar progress = (ProgressBar) v.findViewById(R.id.progressBar2); 

auf Update progressbar. Wie kann ich es beheben? Danke allen!

+0

Haben Sie versucht, die Anmeldung Ihrer ' index 'und die zurückgegebene Ausgabe von' lstView.getFirstVisiblePosition() '? –

+0

Ihr Problem gelöst? –

+0

@ShreeKrishna es läuft gut, wenn ich Listenansicht nicht scrollen. log ist oke –

Antwort

0

Statt getFirstVisiblePosition der Verwendung ProgressBar von geklickt ListView Reihe zu bekommen, fügen ProgressBar in startDownload Methode als Parameter und übergeben es an updateStatus:

ändern getView Methode wie:

ProgressBar progress = (ProgressBar) v.findViewById(R.id.progressBar2); 
download.setTag(progress); 
download.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
     ProgressBar pBar=(ProgressBar)view.getTag(); 
     startDownload(i,listUrl.get(i),pBar); 
     } 
    }); 
Verwandte Themen