滚动时listview的奇怪行为

时间:2011-08-26 13:32:04

标签: android listview

IMAGE URL ---> Please click

enter image description here

我正在制作ListView,其中包含TextViewProgressBarProgressBar最初处于隐身状态。我向包含textviews数据的ArrayList提供了BaseAdapter

单击ListView的项目时启用ProgressBar,将建立Http连接,并使用服务器中的数据更新TextView值。我为此使用了AsyncTask

假设列表中有10个项目,我点击1,2,3,4个项目,每个项目中都会显示进度条并更新文本视图。但是当我滚动列表视图时出现问题。在项目7,6,8,9中的任何一个中显示在3处较早显示的进度条(在3中不再显示)。 3项进度条的行为并不具体,它可能发生在1,2,4进度条上。进度条画在任何地方。发生了什么我不知道

初始化ListView的活动类:

private void onListViewItemSelection()
{
    progressLstVw.setOnItemClickListener( new OnItemClickListener() 
    {
        public void onItemClick( AdapterView<?> parent, View view, int position, long id ) 
        {

            downloadFile(url, view, position);
            view.postInvalidate(); 
        }
    });
}

public void downloadFile(String url, View view, int position)
{
    ProgressBar itemProgressBar = null;
    TextView titleTxtVw = null;

    itemProgressBar = (ProgressBar) view.findViewById(R.id.progressbar);
    titleTxtVw = (TextView) view.findViewById(R.id.progress_txt_TxtVw);

    progressBar = itemProgressBar;
    progressTxtVw = titleTxtVw;

    Handler handler = new Handler()
    {
        public void handleMessage(Message message)
        {
            switch (message.what)
            {
                case HttpConnection.DID_START: 
                {
                    Log.d("Twitter Icon", "Starting Connection");
                    progressBar.setVisibility(View.VISIBLE);
                    break;
                }
                case HttpConnection.DID_SUCCEED: 
                {
                    int position = message.getData().getInt("ITEM_POSITOIN");

                    listDataAry.get(position).setTxtData(message.getData().getString("RECIEVED_DATA"));
                    listDataAry.get(position).setProgressData(message.getData().getInt("PERCENT_CMPLTD"));

                    progressTxtVw.setText(listDataAry.get(position).getTxtData());
                    progressBar.setProgress(listDataAry.get(position).getProgressData());
                    break;
                }
                case HttpConnection.DID_CLOSED: 
                {
                    //progressBar.setVisibility(View.GONE);
                    break;
                }
                case HttpConnection.DID_ERROR: 
                {
                    Exception e = (Exception) message.obj;
                    e.printStackTrace();
                    break;
                }
            }
        }
    };
    new HttpConnection(handler, position).get(url);
}

BASE ADAPTER GET VIEW METHOD

public class ListAddapter extends BaseAdapter

{     private Context context = null;     private LayoutInflater layoutInflater = null;     private ArrayList urls = null;

public ListAddapter(Context context, LayoutInflater layoutInflater, ArrayList<ListData> urls)
{
    this.context = context;
    this.layoutInflater = layoutInflater;
    this.urls = urls;
}
@Override
public int getCount()
{
    System.out.println("getCount");
    return urls.size();
}

@Override
public Object getItem(int arg0) 
{
    System.out.println("getItem");
    return null;
}

@Override
public long getItemId(int arg0) 
{
    System.out.println("getItemId");
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) 
{
    View row = convertView;
    ViewHolder viewHolder = null;

    if(row == null)
    {
        ViewController viewController = null;

        row = layoutInflater.inflate(R.layout.listview, parent ,false);
        viewController = new ViewController(row);
        viewHolder = new ViewHolder();
        viewHolder.viewController = viewController;
        row.setTag(viewHolder);
    }
    else
    {
        viewHolder = (ViewHolder) row.getTag();
    }

    viewHolder.viewController.setTextData(urls.get(position).getTxtData());
    viewHolder.viewController.setProgressData(urls.get(position).getProgressData());


/*  if(row == null)
    {
        row = layoutInflater.inflate(R.layout.listview, parent ,false);
    }
    viewController = new ViewController(row);*/
    return row;
}

static class ViewHolder 
{
    ViewController viewController;
}

查看控制器类

public ViewController(View view)
{
    this.view = view;
    intializeUI();
}

private void intializeUI()
{
    txtView = (TextView) view.findViewById(R.id.progress_txt_TxtVw);
    progressBar = (ProgressBar) view.findViewById(R.id.progressbar);
}

public void setProgressData(int progressData) 
{
    this.progressData = progressData;
    progressBar.setProgress(this.getProgressData());
}

public int getProgressData() 
{
    return progressData;
}

public void setTextData(String textData) 
{
    this.textData = textData;
    txtView.setText(this.getTextData());
}

public String getTextData() 
{
    return textData;
}

1 个答案:

答案 0 :(得分:0)

只需更改以下代码即可获得正确答案

@Override
public View getView(int position, View convertView, ViewGroup parent) 
{
    View row = convertView;
    ViewHolder viewHolder = null;

    ViewController viewController = null;

    row = layoutInflater.inflate(R.layout.listview, parent ,false);
    viewController = new ViewController(row);
    viewHolder = new ViewHolder();
    viewHolder.viewController = viewController;
    row.setTag(viewHolder);
    viewHolder.viewController.setTextData(urls.get(position).getTxtData());
    viewHolder.viewController.setProgressData(urls.get(position).getProgressData());


/*  if(row == null)
    {
        row = layoutInflater.inflate(R.layout.listview, parent ,false);
    }
    viewController = new ViewController(row);*/
    return row;
}