我有一个listView,它当前显示图像的名称以及旁边图像的拇指。我突出显示所选的textBox green onClick,但在滚动列表时,其他项目也会突出显示。
ADAPTER:
public class CustomListAdapter extends BaseAdapter {
private ArrayList<String> data;
private Boolean[] isSelected;
private Activity activity;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
View vi;
public CustomListAdapter(Activity a, ArrayList<String> c){
activity = a;
data = c;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
isSelected = new Boolean[data.size()];
System.out.println("data size : " + data.size());
for(int i =0; i < isSelected.length; i++)isSelected[i] = false;
}
public int getCount(){
return data.size();
}
public Object getItem(int position){
return data.get(position);
}
public long getItemId(int position){
return position;
}
public View getView(int position, View convertView, ViewGroup parent){
vi=convertView;
if(convertView == null) // if it's not recycled, initialize some attributes
vi = inflater.inflate(R.layout.each, null);
TextView text=(TextView)vi.findViewById(R.id.text);
ImageView image=(ImageView)vi.findViewById(R.id.image);
/////////////////!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! HERE
if(isSelected[position])text.setBackgroundColor(Color.GREEN);
text.setText("image: "+ data.get(position).substring(data.get(position).lastIndexOf("/")+1, data.get(position).indexOf(".jpg")));
imageLoader.DisplayImage(data.get(position), image);
return vi;
}
public void setMy_ItemSelected(int position, Boolean tf){/////each convertview resets this value?
System.out.println("selected position size of array: " + Integer.toString(isSelected.length));
System.out.println("selected position: " + position);
if(tf){ isSelected[position] = true;}
notifyDataSetChanged();
System.out.println("selected position true/false: " + isSelected);
}
}
点击听众:
private class dataExport implements AdapterView.OnItemClickListener {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
adapter.setMy_ItemSelected(position, true);
/*FileDialog popWindow = new FileDialog(); //USB pop window for folder selection
popWindow.open_PathSelector(MainActivity.this);
*/
}
}
答案 0 :(得分:0)
您需要两种项目视图类型:一种用于所选项目,另一种用于所有未选择项目。适配器将自动处理仅将正确类型传递到getView
方法。
目前,您的适配器只知道一种类型的视图,因此它只会将其可用的任何回收视图传递到您的getView
方法中 - 其中一些可能仍然具有绿色突出显示。
您需要在适配器中实施getItemViewType和getViewTypeCount,它才能正常运行。
我现在很无聊,所以这里应该是这样的:D
protected static final int TYPE_NORMAL = 0;
protected static final int TYPE_SELECTED = 1;
public int getItemViewType(int position) {
return isSelected[position] ? TYPE_SELECTED : TYPE_NORMAL;
}
public int getViewTypeCount() {
return 2;
}