2017-05-21 52 views
1

嘿家伙我的问题是一个字的第一个字母不显示在我的ListView,但它显示一个空白。我怎样才能解决这个问题 。我相信我的问题是在charAt .Sorry我是新手charAt在java不工作,字的第一个字母不显示

这里是我的代码

@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

它是一个unicode字符串吗?你是否也有其他角色的问题(比如'charat(2)')? –

+0

即时通讯在我的listview textdrawable https://github.com/amulyakhare/TextDrawable中使用这个libray,但我的问题即时通讯不使用因此,charat功能不能很好地工作,它显示在我的列表视图而不是字母空白。即时通讯使用而不是

+0

是'PlacesList'自定义类型?如果是的话,定义一个方法返回特定索引处的字符,就像你想要的那样。 –

回答

0

charAt的返回类型不应该是char[],而应该是char,因为您刚刚在位置上返回了char

另外,你从什么字符串得到的字符?它应该看起来像这样:

public char charAt(int i) { 
    char[] array = yourString.toCharArray(); 
    return array[i]; 
} 
+0

非常感谢你的工作,yehey。 –

0

我想你是对你的charAt总是返回空白。因为它是一个新的字符数组,我很惊讶它不会抛出IndexOutOfBoundException,但无论如何。你应该在你的PlacesList中有一些char[]和一个给它一些值的构造函数,然后得到该数组的第一个字符。

相关问题