2

我的应用程序使用 cursorloader 将 sqlite 数据填充到 ListView 中。实际上,游标加载器应该只将数据库中的一列 (COLUM_NAME_SITE) 填充到列表视图中。

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

logcat 在下面,但我不明白如何破译它在说什么。我已经确定问题可能在第 98 行 [loginBundle.putString("clickedWebSite",((LoginDetails) clickedObject).getsName());] 区域,但不知道如何解决问题。

日志猫:

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)

游标加载器:

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;
  }
}
4

1 回答 1

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);
        ...
    }
于 2014-01-07T06:38:14.273 回答