ContentResolver $ CursorWrapperInner无法强制转换

时间:2013-05-03 01:22:58

标签: listview logcat simplecursoradapter onitemclicklistener

我的应用程序使用游标加载器将sqlite数据填充到ListView中。实际上,cursorloader应该只将一个列(COLUNM_NAME_SITE)从DB填充到listview中。

我遇到的问题是......一旦在数据库中插入信息,就会创建一个列表项(我可以通过每次插入数据时显示的行线来判断)但列表视图中没有显示文本列表视图基本上是空白的。我相信这也会在点击列表视图项后使应用程序崩溃。

logcat在下面,但我不明白如何破译它的含义。我已经确定问题可能出在第98行[loginBundle.putString(“clickedWebSite”,((LoginDetails)clickedObject).getsName());]但不知道如何纠正问题。

logcat的:

05-02 15:51:21.573: E/AndroidRuntime(965): FATAL EXCEPTION: main
05-02 15:51:21.573: E/AndroidRuntime(965): java.lang.ClassCastException: android.content.ContentResolver$CursorWrapperInner cannot be cast to com.loginplus.home.LoginDetails
05-02 15:51:21.573: E/AndroidRuntime(965):  at com.loginplus.home.LoginList.onItemClick(LoginList.java:98)
05-02 15:51:21.573: E/AndroidRuntime(965):  at android.widget.AdapterView.performItemClick(AdapterView.java:292)
05-02 15:51:21.573: E/AndroidRuntime(965):  at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
05-02 15:51:21.573: E/AndroidRuntime(965):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
05-02 15:51:21.573: E/AndroidRuntime(965):  at android.widget.AbsListView.onTouchEvent(AbsListView.java:3180)
05-02 15:51:21.573: E/AndroidRuntime(965):  at android.view.View.dispatchTouchEvent(View.java:5541)
05-02 15:51:21.573: E/AndroidRuntime(965):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1951)
05-02 15:51:21.573: E/AndroidRuntime(965):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1712)
05-02 15:51:21.573: E/AndroidRuntime(965):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
05-02 15:51:21.573: E/AndroidRuntime(965):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
05-02 15:51:21.573: E/AndroidRuntime(965):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
05-02 15:51:21.573: E/AndroidRuntime(965):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
05-02 15:51:21.573: E/AndroidRuntime(965):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
05-02 15:51:21.573: E/AndroidRuntime(965):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
05-02 15:51:21.573: E/AndroidRuntime(965):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
05-02 15:51:21.573: E/AndroidRuntime(965):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
05-02 15:51:21.573: E/AndroidRuntime(965):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1912)
05-02 15:51:21.573: E/AndroidRuntime(965):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1371)
05-02 15:51:21.573: E/AndroidRuntime(965):  at android.app.Activity.dispatchTouchEvent(Activity.java:2364)
05-02 15:51:21.573: E/AndroidRuntime(965):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1860)
05-02 15:51:21.573: E/AndroidRuntime(965):  at android.view.View.dispatchPointerEvent(View.java:5721)
05-02 15:51:21.573: E/AndroidRuntime(965):  at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:2890)
05-02 15:51:21.573: E/AndroidRuntime(965):  at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2466)
05-02 15:51:21.573: E/AndroidRuntime(965):  at android.view.ViewRootImpl.processInputEvents(ViewRootImpl.java:845)
05-02 15:51:21.573: E/AndroidRuntime(965):  at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2475)
05-02 15:51:21.573: E/AndroidRuntime(965):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-02 15:51:21.573: E/AndroidRuntime(965):  at android.os.Looper.loop(Looper.java:137)
05-02 15:51:21.573: E/AndroidRuntime(965):  at android.app.ActivityThread.main(ActivityThread.java:4424)
05-02 15:51:21.573: E/AndroidRuntime(965):  at java.lang.reflect.Method.invokeNative(Native Method)
05-02 15:51:21.573: E/AndroidRuntime(965):  at java.lang.reflect.Method.invoke(Method.java:511)
05-02 15:51:21.573: E/AndroidRuntime(965):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-02 15:51:21.573: E/AndroidRuntime(965):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-02 15:51:21.573: E/AndroidRuntime(965):  at dalvik.system.NativeStart.main(Native Method)

