当listView滚动listView.getChildAt(int index)返回NULL(Android)时

时间:2016-06-30 02:45:52

标签: android listview

我尝试在listview中添加进度条下载,然后我就完成了。这是我的代码 AdapterLv.class

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();
}}

但是当我滚动列表视图时,如果项目下载隐藏(未在屏幕中显示),则显示错误nullpointerexception

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

更新进度条。 我该怎么办呢? 谢谢大家!

1 个答案:

答案 0 :(得分:0)

不是使用getFirstVisiblePosition从点击的ListView行获取ProgressBar,而是在ProgressBar方法中添加startDownload作为参数并将其传递给updateStatus

getView方法更改为:

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);
        }
    });
相关问题