嗨同志我想在段落输出yu date(13.4.2014):
主要
public class MainActivity extends Activity implements OnItemClickListener {
public static final String[] titles = new String[] { "Strawberry",
"Banana", "Orange", "Mixed" };
public static final Integer[] images = { R.drawable.news_icon_1,
R.drawable.news_icon_2, R.drawable.news_icon_1, R.drawable.news_icon_2 };
ListView listView;
List<RowItem> rowItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rowItems = new ArrayList<RowItem>();
for (int i = 0; i < titles.length; i++) {
RowItem item = new RowItem(images[i], titles[i], new GregorianCalendar(2011, 11, i).getTime());
rowItems.add(item);
}
listView = (ListView) findViewById(R.id.list);
CustomListViewAdapter adapter = new CustomListViewAdapter(this,
R.layout.list_item, rowItems);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast toast = Toast.makeText(getApplicationContext(),
"Item " + (position + 1) + ": " + rowItems.get(position),
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
}
}
CustomListViewAdapter
public class CustomListViewAdapter extends ArrayAdapter {
Context context;
public CustomListViewAdapter(Context context, int resourceId,
List<RowItem> items) {
super(context, resourceId, items);
this.context = context;
}
/*private view holder class*/
private class ViewHolder {
ImageView imageView;
TextView txtTitle;
TextView txtDesc;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
RowItem rowItem = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.txtDesc.setText((CharSequence) rowItem.getPostDate());
holder.txtTitle.setText(rowItem.getTitle());
holder.imageView.setImageResource(rowItem.getImageId());
return convertView;
}
}
RowItem
public class RowItem {
private int imageId;
private String title;
private Date postDate;
public RowItem(int imageId, String title, Date postDate) {
this.imageId = imageId;
this.title = title;
this.postDate = postDate;
}
public int getImageId() {
return imageId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
public Date getPostDate() {
return postDate;
}
public void setPostDate(Date postDate) {
this.postDate = postDate;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public String toString() {
return title + "\n" + postDate;
}
}
我收到错误:
04-13 07:42:13.559 862-862 / com.example.listview E / AndroidRuntime:FATAL EXCEPTION:main java.lang.ClassCastException:java.util.Date无法强制转换为java.lang.CharSequence
为什么?,以及如何约会?
答案 0 :(得分:0)
你有
holder.txtDesc.setText((CharSequence) rowItem.getPostDate());
您正在向Date
投射CharacterSequence
。 setText
将CharacterSequence
作为参数。
您需要做的是将日期转换为字符串,然后将其设置为textview
Convert java.util.Date to String
Format formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String datestring = formatter.format(rowItem.getPostDate());
holder.txtDesc.setText(datestring);