CursorLoader:

public class LoginList extends FragmentActivity implements AdapterView.OnItemClickListener, OnClickListener, LoaderManager.LoaderCallbacks<Cursor> {

private ListView loginList;
private Button webLogin;
private SimpleCursorAdapter adapter;

@Override 
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_listview);

loginList = (ListView)findViewById(R.id.loginlist);
loginList.setOnItemClickListener(this);

webLogin = (Button)findViewById(R.id.button3);
webLogin.setOnClickListener(this);

//Specify fileds to display in the list
String[] from = new String[] { ListProvider.COLUMN_NAME_SITE };

//Bind fields to listview
int[] to = new int[] {R.id.loginlist };

// Create CursorAdapter and set it to display
adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, null, from, to, 0 );

loginList.setAdapter(adapter);

getSupportLoaderManager().initLoader( 0, null, this);
}


@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Selected ID :" + arg2, Toast.LENGTH_SHORT).show();

Intent updateDeleteLoginInfo = new Intent (this, UpdateDeleteLoginList.class);

Cursor clickedObject = (Cursor)loginList.getItemAtPosition(0);

Bundle loginBundle = new Bundle();
loginBundle.putString("clickedWebSite",((LoginDetails) clickedObject).getsName());  //this is line 98
loginBundle.putString("clickedWebAddress",((LoginDetails) clickedObject).getwUrl());
loginBundle.putString("clickedUserName",((LoginDetails) clickedObject).getuName());
loginBundle.putString("clickedPassWord",((LoginDetails) clickedObject).getpWord());
loginBundle.putString("clickedNotes",((LoginDetails) clickedObject).getlNotes());

updateDeleteLoginInfo.putExtras(loginBundle);

startActivityForResult(updateDeleteLoginInfo, 0); 
} 

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent webLoginIntent = new Intent (this, LoginPlusActivity.class);
startActivity(webLoginIntent);
}

loginDetail类:

public class LoginDetails {

private int id;
private String sName;
private String wUrl;
private String uName;
private String pWord;
private String lNotes;


// getting ID
public int getid(){
    return id;
    }
    // setting id
    public void setid(int id){
    this.id = id;
    }

public String getsName() {
   return sName;
   }
   public void setsName(String sName) {
   this.sName = sName;
   }

public String getwUrl() {
   return wUrl;
   }
   public void setwUrl(String wUrl) {
   this.wUrl = wUrl;
   }

public String getuName() {
  return uName;
  }
  public void setuName(String uName) {
  this.uName = uName;
  }

public String getpWord() {
  return pWord;
  }
  public void setpWord(String pWord) {
  this.pWord = pWord;
  }

public String getlNotes() {
  return lNotes;
  }
  public void setlNotes(String lNotes) {
  this.lNotes = lNotes;
  }
}

1 个答案:

答案 0 :(得分:4)

我知道这是一个老问题,我只回答,因为这似乎是谷歌的唯一结果..

您无法将光标强制转换为对象。 CursorWrapperInner来自使用CursorLoader,并且(从我可以告诉的)代理对象到加载器的实际Cursor。您必须通过类似静态方法的方式从Cursor实例化您的对象,该方法接收光标并返回您的对象。

例如:

public class LoginDetails {

private int id;
private String sName;
private String wUrl;
private String uName;
private String pWord;
private String lNotes;

public static LoginDetails newInstance(Cursor c){
   LoginDetails ld = new LoginDetails()
   int _id = c.getInt(c.getColumnIndex("id"));
   ...
   ld.setId(_id);
   return ld;
}
....

}

然后在ListView中

@Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        Cursor c = (Cursor)adapter.getItem(position);
        LoginDetails loginDetails = LoginDetails.newInstance(c);
        ...
    }