ListView 动态数据行在滚动时发生变化
ListView dynamic datarow changing on scrolling
我想将文本视图的文本和背景颜色从 "Not Uploaded" 更改为 "Uploading...",并且还想更改背景颜色。但是,当我单击上传按钮时,它会更改所选视图的颜色和文本,但它也会对最后一个可见项目+1 进行相同的更改。当我滚动列表时,更改效果也会在其他视图上继续移动。
这是我的自定义适配器 getview 函数的代码
public View getView(int position, View convertView, ViewGroup parent) {
View v = null;
if(convertView == null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v=inflater.inflate(R.layout.fileview,parent,false);
TextView tv = (TextView) v.findViewById(R.id.tv_file);
TextView tv_date = (TextView) v.findViewById(R.id.tv_fileDate);
TextView tvFileSize = (TextView) v.findViewById(R.id.tvFileSize);
TextView tvFileStatus = (TextView) v.findViewById(R.id.tvUploadStatus);
ImageView _img = (ImageView) v.findViewById(R.id.iv_file);
ImageView btnTrash = (ImageView) v.findViewById(R.id.btnTrash);
ImageView btnEdit = (ImageView) v.findViewById(R.id.btnEdit);
ImageView btnUpload = (ImageView) v.findViewById(R.id.btnUpload);
btnTrash.setOnClickListener(new myclickListener(context,files[position],position));
btnEdit.setOnClickListener(new myclickListener(context,files[position],position));
btnUpload.setOnClickListener(new myclickListener(context,files[position],position));
int index = files[position].lastIndexOf(".");
String file_extention = files[position].substring(index+1,files[position].length());
file_extention=file_extention.toLowerCase();
if(file_extention.equals("png"))
{
_img.setImageResource(images[0]);
}
else if(file_extention.equals("txt"))
{
_img.setImageResource(images[1]);
}
if(appPreferences.getUploadValue(files[position]).equals(statusUploaded))
{
tvFileStatus.setTextColor(Color.WHITE);
tvFileStatus.setText(statusUploaded);
tvFileStatus.setBackgroundResource(R.drawable.fileview2);
}
else if (appPreferences.getUploadValue(files[position]).equals(statusUploading)) {
tvFileStatus.setTextColor(Color.BLACK);
tvFileStatus.setText(statusUploading);
tvFileStatus.setBackgroundResource(R.drawable.fileview3);
}
tv.setText(files[position]);
tvFileSize.setText("Size:" + file_sizes[position]);
tv_date.setText(file_dates[position]);
}
else
{
v=convertView;
}
return v;
}
实施 ListView
时始终使用 ViewHolder
。参考 this tutorial
我想将文本视图的文本和背景颜色从 "Not Uploaded" 更改为 "Uploading...",并且还想更改背景颜色。但是,当我单击上传按钮时,它会更改所选视图的颜色和文本,但它也会对最后一个可见项目+1 进行相同的更改。当我滚动列表时,更改效果也会在其他视图上继续移动。
这是我的自定义适配器 getview 函数的代码
public View getView(int position, View convertView, ViewGroup parent) {
View v = null;
if(convertView == null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v=inflater.inflate(R.layout.fileview,parent,false);
TextView tv = (TextView) v.findViewById(R.id.tv_file);
TextView tv_date = (TextView) v.findViewById(R.id.tv_fileDate);
TextView tvFileSize = (TextView) v.findViewById(R.id.tvFileSize);
TextView tvFileStatus = (TextView) v.findViewById(R.id.tvUploadStatus);
ImageView _img = (ImageView) v.findViewById(R.id.iv_file);
ImageView btnTrash = (ImageView) v.findViewById(R.id.btnTrash);
ImageView btnEdit = (ImageView) v.findViewById(R.id.btnEdit);
ImageView btnUpload = (ImageView) v.findViewById(R.id.btnUpload);
btnTrash.setOnClickListener(new myclickListener(context,files[position],position));
btnEdit.setOnClickListener(new myclickListener(context,files[position],position));
btnUpload.setOnClickListener(new myclickListener(context,files[position],position));
int index = files[position].lastIndexOf(".");
String file_extention = files[position].substring(index+1,files[position].length());
file_extention=file_extention.toLowerCase();
if(file_extention.equals("png"))
{
_img.setImageResource(images[0]);
}
else if(file_extention.equals("txt"))
{
_img.setImageResource(images[1]);
}
if(appPreferences.getUploadValue(files[position]).equals(statusUploaded))
{
tvFileStatus.setTextColor(Color.WHITE);
tvFileStatus.setText(statusUploaded);
tvFileStatus.setBackgroundResource(R.drawable.fileview2);
}
else if (appPreferences.getUploadValue(files[position]).equals(statusUploading)) {
tvFileStatus.setTextColor(Color.BLACK);
tvFileStatus.setText(statusUploading);
tvFileStatus.setBackgroundResource(R.drawable.fileview3);
}
tv.setText(files[position]);
tvFileSize.setText("Size:" + file_sizes[position]);
tv_date.setText(file_dates[position]);
}
else
{
v=convertView;
}
return v;
}
实施 ListView
时始终使用 ViewHolder
。参考 this tutorial