当我想在列表视图中显示sone数据时,我的应用程序崩溃了。
package com.example.sqliteapplication;
import java.util.ArrayList;
import java.util.List;
import android.R.string;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;
public class SmsMerge extends Activity {
private ImageButton btn;
private ImageButton imgb;
private TemplateOperation templateDBoperation;
ArrayList<User> arr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sms_merge);
templateDBoperation = new TemplateOperation(this);
templateDBoperation.open();
List<String> values=new ArrayList<String>();
values = templateDBoperation.getAllTemplate();
Object[] obj=values.toArray();
arr=new ArrayList<User>();
String[] da=new String[obj.length];
for(int i=0; i<obj.length; i++)
{
da[i]=(String)obj[i];
arr.add(new User(da[i]));
}
Toast.makeText(getBaseContext(), "Siddiqueeeeeeeeeeeeeeeeeee"+obj.length, Toast.LENGTH_SHORT).show();
ListView listview = (ListView)findViewById(android.R.id.list);
MyListAdapter adpttt=new MyListAdapter(SmsMerge.this,R.layout.row,arr);
listview.setItemsCanFocus(false);
listview.setAdapter(adpttt);
imgb=(ImageButton)findViewById(R.id.btnaddtemplate);
imgb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(SmsMerge.this, SqliteActivity.class);
startActivity(intent);
}
});
}
我的自定义适配器代码在这里......
package com.example.sqliteapplication;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
public class MyListAdapter extends ArrayAdapter<User> {
int inflatr;
Context ctxt;
ArrayList<User> data=new ArrayList<User>();
public MyListAdapter(Context context, int layoutResourceId,
ArrayList<User> data) {
super(context, layoutResourceId, data);
this.inflatr = layoutResourceId;
this.ctxt = context;
this.data= data;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
UserHolder holder = null;
View row = convertView;
if(convertView==null)
{
LayoutInflater inflater = ((Activity) ctxt).getLayoutInflater();
row = inflater.inflate(inflatr, parent, false);
holder = new UserHolder();
holder.textName=(TextView)row.findViewById(R.id.label);
holder.btnEdit = (ImageButton) row.findViewById(R.id.atomPay_removePay);
row.setTag(holder);
}
else
{
holder = (UserHolder) row.getTag();
}
User dta=data.get(position);
holder.textName.setText(dta.getName());
holder.btnEdit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
return row;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
}
我的模板类在这里......
package com.example.sqliteapplication;
public class Template {
private int id;
private String name;
private String Description;
public long getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public void setDescription(String Description) {
this.Description = Description;
}
public String getDescription()
{
return Description;
}
@Override
public String toString() {
return name;
}
}
我的数据库包装代码就在这里.....
package com.example.sqliteapplication;
import android.content.Context;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DataBaseWraper extends SQLiteOpenHelper {
public static final String TEMPLATE = "template";
public static final String TEMPLATE_ID = "_id";
public static final String PERSON_NAME = "_name";
public static final String TEMPLATE_DESCRIPTION = "_description";
public static final String DATABASE_NAME = "Template.db";
public static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE_Trmplate = "create table "
+ TEMPLATE + "(" + TEMPLATE_ID
+ " integer primary key autoincrement, " + PERSON_NAME
+ " text not null, "+ TEMPLATE_DESCRIPTION
+ " text not null);";
public DataBaseWraper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE_Trmplate);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// you should do some logging in here
// ..
db.execSQL("DROP TABLE IF EXISTS " + TEMPLATE);
onCreate(db);
}
}
我的模板操作代码在这里
package com.example.sqliteapplication;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
public class TemplateOperation {
// Database fields
private DataBaseWraper dbHelper;
private String[] TEMPLATE_TABLE_COLUMNS = { DataBaseWraper.TEMPLATE_ID,
DataBaseWraper.PERSON_NAME, DataBaseWraper.TEMPLATE_DESCRIPTION };
private SQLiteDatabase database;
public TemplateOperation(Context context) {
dbHelper = new DataBaseWraper(context);
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
public void close() {
dbHelper.close();
}
public Template addTemplate(String Description, String Person_Name) {
ContentValues values = new ContentValues();
values.put(DataBaseWraper.PERSON_NAME, Person_Name);
values.put(DataBaseWraper.TEMPLATE_DESCRIPTION, Description);
long templateId = database.insert(DataBaseWraper.TEMPLATE, null, values);
// now that the student is created return it ...
Cursor cursor = database.query(DataBaseWraper.TEMPLATE,
TEMPLATE_TABLE_COLUMNS, DataBaseWraper.TEMPLATE_ID + " = "
+ templateId, null, null, null, null);
cursor.moveToFirst();
Template newComment = parseTemplate(cursor);
cursor.close();
return newComment;
}
public void deleteStudent(Template comment) {
long id = comment.getId();
System.out.println("Comment deleted with id: " + id);
database.delete(DataBaseWraper.TEMPLATE, DataBaseWraper.TEMPLATE_ID
+ " = " + id, null);
}
public List getAllTemplate() {
List students = new ArrayList();
Cursor cursor = database.query(DataBaseWraper.TEMPLATE,
TEMPLATE_TABLE_COLUMNS, null, null, null, null,
null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Template student = parseTemplate(cursor);
students.add(student);
cursor.moveToNext();
}
cursor.close();
Log.d(""+students, "humayoonnnnn");
return students;
}
private Template parseTemplate(Cursor cursor) {
Template template = new Template();
template.setId((cursor.getInt(0)));
template.setName(cursor.getString(1));
return template;
}
}
错误的堆栈跟踪:
05-08 18:44:03.336: E/AndroidRuntime(10100): FATAL EXCEPTION: main
05-08 18:44:03.336: E/AndroidRuntime(10100): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sqliteapplication/com.example.sqliteapplication.SmsMerge}: java.lang.ClassCastException: com.example.sqliteapplication.Template cannot be cast to java.lang.String
05-08 18:44:03.336: E/AndroidRuntime(10100): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
05-08 18:44:03.336: E/AndroidRuntime(10100): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
05-08 18:44:03.336: E/AndroidRuntime(10100): at android.app.ActivityThread.access$600(ActivityThread.java:156)
05-08 18:44:03.336: E/AndroidRuntime(10100): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
05-08 18:44:03.336: E/AndroidRuntime(10100): at android.os.Handler.dispatchMessage(Handler.java:99)
05-08 18:44:03.336: E/AndroidRuntime(10100): at android.os.Looper.loop(Looper.java:153)
05-08 18:44:03.336: E/AndroidRuntime(10100): at android.app.ActivityThread.main(ActivityThread.java:5299)
05-08 18:44:03.336: E/AndroidRuntime(10100): at java.lang.reflect.Method.invokeNative(Native Method)
05-08 18:44:03.336: E/AndroidRuntime(10100): at java.lang.reflect.Method.invoke(Method.java:511)
05-08 18:44:03.336: E/AndroidRuntime(10100): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
05-08 18:44:03.336: E/AndroidRuntime(10100): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
05-08 18:44:03.336: E/AndroidRuntime(10100): at dalvik.system.NativeStart.main(Native Method)
05-08 18:44:03.336: E/AndroidRuntime(10100): Caused by: java.lang.ClassCastException: com.example.sqliteapplication.Template cannot be cast to java.lang.String
05-08 18:44:03.336: E/AndroidRuntime(10100): at com.example.sqliteapplication.SmsMerge.onCreate(SmsMerge.java:43)
05-08 18:44:03.336: E/AndroidRuntime(10100): at android.app.Activity.performCreate(Activity.java:5122)
05-08 18:44:03.336: E/AndroidRuntime(10100): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
05-08 18:44:03.336: E/AndroidRuntime(10100): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
05-08 18:44:03.336: E/AndroidRuntime(10100): ... 11 more
答案 0 :(得分:1)
我猜da[i]=(String)obj[i];
就是它的全部归宿。
错误已经说明:
引起:java.lang.ClassCastException: com.example.sqliteapplication.Template无法强制转换为 java.lang.String中
换句话说:您无法将Template
- 对象投射到String
。
修改强>
一个替代方案是使用obj[i].toString()
。
在这种情况下,您可以覆盖存储在Object-Array中的每个类的toString()
- Method,以便返回的输出有意义。