0

我的應用程序使用一個光標裝載器來將sqlite數據填充到一個ListView中。實際上,cursorloader只應該從數據庫填充一列(COLUNM_NAME_SITE)到列表視圖中。我遇到的問題是,一旦信息被插入到數據庫中,就會創建一個列表項(我可以通過插入數據時顯示的行數來判斷),但是列表視圖中沒有顯示任何文本,列表視圖基本上是空白的。我相信這也會讓應用程序崩潰,一旦列表視圖項被點擊。CursorLoader不填充ListView [幫助]

我假設我的FROM和TO數組和光標適配器已正確創建,但我可能是錯的。問題是否與我的佈局有關?我不確定,但希望有人深入瞭解我的代碼,並讓我知道我出錯的地方。

我沒有收到任何錯誤,直到我點擊空白列表視圖項目。

裝載器類:

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); 

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(arg2); 

Bundle loginBundle = new Bundle(); 
loginBundle.putString("clickedWebSite",((LoginDetails) clickedObject).getsName()); 
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); 
} 

@Override 
public Loader<Cursor> onCreateLoader(int ignored, final Bundle args) { 
return new CursorLoader(this, ListProvider.CONTENT_URI, null, null, null, null); 
} 

@Override 
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { 
adapter.swapCursor(cursor); 
} 

@Override 
public void onLoaderReset (Loader<Cursor> loader) { 
adapter.swapCursor(null); 
} 
}  

佈局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:ns="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" > 

<ListView 
    android:id="@+id/loginlist" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_above="@+id/button3" 
    android:layout_alignParentTop="true" /> 

<Button 
    android:id="@+id/button3" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:text="ADD" /> 

</RelativeLayout> 

數據庫:

//Database Columns 
public static final String COLUMN_ROWID = "_id"; 
public static final String COLUMN_NAME_SITE = "sName"; 
public static final String COLUMN_NAME_ADDRESS = "wUrl"; 
public static final String COLUMN_NAME_USERNAME = "uName"; 
public static final String COLUMN_NAME_PASSWORD = "pWord"; 
public static final String COLUMN_NAME_NOTES = "lNotes"; 

// Database related Constants 
public static final String DATABASE_NAME = "SiteLogindb"; 
public static final int DATABASE_VERSION = 2; 


public static final String DSTORE_CREATE = "create table if not exists " + 
     TABLE_NAME_INFOTABLE + " ("+ COLUMN_ROWID + " integer primary key autoincrement," 

        + COLUMN_NAME_SITE + " text not null," 
        + COLUMN_NAME_ADDRESS + " text not null," 
        + COLUMN_NAME_USERNAME + " text not null," 
        + COLUMN_NAME_PASSWORD + " text not null," 
        + COLUMN_NAME_NOTES + " text not null);"; 

回答

2

你可以做到這一點的另一種方法是延長ListActivity和實施LoaderManager.LoaderCallbacks。

public class LoginActivity extends ListActivity implements LoaderManager.LoaderCallbacks<Cursor> {... 

Android Design Patterns博客上有很好的4部分教程。