我是Android新手,我无法找到一种方法来检索整数值并在列表视图中显示它们。
public class NewsItem {
private int content;
public int getContent() {
return content;
}
public void setContent(int content) {
this.content = content;
}
}
适配器类
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.householdincomeview, null);
viewHolder = new ViewHolder();
viewHolder.headlineView(TextView) convertView.findViewById(R.id.content);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.headlineView.
setText(arrayList.get(position).getContent());
return convertView;
}
static class ViewHolder {
TextView headlineView;
}
活动类
final ListView listView = (ListView) findViewById(R.id.viewincome);
ArrayList<NewsItem> getData = getitem();
listView.setAdapter(newHouseHoldincomeAdapter(this,getData));
private ArrayList<NewsItem> getitem() {
ArrayList<NewsItem> result = new ArrayList<NewsItem>();
NewsItem newsItem = new NewsItem();
newsItem.setContent(1);
return result;
}
listview接受字符串值,但是当我尝试在getview中检索整数时,我收到错误,因为空指针异常。如何显示整数值?
答案 0 :(得分:1)
在适配器类中,使用String.ValueOf()
方法设置文本。如下所示。
viewHolder.headlineView.setText(String.ValueOf(arrayList.get(position).getContent()));
另请查看Developer Site以获取更多信息。
答案 1 :(得分:0)
使用:
viewHolder.headlineView.setText(arrayList.get(position).getContent() + "");
或者:
viewHolder.headlineView.setText(String.valueOf(arrayList.get(position).getContent()));
答案 2 :(得分:0)
if (convertView == null)
{
convertView=layoutInflater.
inflate(R.layout.householdincomeview,null);
viewHolder=new ViewHolder();
viewHolder.headlineView=(TextView)convertView.
findViewById(R.id.content);
convertView.setTag(viewHolder);// settag
else
{
viewHolder=(ViewHolder)convertView.getTag();
}