如何在android中运行时更新列表项?

时间:2015-06-24 19:17:39

标签: android listview

在我的Android应用程序中,我使用FTP将文件上传到服务器。我有一个文件列表,我正在显示这是一个Listview。当用户选择列表项并点击上传按钮时,它正在上传,我想在所选文件中显示上传进度。我完成了这部分,但问题是当我滚动列表视图时,显示了不同文件的进度(未选择上传)。我的确切期望就像belo图像。请任何人帮助我 enter image description here

public View getView(final int position, View convertView, ViewGroup parent) {

    View view = convertView;
    if (view == null) {
        view = LayoutInflater.from(context).inflate(layoutResourceId, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        view.setTag(viewHolder);
    }

    final ViewHolder viewHolder = (ViewHolder) view.getTag();

    final FileItem fItem = files.get(position);
    //if(position!=0){
    if(fItem != null) {
        viewHolder.fNameTextView.setText(fItem.getName());
        if(fItem.getDate() != null) {
            Date lastModifiedDate = fItem.getDate();
            DateFormat formatter = DateFormat.getDateTimeInstance();
            viewHolder.fDateTextView.setText(formatter.format(lastModifiedDate));
        }
        else {
            viewHolder.fDateTextView.setText("");
        }

// fItem.getType()             int fileType = fItem.getType();             long data = fItem.getData();

        if(fileType == Globals.FILE_TYPE_DIRECTORY) {
            if(data <= 1){
                viewHolder.fDataTextView.setText(data+" Item");
            }
            else if(data == Long.MAX_VALUE) {
                viewHolder.fDataTextView.setText("Folder");
            }
            else {
                viewHolder.fDataTextView.setText(data+" Items");
                //viewHolder.fImageviewSelection.setVisibility(View.GONE);
            }
            viewHolder.fIconImageView.setImageResource(Globals.ICON_DIRECTORY);

        }
        else if (fileType == Globals.FILE_TYPE_APPLICATION) {
            viewHolder.fDataTextView.setText(Utilities.getHumanReadableFileSize(data,true));
            viewHolder.fIconImageView.setImageResource(Globals.ICON_APPLICATION);
        }
        else if (fileType == Globals.FILE_TYPE_AUDIO) {
            viewHolder.fDataTextView.setText(Utilities.getHumanReadableFileSize(data,true));
            viewHolder.fIconImageView.setImageResource(Globals.ICON_AUDIO);
        }
        else if (fileType == Globals.FILE_TYPE_IMAGE) {
            viewHolder.fDataTextView.setText(Utilities.getHumanReadableFileSize(data,true));
            viewHolder.fIconImageView.setImageResource(Globals.ICON_IMAGE);
        }
        else if (fileType == Globals.FILE_TYPE_TEXT) {
            viewHolder.fDataTextView.setText(Utilities.getHumanReadableFileSize(data,true));
            viewHolder.fIconImageView.setImageResource(Globals.ICON_TEXT);
        }
        else if (fileType == Globals.FILE_TYPE_VIDEO) {
            viewHolder.fDataTextView.setText(Utilities.getHumanReadableFileSize(data,true));
            viewHolder.fIconImageView.setImageResource(Globals.ICON_VIDEO);
        }
        else if (fileType == Globals.FILE_TYPE_PARENT) {
            viewHolder.fDataTextView.setText("Parent Directory");
            viewHolder.fIconImageView.setImageResource(Globals.ICON_PARENT);

        }
        else if (fileType == Globals.FILE_TYPE_OTHERS) {
            viewHolder.fDataTextView.setText(Utilities.getHumanReadableFileSize(data,true));
            viewHolder.fIconImageView.setImageResource(Globals.ICON_OTHERS);
        }
    }

//}         final CheckBox cb = viewHolder.fImageviewSelection;         cb.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (files.get(position).isclicked) {
                    files.get(position).isclicked = false;
                    files.get(position).index=0;
                } else {
                    files.get(position).isclicked = true;
                    files.get(position).index=position;
                }               
        }
    });

    if (files.get(position).isclicked) {
        cb.setChecked(true);           
    }
    else {
        cb.setChecked(false);
    }  


    return view;
}

更新进度的代码

                                   final ListView listView = mLocalFileListView;


                                        final int firstListItemPosition = listView.getFirstVisiblePosition();
                                        final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;

                                        if (pos < firstListItemPosition || pos > lastListItemPosition ) {
                                             convertView= listView.getAdapter().getView(pos, null, listView);
                                        } else {
                                            final int childIndex = pos- firstListItemPosition;
                                             convertView=listView.getChildAt(childIndex);
                                        }
                                     convertView=listView.getChildAt(pos);


                                    //View convertView = listView.getChildAt(selectedFiles.get(0).getIndex()-first);
                                    if(convertView!=null){                                                                              
                                        ImageView iv=(ImageView)convertView.findViewById(R.id.file_list_item_icon);
                                        iv.setVisibility(View.VISIBLE);
                                        CheckBox cb=(CheckBox)convertView.findViewById(R.id.file_list_item_selector);
                                        cb.setVisibility(View.VISIBLE);
                                        TextView tv=(TextView)convertView.findViewById(R.id.textView1);
                                        ProgressBar pb=(ProgressBar)convertView.findViewById(R.id.progressBar1);
                                        pb.setVisibility(View.INVISIBLE);
                                        pb.setProgress(percent);
                                        pb.setSecondaryProgress(percent+10);
                                        tv.setVisibility(View.INVISIBLE);
                                        tv.setText(percent + "%" ); 
                                        mAdapter.notifyDataSetChanged();

0 个答案:

没有答案