我正在尝试CheckBox
. 但是当我点击复选框然后我滚动我的列表时,我看到在下一个项目列表中有一些复选框被选中。
这是图像
这是代码:
package com.tsdv.wds.GUI.Server;
import java.io.File;
import java.util.ArrayList;
import com.tsdv.wds.R;
import com.tsdv.wds.manager.FileItems;
import com.tsdv.wds.manager.FileManager;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class ItemFileAdapter2 extends BaseAdapter implements OnClickListener{
private static final String TAG = "ItemFileAdapter";
Context context;
FileManager fileManager;
ArrayList<FileItems> arrayList;
LayoutInflater inflater;
ViewHolder holder;
int mPosition;
public ItemFileAdapter2(Context context, ArrayList<FileItems> arrayList, FileManager fileManager) {
super();
inflater = LayoutInflater.from(context);
this.context = context;
this.fileManager = fileManager;
this.arrayList = arrayList;
Log.d(TAG, "create " + this.arrayList.size());
//holder = new ViewHolder();
}
@Override
public int getCount() {
return arrayList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return arrayList.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
mPosition = position;
holder = new ViewHolder();
if (null == convertView){
convertView = inflater.inflate(R.layout.item_custom_lv_serverfile, null);
convertView.setTag(holder);
Log.d(TAG, "Position: " + position);
} else{
holder = (ViewHolder) convertView.getTag();
}
holder.llFilenameAndInfo = (LinearLayout) convertView.findViewById(R.id.llFilenameAndInfo);
holder.llFilenameAndInfo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
File path = new File(arrayList.get(position).getParent()
.toString()
+ File.separator
+ arrayList.get(position).getName().toString());
ServerMainActivity.setPath(path);
arrayList = fileManager.displayDirectory(
arrayList.get(position).getParent(),
arrayList.get(position).getName());
ItemFileAdapter2.this.notifyDataSetChanged();
}
});
holder.imvThumbnail = (ImageView) convertView.findViewById(R.id.imvThumbnail);
holder.imvThumbnail.setImageBitmap(arrayList.get(position).getThumnail());
holder.tvName = (TextView) convertView.findViewById(R.id.tvName);
holder.tvName.setText(arrayList.get(position).getName());
holder.tvInfo = (TextView) convertView.findViewById(R.id.tvInfo);
holder.tvInfo.setText("Info");
holder.cbRead = (CheckBox) convertView.findViewById(R.id.cbRead);
holder.cbRead.setVisibility(View.VISIBLE);
holder.cbWrite = (CheckBox) convertView.findViewById(R.id.cbWrite);
holder.cbWrite.setVisibility(View.VISIBLE);
if (!arrayList.get(position).isDirectory()){
Log.d(TAG, "invisible: " + position);
holder.cbRead.setVisibility(View.INVISIBLE);
holder.cbWrite.setVisibility(View.INVISIBLE);
}
return convertView;
}
private class ViewHolder {
LinearLayout llFilenameAndInfo;
TextView tvName;
TextView tvInfo;
ImageView imvThumbnail;
CheckBox cbRead;
CheckBox cbWrite;
}
@Override
public void onClick(View v) {
if (arrayList.get(mPosition).isDirectory()) {
File path = new File(arrayList.get(mPosition).getParent().toString() + File.separator + arrayList.get(mPosition).getName().toString());
ServerMainActivity.setPath(path);
arrayList = fileManager.displayDirectory(arrayList.get(mPosition).getParent(), arrayList.get(mPosition).getName());
ItemFileAdapter2.this.notifyDataSetChanged();
}
}
}