Mobin Vahdati Mobin Vahdati - 3 months ago 12
Android Question

An Issue In List View Adapter

I Have A ListView In Dialog & I Want Set An Item TextColor In ListView To Brown So I Use This Adapter For My ListView :

public class RankingListAdapter extends BaseAdapter {

private Context context;
private ArrayList<RankingItem> RankingItems;
int Po;

public RankingListAdapter(Context context, ArrayList<RankingItem> navDrawerItems, int p) {
this.context = context;
this.RankingItems = navDrawerItems;
this.Po = p;
}

@Override
public int getCount() {
return RankingItems.size();
}

@Override
public Object getItem(int position) {
return RankingItems.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.ranking_list_item, null);
}

TextView TVRank = (TextView) convertView.findViewById(R.id.TVRank);
TextView TVUser = (TextView) convertView.findViewById(R.id.TVUser);
TextView TVLVL = (TextView) convertView.findViewById(R.id.TVLVL);
TextView TVScore = (TextView) convertView.findViewById(R.id.TVScore);
Typeface TEng = Typeface.createFromAsset(context.getAssets(), "fonts/English.ttf");
Typeface TNum = Typeface.createFromAsset(context.getAssets(), "fonts/Nums.ttf");

TVRank.setText(RankingItems.get(position).getRank());
TVRank.setTypeface(TNum);
TVUser.setText(RankingItems.get(position).getUsername());
TVUser.setTypeface(TEng);
TVLVL.setText(RankingItems.get(position).getLVL());
TVLVL.setTypeface(TNum);
TVScore.setText(RankingItems.get(position).getScore());
TVScore.setTypeface(TNum);

if (position == Po) {
TVRank.setTextColor(Color.parseColor("#b38620"));
TVUser.setTextColor(Color.parseColor("#b38620"));
TVLVL.setTextColor(Color.parseColor("#b38620"));
TVScore.setTextColor(Color.parseColor("#b38620"));
}

return convertView;
}
}


But When I Run My App I See This :
enter image description here

enter image description here

What Is The Problem ?

Answer

Write else condition also for color

 if (position == Po) {
    TVRank.setTextColor(Color.parseColor("#b38620"));
    TVUser.setTextColor(Color.parseColor("#b38620"));
    TVLVL.setTextColor(Color.parseColor("#b38620"));
    TVScore.setTextColor(Color.parseColor("#b38620"));
}
else
{
    //set default color of your texts.
    TVRank.setTextColor(Color.BLACK);
    TVUser.setTextColor(Color.BLACK);
    TVLVL.setTextColor(Color.BLACK);
    TVScore.setTextColor(Color.BLACK);        
}