我正在制作一个简单的自定义listView活动,我正在设置一个自定义行样式的自定义适配器但是当我运行应用程序时它给了我例外 这是我正在尝试的代码
自定义适配器
public class CustomListAdapter extends BaseAdapter {
private ArrayList listData;
private LayoutInflater layoutInflater;
Activities Act = new Activities();
public CustomListAdapter(Context context, ArrayList listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return listData.size();
}
@Override
public Object getItem(int position) {
return listData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.message_row, null);
holder = new ViewHolder();
holder.titleView = (TextView) convertView.findViewById(R.id.title_tv);
holder.timeView = (TextView) convertView.findViewById(R.id.time_tv);
holder.refView = (TextView) convertView.findViewById(R.id.ref_tv);
holder.detailsView = (TextView) convertView.findViewById(R.id.detail_tv);
holder.matterView = (TextView) convertView.findViewById(R.id.date_tv);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.titleView.setText(((Activities) listData.get(position)).getTitle());
holder.timeView.setText(((Activities) listData.get(position)).getTime());
holder.refView.setText(((Activities) listData.get(position)).getRef());
holder.detailsView.setText(((Activities) listData.get(position)).getDetails());
holder.matterView.setText(((Activities) listData.get(position)).getMatter());
return convertView;
}
static class ViewHolder {
TextView titleView;
TextView timeView;
TextView refView;
TextView detailsView;
TextView matterView;
}
}
这是带有getter setters的 Activites 类
public class Activities implements Serializable
{
private static final long serialVersionUID = 2L;
private String ref;
private String details;
private String time;
private String title;
private String matter;
private String date;
public String getMatter() {
return matter;
}
public void setMatter(String matter) {
this.matter = matter;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getDetails() {
return details;
}
public void setDetails(String details) {
this.details = details;
}
public String getRef() {
return ref;
}
public void setRef(String ref) {
this.ref = ref;
}
public String getDate() {
return date;
}
public void setDate(String ref) {
this.date = date;
}
}
这就是LogCat所说的
01-08 12:11:28.784: E/AndroidRuntime(13273): FATAL EXCEPTION: main
01-08 12:11:28.784: E/AndroidRuntime(13273): java.lang.ClassCastException: java.lang.String cannot be cast to com.example.blavo_timeinn.Activities
01-08 12:11:28.784: E/AndroidRuntime(13273): at com.example.blavo_timeinn.CustomListAdapter.getView(CustomListAdapter.java:59)
01-08 12:11:28.784: E/AndroidRuntime(13273): at android.widget.AbsListView.obtainView(AbsListView.java:2084)
01-08 12:11:28.784: E/AndroidRuntime(13273): at android.widget.ListView.measureHeightOfChildren(ListView.java:1249)
01-08 12:11:28.784: E/AndroidRuntime(13273): at android.widget.ListView.onMeasure(ListView.java:1160)
01-08 12:11:28.784: E/AndroidRuntime(13273): at android.view.View.measure(View.java:12773)
01-08 12:11:28.784: E/AndroidRuntime(13273): at android.widget.RelativeLayout.measureChild(RelativeLayout.java:579)
01-08 12:11:28.784: E/AndroidRuntime(13273): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:392)
01-08 12:11:28.784: E/AndroidRuntime(13273): at android.view.View.measure(View.java:12773)
01-08 12:11:28.784: E/AndroidRuntime(13273): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4788)
01-08 12:11:28.784: E/AndroidRuntime(13273): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
01-08 12:11:28.784: E/AndroidRuntime(13273): at android.view.View.measure(View.java:12773)
01-08 12:11:28.784: E/AndroidRuntime(13273): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4788)
01-08 12:11:28.784: E/AndroidRuntime(13273): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1374)
01-08 12:11:28.784: E/AndroidRuntime(13273): at android.widget.LinearLayout.measureVertical(LinearLayout.java:663)
01-08 12:11:28.784: E/AndroidRuntime(13273): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
01-08 12:11:28.784: E/AndroidRuntime(13273): at android.view.View.measure(View.java:12773)
01-08 12:11:28.784: E/AndroidRuntime(13273): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4788)
01-08 12:11:28.784: E/AndroidRuntime(13273): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
01-08 12:11:28.784: E/AndroidRuntime(13273): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2105)
01-08 12:11:28.784: E/AndroidRuntime(13273): at android.view.View.measure(View.java:12773)
01-08 12:11:28.784: E/AndroidRuntime(13273): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1149)
01-08 12:11:28.784: E/AndroidRuntime(13273): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2583)
01-08 12:11:28.784: E/AndroidRuntime(13273): at android.os.Handler.dispatchMessage(Handler.java:99)
01-08 12:11:28.784: E/AndroidRuntime(13273): at android.os.Looper.loop(Looper.java:137)
01-08 12:11:28.784: E/AndroidRuntime(13273): at android.app.ActivityThread.main(ActivityThread.java:4511)
01-08 12:11:28.784: E/AndroidRuntime(13273): at java.lang.reflect.Method.invokeNative(Native Method)
01-08 12:11:28.784: E/AndroidRuntime(13273): at java.lang.reflect.Method.invoke(Method.java:511)
01-08 12:11:28.784: E/AndroidRuntime(13273): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
01-08 12:11:28.784: E/AndroidRuntime(13273): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
01-08 12:11:28.784: E/AndroidRuntime(13273): at dalvik.system.NativeStart.main(Native Method)
以下是填充 ListView
的方法 final ArrayList<String> sData = new ArrayList<String>();
try{
SQLiteDatabase db = openOrCreateDatabase("iTimeDB", MODE_PRIVATE, null);
Cursor c2 = db.rawQuery("SELECT * FROM Activities", null);
if(c2 != null){
if(c2.moveToFirst()){
// c2.moveToFirst();
do{
SetAct.setTitle(c2.getString(c2.getColumnIndex("Title")));
SetAct.setDetails(c2.getString(c2.getColumnIndex("Details")));
SetAct.setTime(c2.getString(c2.getColumnIndex("Time")));
SetAct.setRef(c2.getString(c2.getColumnIndex("Ref")));
SetAct.setMatter(c2.getString(c2.getColumnIndex("Matter")));
SetAct.setMatter(c2.getString(c2.getColumnIndex("Date")));
sData.add(SetAct.getMatter());
sData.add(SetAct.getTime());
sData.add(SetAct.getTitle());
sData.add(SetAct.getRef());
sData.add(SetAct.getDetails());
Log.d("Title",c2.getString(c2.getColumnIndex("Title")));
Log.d("Details",c2.getString(c2.getColumnIndex("Details")));
Log.d("Time",SetAct.getTime());
Log.d("Matter",c2.getString(c2.getColumnIndex("Matter")));
} while (c2.moveToNext());
}
}
c2.close();
db.close();
}
catch(Exception e){
e.printStackTrace();
Log.v("YYY", e.toString());
}
final ListView listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(new CustomListAdapter(this, sData));
答案 0 :(得分:2)
由于以下代码您尝试将ArrayList
投放到Activity
holder.titleView.setText(((Activities) listData.get(position)).getTitle());
holder.timeView.setText(((Activities) listData.get(position)).getTime());
holder.refView.setText(((Activities) listData.get(position)).getRef());
holder.detailsView.setText(((Activities) listData.get(position)).getDetails());
holder.matterView.setText(((Activities) listData.get(position)).getMatter());
将私有ArrayList listData; 更改为私有ArrayList listData;
因为它应该包含您已添加到的Activities
的数据。
private ArrayList<Activities> listData;
public CustomListAdapter(Context context, ArrayList<Activities> listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(context);
}
答案 1 :(得分:2)
java.lang.String cannot be cast to com.example.blavo_timeinn.Activities
您的listData
是字符串类型的arraylist。
您需要检查填充列表的方式
你有这个
final ArrayList<String> sData = new ArrayList<String>()
这是传递给构造函数的内容
所以你正在做的是将字符串转换为活动
纠正
final ArrayList<Activities> sData = new ArrayList<Activities>();
然后
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
Activites act = new Activities();
String title =c2.getString(c2.getColumnIndex("Title")));
String details =(c2.getString(c2.getColumnIndex("Details")));
String time =c2.getString(c2.getColumnIndex("Time")));
String ref= c2.getString(c2.getColumnIndex("Ref")));
String matter =(c2.getString(c2.getColumnIndex("Matter")));
String date = c2.getString(c2.getColumnIndex("Date")));
act.setTitle(title);
...// similarly for others
sData.add(act);
}
现在将sData传递给适配器类
的构造函数listView.setAdapter(new CustomListAdapter(this, sData));
然后
private ArrayList<Activities> listData;
public CustomListAdapter(Context context, ArrayList<Activities> listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(context);
}
然后在getView
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.message_row, null);
holder = new ViewHolder();
holder.titleView = (TextView) convertView.findViewById(R.id.title_tv);
holder.timeView = (TextView) convertView.findViewById(R.id.time_tv);
holder.refView = (TextView) convertView.findViewById(R.id.ref_tv);
holder.detailsView = (TextView) convertView.findViewById(R.id.detail_tv);
holder.matterView = (TextView) convertView.findViewById(R.id.date_tv);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Activities act = listData.get(position)
holder.titleView.setText(act.getTitle());
holder.timeView.setText(act.getTime());
holder.refView.setText(act.getRef());
holder.detailsView.setText(act.getDetails());
holder.matterView.setText(act.getMatter());
return convertView;
}
答案 2 :(得分:0)
您应该在数组列表中指定它将要保存的数据类型。在你的情况下它是活动。因此,当你得到物品时,你不必输入演员。
public class CustomListAdapter extends BaseAdapter {
private ArrayList<Activities> listData;
private LayoutInflater layoutInflater;
Activities Act = new Activities();
public CustomListAdapter(Context context, ArrayList<Activities> listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return listData.size();
}
@Override
public Object getItem(int position) {
return listData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.message_row, null);
holder = new ViewHolder();
holder.titleView = (TextView) convertView.findViewById(R.id.title_tv);
holder.timeView = (TextView) convertView.findViewById(R.id.time_tv);
holder.refView = (TextView) convertView.findViewById(R.id.ref_tv);
holder.detailsView = (TextView) convertView.findViewById(R.id.detail_tv);
holder.matterView = (TextView) convertView.findViewById(R.id.date_tv);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.titleView.setText(listData.get(position).getTitle());
holder.timeView.setText(listData.get(position).getTime());
holder.refView.setText(listData.get(position).getRef());
holder.detailsView.setText(listData.get(position).getDetails());
holder.matterView.setText(listData.get(position).getMatter());
return convertView;
}
static class ViewHolder {
TextView titleView;
TextView timeView;
TextView refView;
TextView detailsView;
TextView matterView;
}
}
答案 3 :(得分:0)
这就是您尝试将ArrayList
传递给Activity
时遇到异常的原因。将private ArrayList listData;
更改为private ArrayList<Activities > listData;
holder.titleView.setText(((Activities) listData.get(position)).getTitle());
holder.timeView.setText(((Activities) listData.get(position)).getTime());
holder.refView.setText(((Activities) listData.get(position)).getRef());
holder.detailsView.setText(((Activities) listData.get(position)).getDetails());
holder.matterView.setText(((Activities) listData.get(position)).getMatter());
试试这个
holder.titleView.setText(listData.get(position)).getTitle());