public View getView(int position, View convertView, ViewGroup parent){
View V = convertView;
if(V == null){
LayoutInflater vi =
(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
V = vi.inflate(R.layout.barlist_row, null);
}
BarListItem ci = mBarList.get(position);
double likes_cnt = ci.getLikes();
double dislikes_cnt = ci.getDislikes();
TextView likes = (TextView)V.findViewById(R.id.prg_likes);
TextView dislikes = (TextView)V.findViewById(R.id.prg_dislikes);
TextView title = (TextView)V.findViewById(R.id.txt_title);
TextView distance = (TextView)V.findViewById(R.id.txt_distance);
if(likes_cnt > dislikes_cnt){
Resources r = getResources();
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 150, r.getDisplayMetrics());
likes.getLayoutParams().width = Math.round(px);
px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 150*((float)ci.getDislikes()/(float)ci.getLikes()), r.getDisplayMetrics());
dislikes.getLayoutParams().width = Math.round(px);
}else{
Resources r = getResources();
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 150, r.getDisplayMetrics());
dislikes.getLayoutParams().width = Math.round(px);
px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,150*((float)ci.getLikes()/(float)ci.getDislikes()), r.getDisplayMetrics());
likes.getLayoutParams().width = Math.round(px);
}
title.setText(ci.getTitle());
likes.setText(" " + String.valueOf(ci.getLikes()));
dislikes.setText(" " + String.valueOf(ci.getDislikes()));
distance.setText(String.valueOf(ci.getDistance()) + " miles");
return V;
}
所以对于这个应用程序我已经制作了一个自定义列表适配器。一切都运行得很好,但我的listView的每一行内的textView的宽度都不正确。
这些textViews的目的是用图像和文字来说明,喜欢和不喜欢的是吧。我无法在问题中插入图像,但我会尝试解释。我正在使用150 dp。因此,如果一个酒吧有一个喜欢和0不喜欢,它应该看起来像这样
喜欢-------------- 恶注意没有什么可以表示不喜欢。
如果一个酒吧有5个喜欢而且1个不喜欢它会与150 dp SO成比例:
喜欢---------------
恶---
问题是textViews有时是正确的,然后随机出现如下错误的插图:
likes = 10
dilikes = 0
喜欢-------
不喜欢------------------
OR
喜欢 - 0
不喜欢 - 0
喜欢--------------------
不喜欢---------
解决方案:
if(dislikes_cnt < 1){
dislikes.setVisibility(View.GONE);
}
else{
dislikes.setVisibility(View.VISIBLE);
}
if(likes_cnt < 1){
likes.setVisibility(View.GONE);
}
else{
likes.setVisibility(View.VISIBLE);
}
而不是尝试将喜欢的宽度设置为1或0,或者不喜欢为0.我不会将视图设置为GONE和VISIBLE,而是按照我的意图完成。感谢您的评论!
最终代码:
public View getView(int position, View convertView, ViewGroup parent){
View V = convertView;
if(V == null){
LayoutInflater vi =
(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
V = vi.inflate(R.layout.barlist_row, null);
}
BarListItem ci = mBarList.get(position);
long likes_cnt = ci.getLikes();
long dislikes_cnt = ci.getDislikes();
TextView likes = (TextView)V.findViewById(R.id.prg_likes);
TextView dislikes = (TextView)V.findViewById(R.id.prg_dislikes);
TextView title = (TextView)V.findViewById(R.id.txt_title);
TextView distance = (TextView)V.findViewById(R.id.txt_distance);
if(likes_cnt > dislikes_cnt){
Resources r = getResources();
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 150, r.getDisplayMetrics());
likes.getLayoutParams().width = Math.round(px);
px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 150*((float)dislikes_cnt/(float)likes_cnt), r.getDisplayMetrics());
dislikes.getLayoutParams().width = Math.round(px);
}else{
Resources r = getResources();
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 150, r.getDisplayMetrics());
dislikes.getLayoutParams().width = Math.round(px);
px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,150*((float)likes_cnt/(float)dislikes_cnt), r.getDisplayMetrics());
likes.getLayoutParams().width = Math.round(px);
}
if(dislikes_cnt < 1){
dislikes.setVisibility(View.GONE);
}
else{
dislikes.setVisibility(View.VISIBLE);
}
if(likes_cnt < 1){
likes.setVisibility(View.GONE);
}
else{
likes.setVisibility(View.VISIBLE);
}
title.setText(ci.getTitle());
likes.setText(" " + String.valueOf(ci.getLikes()));
dislikes.setText(" " + String.valueOf(ci.getDislikes()));
distance.setText(String.valueOf(ci.getDistance()) + " miles");
return V;
}