简单的自定义适配器,使用一个布局,其中包含一个“分隔符”文本视图,当我们到达字母顺序列表的下一个“字母”时显示,或者如果没有则隐藏。问题是textview显示每个项目,并显示数字而不是字母。有什么建议?感谢。
来自适配器:
public int getCount() {
// TODO Auto-generated method stub
return myArr.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
/*private view holder class*/
private class ViewHolder {
public TextView separator;
public ImageView imageView;
public TextView birdId;
public TextView birdName;
public TextView familyName;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
boolean needSeparator = false;
String firstLetter;
String firstLetterPrevious;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.bird_entry, null);
holder = new ViewHolder();
holder.separator = (TextView) convertView.findViewById(R.id.separator);
holder.imageView = (ImageView) convertView.findViewById(R.id.list_image);
holder.birdId = (TextView) convertView.findViewById(R.id.birdId);
holder.birdName = (TextView) convertView.findViewById(R.id.birdName);
holder.familyName = (TextView) convertView.findViewById(R.id.familyName);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// Determine points where a separator is needed: At the beginning and any
// time the first letter of the bird name alphabetically increments
firstLetter = myArr.get(position).get("fullName").substring(0,1);
if (position == 0) {
needSeparator = true;
} else {
firstLetterPrevious = myArr.get(position-1).get("fullName").substring(0,1);
if (!firstLetter.equals(firstLetterPrevious)){
needSeparator = true;
}
}
if (needSeparator) {
holder.separator.setText(firstLetter);
holder.separator.setVisibility(View.VISIBLE);
} else {
holder.separator.setVisibility(View.GONE);
}
int ResID = context.getResources().getIdentifier(myArr.get(position).get("alphaCode"), "drawable", context.getPackageName());
holder.imageView.setImageResource(ResID);
holder.birdId.setText(myArr.get(position).get("birdId"));
holder.birdName.setText(myArr.get(position).get("fullName"));
holder.familyName.setText(myArr.get(position).get("family"));
return convertView;
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
style="?android:attr/listSeparatorTextViewStyle"
android:id="@+id/separator"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_selector"
android:orientation="horizontal"
android:padding="5dip" >
<TextView
android:id="@+id/birdId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<!-- Left side Thumbnail image -->
<LinearLayout android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:layout_alignParentLeft="true"
android:background="@drawable/image_bg"
android:layout_marginRight="5dip">
<ImageView
android:id="@+id/list_image"
android:layout_width="75dip"
android:layout_height="75dip"
android:contentDescription="@string/imageview_desc" />
</LinearLayout>
<!-- Bird Name -->
<TextView
android:id="@+id/birdName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/thumbnail"
android:layout_toRightOf="@+id/thumbnail"
android:textColor="#040404"
android:typeface="sans"
android:textSize="20sp"
android:textStyle="bold"
/>
<!-- Family Name -->
<TextView
android:id="@+id/familyName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/birdName"
android:textColor="#343434"
android:textSize="15sp"
android:layout_marginTop="1dip"
android:layout_toRightOf="@+id/thumbnail" />
</RelativeLayout>
</LinearLayout>
答案 0 :(得分:0)
事实证明,在RelativeLayout中移动分隔符TextView解决了这个问题。新代码如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_selector"
android:orientation="horizontal"
android:padding="5dip" >
<TextView
android:id="@+id/birdId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<TextView
style="?android:attr/listSeparatorTextViewStyle"
android:id="@+id/separator"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ff33b5e5" />
<!-- Left side Thumbnail image -->
<LinearLayout android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:layout_alignParentLeft="true"
android:layout_below="@id/separator"
android:background="@drawable/image_bg"
android:layout_marginTop="5dip"
android:layout_marginRight="5dip">
<ImageView
android:id="@+id/list_image"
android:layout_width="75dip"
android:layout_height="75dip"
android:contentDescription="@string/imageview_desc" />
</LinearLayout>
<!-- Bird Name -->
<TextView
android:id="@+id/birdName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/thumbnail"
android:layout_toRightOf="@+id/thumbnail"
android:textColor="#040404"
android:typeface="sans"
android:textSize="20sp"
android:textStyle="bold"
/>
<!-- Family Name -->
<TextView
android:id="@+id/familyName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/birdName"
android:textColor="#343434"
android:textSize="15sp"
android:layout_marginTop="1dip"
android:layout_toRightOf="@+id/thumbnail" />
</RelativeLayout>
为什么呢?不知道。