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);
*/
}
}