在Android中显示字符串的第一个字母到另一个textview

时间:2016-11-04 13:03:35

标签: android listview

  

我正在显示一个列表视图,其中行项目有两个textview。第二个textview用于公司名称,第一个textview用于公司的起始信。如何实现这一目标?需要帮助!!

public class ExampleAdapter extends BaseAdapter {

Context context;
ArrayList<ExamplePojo> items = new ArrayList<>();

public ExampleAdapter(Context context, ArrayList<ExamplePojo> items) {
    this.context = context;
    this.items = items;
}

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

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

@Override
public long getItemId(int position) {
    return items.indexOf(items.get(position));
}

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

    ViewHolder holder = null;
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (convertView == null){
        convertView = inflater.inflate(R.layout.example_row_item, null);
        holder = new ViewHolder();
        holder.txtSentence = (TextView) convertView.findViewById(R.id.txtSentence);
        holder.txtInitialLetter = (TextView) convertView.findViewById(R.id.txtInitialLetter);
        convertView.setTag(holder);
    }
    else {
        holder = (ViewHolder) convertView.getTag();
    }

    final ExamplePojo pojo = items.get(position);
    holder.txtSentence.setText(pojo.getSentence());
    holder.txtInitialLetter.setText(pojo.getSentence().charAt(0));
    return convertView;
}

public class ViewHolder{

    TextView txtSentence, txtInitialLetter;

}

}

1 个答案:

答案 0 :(得分:1)

这是用于显示第一个textview中的第一个字符和其他textview中的完整名称的代码。还在if(convertView == null)块中放入以下行

final ExamplePojo pojo = items.get(position);
holder.txtSentence.setText(pojo.getSentence());
holder.txtInitialLetter.setText(pojo.getSentence().substring(0, 1));