I am creating a custom list view with favorite functionality, but I don't know how to change favorite image background on click. When I simply change the background of favorite icon than it automatically change background of another favorite image background at the time of scrolling. Please check below code :
public class CustomArrayAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater = null;
ArrayList<Customlist> list;
DecimalFormat formatter = new DecimalFormat("#,##,###");
public CustomArrayAdapter(Activity a, ArrayList<Customlist> list) {
activity = a;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.list = list;
}
public int getCount() {
return list.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView,
final ViewGroup parent) {
TextView txt_unit, txt_state, txt_price, term_left, customr;
TextView install_date;
final ImageView fav;
View view = convertView;
if (convertView == null)
view = inflater.inflate(R.layout.list_item, null);
customr = (TextView) view.findViewById(R.id.customr);
txt_state = (TextView) view.findViewById(R.id.txt_state);
install_date = (TextView) view.findViewById(R.id.install_date);
term_left = (TextView) view.findViewById(R.id.term_left);
txt_price = (TextView) view.findViewById(R.id.txt_price);
fav = (ImageView) view.findViewById(R.id.fav);
txt_unit = (TextView) view.findViewById(R.id.txt_unit);
fav.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// fav = (ImageView)v.findViewById(R)
fav.setBackgroundResource(R.drawable.favourite_select);
Toast.makeText(activity, "click", 1).show();
}
});
// set values
customr.setText(list.get(position).getCUSTOMER());
txt_state.setText(list.get(position).getSTATE_NAME());
install_date.setText(list.get(position).getINSTALL_DATE());
term_left.setText(list.get(position).getTREM_LEFT());
String price = formatter.format(Integer.parseInt(list.get(position)
.getRUPEES()));
return view;
}
}
First, you need to implement the adapter on ViewHolder pattern:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHoldler holder = null;
if (convertView == null) {
convertView = LayoutInflater.from(ctx).inflate(
R.layout.frag_home_gridview_item, null, false);
holder = new ViewHoldler();
holder.iv = (ImageView) convertView
.findViewById(R.id.gridview_item_label);
holder.tv = (TextView) convertView
.findViewById(R.id.gridview_item_name);
convertView.setTag(holder);
} else {
holder = (ViewHoldler) convertView.getTag();
}
holder.tv.setText(getItem(position));
holder.iv.setImageResource(this.ids[position]);
return convertView;
}
private class ViewHoldler {
ImageView iv;
TextView tv;
}
Second, use partial refreshment mechanism to change the target View's background:
private void refreshPartially(int position){
int firstVisiblePosition = listview.getFirstVisiblePosition();
int lastVisiblePosition = listview.getLastVisiblePosition();
if(position>=firstVisiblePosition && position<=lastVisiblePosition){
View view = listview.getChildAt(position - firstVisiblePosition);
if(view.getTag() instanceof ViewHolder){
ViewHolder vh = (ViewHolder)view.getTag();
//holder.play.setBackgroundResource(resId);//Do something here.
...
}
}
}
Third, add AdapterView.OnItemClickListener to your ListView:
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
refreshPartially(position);
}
});
You need to implement the adapter on ViewHolder pattern:
http://www.vogella.com/tutorials/AndroidListView/article.html
Related
public class CustomAdapter extends ArrayAdapter<Plan> implements View.OnClickListener {
private ArrayList<Plan> planArrayList;
int position;
Context context;
private static class ViewHolder{
TextView workType;
TextView zone;
TextView division;
TextView station;
TextView Msg;
}
public CustomAdapter(ArrayList<Plan> plan,Context context){
super(context,R.layout.plan_list,plan);
this.planArrayList=plan;
this.context=context;
}
#Override
public void onClick(View v) {
}
private int lastPosition = -1;
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
Plan plans = getItem(position);
ViewHolder viewHolder;
final View result;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null){
viewHolder = new ViewHolder();
//LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.plan_list,parent,false);
viewHolder.workType = (TextView) convertView.findViewById(R.id.plan_worktype);
viewHolder.zone = (TextView) convertView.findViewById(R.id.plan_zone);
viewHolder.division = (TextView) convertView.findViewById(R.id.plan_division);
viewHolder.station = (TextView) convertView.findViewById(R.id.plan_station);
viewHolder.Msg = (TextView) convertView.findViewById(R.id.)
result=convertView;
convertView.setTag(viewHolder);
}else {
viewHolder = (ViewHolder) convertView.getTag();
result = convertView;
}
Animation animation = AnimationUtils.loadAnimation(context, (position > lastPosition) ? R.anim.up_from_bottom : R.anim.down_from_top);
result.startAnimation(animation);
Plan plan = planArrayList.get(position);
lastPosition = position;
viewHolder.workType.setText(plans.getWorkType());
viewHolder.zone.setText(plans.getZone());
viewHolder.division.setText(plans.getDiv());
viewHolder.station.setText(plans.getSTA());
viewHolder.workType.setText(plan.getWorkType());
viewHolder.zone.setText(plan.getZone());
viewHolder.division.setText(plan.getDiv());
viewHolder.station.setText(plan.getSTA());
if (this.position == position){
View view2 = inflater.inflate(R.layout.list_dialog,null);
TextView worktype= (TextView) view2.findViewById(R.id.dlg_worktype);
TextView zone = (TextView) view2.findViewById(R.id.dlg_zone);
TextView div = (TextView) view2.findViewById(R.id.dlg_division);
TextView sta = (TextView) view2.findViewById(R.id.dlg_station);
TextView msg = (TextView) view2.findViewById(R.id.dlg_msg);
worktype.setText(plan.getWorkType());
zone.setText(plan.getZone());
div.setText(plan.getDiv());
sta.setText(plan.getSTA());
msg.setText(plan.getMsg());
return view2;
}
return convertView;
}
public void selectedItem(int position){
this.position=position;
}
}
When I called setOnItemClickListener on listView item those item values need to display in dialog box.
By the above code I'm getting java.lang.NullPointerException: Attempt to read from field 'android.widget.TextView com.example.nsurekha.entry_ex.CustomAdapter$ViewHolder.workType' on a null object reference
If you want to use onClick on listView
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
String title=adapter.getItem(arg2).getTitle();
String description=adapter.getItem(arg2).getDescription();
String addedby=adapter.getItem(arg2).getAddedby();
}
});
Now you can easily show dialogbox with the desired data. Hope it works for you.
I have a listview, which shows the picture, type/description of car and a checkbox. When the user now selected a car/s, I want to show them in another listview. When he unselected him from the CarListAdapter, it should also disappear from the new listview. Can I realize this with using the same Viewholder?
class CarListAdapter extends BaseAdapter
{
ArrayList<ItemsHolder> holderList;
BitmapHelper bitmapHelper = BitmapHelper.getInstance();
public CarListAdapter(ArrayList<ItemsHolder> holderList)
{
this.holderList = holderList;
}
#Override
public int getCount()
{
return holderList.size();
}
#Override
public Object getItem(int position)
{
return null;
}
#Override
public long getItemId(int position)
{
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
final ViewHolder holder;
if (convertView == null)
{
LayoutInflater inflater = LayoutInflater.from(RentingDataActivity.this);
convertView = inflater.inflate(R.layout.listview_renting_entry, null, true);
holder = new ViewHolder();
holder.rowCarId = (TextView) convertView.findViewById(R.id.rowCarId);
holder.rowImageView = (ImageView) convertView
.findViewById(R.id.rowImageView);
holder.rowCarType = (TextView) convertView.findViewById(R.id.rowCarType);
holder.rowCarDescription = (TextView) convertView
.findViewById(R.id.rowCarDescription);
holder.rowCheckbox = (CheckBox) convertView.findViewById(R.id.rowCheckBox);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
// holder.rowCheckbox.setOnCheckedChangeListener(null);
// holder.rowCheckbox.setTag(R.id.rowCheckBox, position);
}
// auto = holderList.get(position);
final ItemsHolder ih = holderList.get(position);
holder.rowImageView.setImageBitmap(bitmapHelper.getBigImage(ih.getImagePath(), 248));
holder.rowCarType.setText(ih.getCarType());
holder.rowCarDescription.setText(ih.getCarDescription());
// holder.rowCheckbox.setTag(ih);
holder.rowCarId.setText(ih.getCarId());
holder.rowCheckbox.setChecked(ih.isSelected());
holder.rowCheckbox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CheckBox checkbox = (CheckBox)v.findViewById(R.id.rowCheckBox);
// Toast.makeText(getApplicationContext(), holder.rowCarType.getText().toString(),
// Toast.LENGTH_LONG).show();
ih.setSelected(checkbox.isChecked());
}
});
return convertView;
}
}
I would add this as a comment but alas I haven't got enough points:
add to your adapter;
notifyDataSetChanged( );
This refreshes the list asynchronously.
i have used horizontal listview like this with Baseadapter.
private class ListViewItemClickListener implements
AdapterView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
if (imageview != null) {
previousImage = (ImageView) imageview
.findViewById(R.id.imageView);
previousImage.setSelected(false);
TextView previousTitle = (TextView) textview
.findViewById(R.id.title);
previousTitle.setTextColor(Color.parseColor("#33D6AD"));
previousTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
}
selected_image = (ImageView) view.findViewById(R.id.imageView);
selected_image.setSelected(true);
selected_text = (TextView) view.findViewById(R.id.title);
selected_text.setTextColor(Color.parseColor("#368AFF"));
selected_text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
select_position = position;
imageview = selected_image;
textview = selected_text;
}
}
private static String[] dataObjects = new String[] { "그랜드 캐니언",
"그레이트 베리어 리프", "디즈니 월드", "뉴질랜드 남섬", "케이프 타운", "암리차르의 황금사원",
"라스베가스", "시드니", "뉴욕", "타지마할" };
private BaseAdapter mAdapter = new BaseAdapter() {
#Override
public int getCount() {
return dataObjects.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(
R.layout.listitem, null);
}
TextView title = (TextView) convertView.findViewById(R.id.title);
title.setText(dataObjects[position]);
return convertView;
}
};
and i want to change textcolor if one item is clicked. but whenever i click one item in the listview, two item's color are changing right now. and distance between those two item is always 5. I am using convertView on the getView code. and textcolor change code is in clicklistener method. can i solve this with setTag, getTag. and if it is, how can i solve this?
Put this code in your adapter class :
private int selectedPos = -1;
public void setSelectedPosition(int pos){
selectedPos = pos;
notifyDataSetChanged();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(
R.layout.listitem, null);
}
TextView title = (TextView) convertView.findViewById(R.id.title);
title.setText(dataObjects[position]);
if (selectedPos == position) {
title.setTextColor(Color.parseColor("#368AFF"));
title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
} else {
title.setTextColor(Color.parseColor("#000000"));
}
return convertView;
}
And on item click, call method to set current position:
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
adapter.setSelectedPosition(pos);
}
Try out as below:
private class ListViewItemClickListener implements
AdapterView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
mPager.setCurrentItem(position);
selected_image = (ImageView) view.findViewById(R.id.imageView);
selected_image.setSelected(true);
selected_text = (TextView) view.findViewById(R.id.title);
selected_text.setTextColor(Color.parseColor("#368AFF"));
selected_text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
select_position = position;
imageview = selected_image;
textview = selected_text;
view.setTag((Integer) position);
}
}
Try this
convertView.setOnClickListener
instead of
onItemClick
I have found a mysterious problem that may be a bug!
I have a list in my fragment. Each row has a button. List shouldn't respond to click however buttons are clickable.
In order to get which button has clicked I have created a listener and implement it in my fragment. This is the code of my adapter.
public class AddFriendsAdapter extends BaseAdapter {
public interface OnAddFriendsListener {
public void OnAddUserClicked(MutualFriends user);
}
private final String TAG = "*** AddFriendsAdapter ***";
private Context context;
private OnAddFriendsListener listener;
private LayoutInflater myInflater;
private ImageDownloader imageDownloader;
private List<MutualFriends> userList;
public AddFriendsAdapter(Context context) {
this.context = context;
myInflater = LayoutInflater.from(context);
imageDownloader = ImageDownloader.getInstance(context);
}
public void setData(List<MutualFriends> userList) {
this.userList = userList;
Log.i(TAG, "List passed to the adapter.");
}
#Override
public int getCount() {
try {
return userList.size();
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = myInflater.inflate(R.layout.list_add_friends_row, null);
holder = new ViewHolder();
Typeface font = Typeface.createFromAsset(context.getAssets(), "fonts/ITCAvantGardeStd-Demi.ttf");
holder.tvUserName = (TextView) convertView.findViewById(R.id.tvUserName);
holder.tvUserName.setTypeface(font);
holder.ivPicture = (ImageView) convertView.findViewById(R.id.ivPicture);
holder.btnAdd = (Button) convertView.findViewById(R.id.btnAdd);
holder.btnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.e(TAG, "Item: " + position);
listener.OnAddUserClicked(userList.get(position));
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvUserName.setText(userList.get(position).getName());
imageDownloader.displayImage(holder.ivPicture, userList.get(position).getPhotoUrl());
return convertView;
}
public void setOnAddClickedListener(OnAddFriendsListener listener) {
this.listener = listener;
}
static class ViewHolder {
TextView tvUserName;
ImageView ivPicture;
Button btnAdd;
}
}
When I run the app, I can see my rows however since my list is long and has over 200 items when i goto middle of list and click an item then returned position is wrong (it's something like 7, sometimes 4 and etc.).
Now what is the mystery?
If I active on item listener of list from my fragment and click on row then correct row position will be displayed while on that row if I click on button then wrong position will be displayed.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.e(TAG, "item " + position + " clicked.");
}
});
Result in logcat:
05-09 10:22:25.228: E/AddFriendsFragment(20296): item 109 clicked.
05-09 10:22:34.453: E/*** AddFriendsAdapter ***(20296): Item: 0
Any suggestion would be appreciated. Thanks
Because the convertView and holder will be recycled to use, move your setOnClickListener out of the if else statement:
if (convertView == null) {
convertView = myInflater.inflate(R.layout.list_add_friends_row, null);
holder = new ViewHolder();
Typeface font = Typeface.createFromAsset(context.getAssets(), "fonts/ITCAvantGardeStd-Demi.ttf");
holder.tvUserName = (TextView) convertView.findViewById(R.id.tvUserName);
holder.tvUserName.setTypeface(font);
holder.ivPicture = (ImageView) convertView.findViewById(R.id.ivPicture);
holder.btnAdd = (Button) convertView.findViewById(R.id.btnAdd);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.btnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
Log.e(TAG, "Item: " + position);
listener.OnAddUserClicked(userList.get(position));
}
});
It is not the best solution for that,because there will be some performance issue. I suggest you create a Map for your view and create a new view for your item then just use the relative view for each view.
I think it will be a better solution with best performance:
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = myInflater.inflate(R.layout.list_add_friends_row, null);
holder = new ViewHolder();
Typeface font = Typeface.createFromAsset(context.getAssets(), "fonts/ITCAvantGardeStd-Demi.ttf");
holder.tvUserName = (TextView) convertView.findViewById(R.id.tvUserName);
holder.tvUserName.setTypeface(font);
holder.ivPicture = (ImageView) convertView.findViewById(R.id.ivPicture);
holder.btnAdd = (Button) convertView.findViewById(R.id.btnAdd);
holder.btnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Integer pos = (Integer)v.getTag();
Log.e(TAG, "Item: " + pos);
listener.OnAddUserClicked(userList.get(pos));
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvUserName.setText(userList.get(position).getName());
imageDownloader.displayImage(holder.ivPicture, userList.get(position).getPhotoUrl());
holder.btnAdd.setTag(position);
return convertView;
}
You can also manage your view by yourself. Create every unique view for your item, don't recycle view.
//member various
private Map<Integer, View> myViews = new HashMap<Integer, View>();
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
View view = myViews.get(position);
if (view == null) {
view = myInflater.inflate(R.layout.list_add_friends_row, null);
//don't need use the holder anymore.
Typeface font = Typeface.createFromAsset(context.getAssets(), "fonts/ITCAvantGardeStd-Demi.ttf");
holder.tvUserName = (TextView) convertView.findViewById(R.id.tvUserName);
holder.tvUserName.setTypeface(font);
holder.ivPicture = (ImageView) convertView.findViewById(R.id.ivPicture);
holder.btnAdd = (Button) convertView.findViewById(R.id.btnAdd);
holder.btnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Integer pos = (Integer)v.getTag();
Log.e(TAG, "Item: " + pos);
listener.OnAddUserClicked(userList.get(pos));
}
});
holder.tvUserName.setText(userList.get(position).getName());
imageDownloader.displayImage(holder.ivPicture,
userList.get(position).getPhotoUrl());
myViews.put(position, view);
}
return view;
}
Did you tried doing something like this:
holder.btnAdd.setTag(Integer.valueOf(position));
And then retrieve wich row was clicked in the callback for the button, like this:
public void btnAddClickListener(View view)
{
position = (Integer)view.getTag();
Foo foo = (Foo)foos_adapter.getItem(position); //get data of row(position)
//do some
}
Another approach I found useful (if you are using the ViewHolder pattern of course) is to set the index on a separate attribute whenever getView() is called, then inside your onClickListener you just have to reference your holder's position attribute, something like this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if(convertView == null){
convertView = View.inflate(mContext, R.layout.contact_picker_row,null);
holder = new ViewHolder();
holder.body = (RelativeLayout)convertView.findViewById(R.id.numberBody);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}
holder.position = position;
holder.body.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(mContext,"Clicked on: "+holder.position,Toast.LENGTH_LONG).show();
}
});
return convertView;
}
private class ViewHolder{
RelativeLayout body;
int position;
}
I'm having a very strange problem with my custom listview.
This Listview shows a lot of names and some more data, and each item haves an onClick listener, and when i press in one item, that item get's the background color changued to RED. ( arg1.setBackgroundColor(Color.RED); )
But something isn't working properly, because if i scrolldown the listview, another item appears colored on near of the bottom part of the list.
this is my code:
public class StartingSquad extends Activity {
public static List<Player> Players = new ArrayList<Player>();
public int selectedPosition=-1;
ListView l1;
private TextView TeamPowerValue=null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyApplication.updateStartingSquadOnPlayers(MyApplication.getPlayerTeam(),this);//actualizo los titulares
Players=MyApplication.getPlayerTeam().getPlayers();
setContentView(R.layout.startingsquad);
TeamPowerValue = (TextView) findViewById(R.id.TeamPowerValue);
l1 = (ListView) findViewById(R.id.ListView01);
l1.setAdapter(new EfficientAdapter(this));
TeamPowerValue.setText(""+MyApplication.getPlayerTeam().getPower());
l1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
//Toast.makeText(getBaseContext(), "You clciked "+Players.get(arg2).getName(), Toast.LENGTH_SHORT).show();
if (selectedPosition==-1) //si no hay ningun item seleccionado ya
{
arg1.setSelected(true);
//arg1.setBackgroundResource(R.drawable.bg3);
arg1.setBackgroundColor(Color.RED);
selectedPosition=arg2;
}
else //si ya habiamos seleccionado un jugador
{
MyApplication.getPlayerTeam().changePlayerPositions(selectedPosition, arg2);
selectedPosition=-1;
MyApplication.updateStartingSquadOnPlayers(MyApplication.getPlayerTeam(),StartingSquad.this);//actualizo los titulares
MyApplication.getPlayerTeam().calculatePower();
TeamPowerValue.setText(""+MyApplication.getPlayerTeam().getPower());
l1.setAdapter(new EfficientAdapter(StartingSquad.this));
}
}
});
}
private static class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return Players.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.startingsquadlistview, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.PlayerPosition);
holder.text2 = (TextView) convertView.findViewById(R.id.PlayerName);
holder.text3 = (TextView) convertView.findViewById(R.id.PlayerPower);
holder.text4 = (TextView) convertView.findViewById(R.id.PlayerStartingSquad);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(Players.get(position).getPosition());
holder.text2.setText(Players.get(position).getName());
holder.text2.setTextColor(Players.get(position).NameColor);
holder.text3.setText(""+Players.get(position).getPower());
holder.text4.setText("XX");
return convertView;
}
static class ViewHolder {
TextView text;
TextView text2;
TextView text3;
TextView text4;
}
}
}
Do background change not in onItemSelected but in adater getView().
if(position == selectedPosition) {
convertView.setBackgroundColor(context.getResources().getColor(R.color.red));
} else {
convertView.setBackgroundColor(context.getResources().getColor(R.color.default));
}
Try to remove this line:
l1.setAdapter(new EfficientAdapter(StartingSquad.this));
If you can't, tell me why?
One more thing: Edit this function:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
View convertViews;
if (convertView == null) {
convertViews = mInflater.inflate(R.layout.startingsquadlistview, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.PlayerPosition);
holder.text2 = (TextView) convertView.findViewById(R.id.PlayerName);
holder.text3 = (TextView) convertView.findViewById(R.id.PlayerPower);
holder.text4 = (TextView) convertView.findViewById(R.id.PlayerStartingSquad);
convertViews.setTag(holder);
} else {
convertViews = convertView;
holder = (ViewHolder) convertViews.getTag();
}
holder.text.setText(Players.get(position).getPosition());
holder.text2.setText(Players.get(position).getName());
holder.text2.setTextColor(Players.get(position).NameColor);
holder.text3.setText(""+Players.get(position).getPower());
holder.text4.setText("XX");
return convertViews;
}