wiered behaviour of listview while scrolling
IMAGE URL ---> Please click
I am creating a ListView
which has a TextView
and a ProgressBar
. The ProgressBar
is initially in invisible state. I am providing an ArrayList
to the BaseAdapter
that contains data for textviews.
When the item of the ListView
is clicked the ProgressBar
is enabled, a Http connection is established and TextView
value is updated with the data from server. I have used an AsyncTask
for this.
suppose there are 10 items in the list and i am clicking 1,2,3,4 items, in each item the progress bar is displayed and the textview is updated. but the problem arises when i scroll the listview. progress bar which is earlier displaying at 3 got displayed in any one the item 7,6,8,9(it is no more displayed in 3). it is not specific that the 3 items progressbar behaves wiered it can happen to 1,2,4 progress bar. progress bar is painted any where. what is happening i have no idea
Activity class where ListView
is initailized:
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;
}
VIEW CONTROLLER CLASS
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;
}
Just change below code you will get proper response
@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;
}
精彩评论