ListView
中没有显示单词的第一个字母,但它显示空白。我怎样才能解决这个问题 。我相信我的问题出在charAt
。抱歉,我是新手
这是我的代码
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.listview_item, null);
holder = new ViewHolder();
holder.imageView = (ImageView)convertView.findViewById(R.id.image_View);
holder.SubjectName = (TextView) convertView.findViewById(R.id.textView1);
holder.SubjectFullForm = (TextView) convertView.findViewById(R.id.textviewFullForm);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
PlacesList subject = SubjectListTemp.get(position);
holder.SubjectName.setText(subject.getSubName());
holder.SubjectFullForm.setText(subject.getSubFullForm());
String firstLetter = String.valueOf(getItem(position).charAt(0));
ColorGenerator generator = ColorGenerator.MATERIAL; // or use DEFAULT
// generate random color
int color = generator.getColor(getItem(position));
TextDrawable drawable = TextDrawable.builder()
.buildRound(firstLetter, color); // radius in px
holder.imageView.setImageDrawable(drawable);
return convertView;
}
这是我的PlacesList.java
public class PlacesList {
String SubName = null;
String SubFullForm = null;
public PlacesList(String Sname, String SFullForm) {
super();
this.SubName = Sname;
this.SubFullForm = SFullForm;
}
public String getSubName() {
return SubName;
}
public void setSubName(String code) {
this.SubName = code;
}
public String getSubFullForm() {
return SubFullForm;
}
public void setSubFullForm(String name) {
this.SubFullForm = name;
}
@Override
public String toString() {
return SubName + " " + SubFullForm ;
}
public char[] charAt(int i) {
return new char[0];
}
}
答案 0 :(得分:0)
我认为你是对的charAt
总是返回空白。因为它是一个新的char数组,我很惊讶它不会抛出IndexOutOfBoundException
但是无论如何。你应该在你的char[]
中有一些PlacesList
和一个给它一些值的构造函数,然后得到那个数组的第一个字符。
答案 1 :(得分:0)
charAt
的返回类型不应该是char[]
,但应该是char
,因为您只是在该位置返回char
。
另外,你从哪个字符串获取char?看起来应该是这样的:
public char charAt(int i) {
char[] array = yourString.toCharArray();
return array[i];
}