问题
I have a listView that currently displays the names of images along with a thumb of the image beside it. I highlight the selected textBox green onClick, but while scrolling through the list other items are highlighted as well.
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);
}
}
CLICK LISTENER:
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);
*/
}
}
回答1:
You need two item view types: one for the selected item and one for all the unselected ones. The adapter will automatically take care of only passing the correct type into your getView
method.
At the moment, your adapter knows of only one type of view, so it will just pass any recycled view it has available into your getView
method - some of which may still have the green highlight.
You need to implement getItemViewType and getViewTypeCount in your adapter and it will work.
Edit
I'm bored right now so here's what it should be like: :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;
}
来源:https://stackoverflow.com/questions/22386334/cant-get-parent-position-from-convert-view