经过大量的工作,我终于使用CursorLoader和SimpleCursorAdapter填充了listview,尽管我仍然不清楚CursorLoader。我试图显示单词含义的列表视图,但列表视图是如此不清楚(暗淡)。它看起来像一个模糊的背景。只有单击它时,列表项才会变得清晰。任何人都可以帮助我为什么这样以及如何解决?
' wordmeanings.xml'包含listview和' wordmeaningrow.xml'包含两个文本视图,分别用于单词和含义。
这是我的代码:
public class WordMeaning_CursorLoader extends ListActivity implements LoaderCallbacks<Cursor>{
// Identifies a particular loader being used in this component
private static final int URL_LOADER = 0;
Uri CONTENT_URI;
SimpleCursorAdapter sAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.wordmeanings);
String[] columns = {Constants.WORD_NAME, Constants.MEANING_NAME};
int[] to = {R.id.tvword_wordmeaningrow, R.id.tvmeaning_wordmeaningrow};
sAdapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.wordmeaningrow, null, columns, to, 0);
this.setListAdapter(sAdapter);
// initializing the loader
getLoaderManager().initLoader(URL_LOADER, null, this);
} // end onCreate
@Override
public Loader<Cursor> onCreateLoader(int loaderId, Bundle args) {
switch(loaderId){
case URL_LOADER:
//returns new cursor loader
String myUri = "content://com.test.provider.wordmeaning/tbl_word_meaning";
CONTENT_URI = Uri.parse(myUri);
return new CursorLoader(
WordMeaning_CursorLoader.this,
CONTENT_URI,
null,
null,
null,
null);
default:
return null;
} // end switch
} // end onCreateLoader
@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor cursor) {
sAdapter.swapCursor(cursor);
} // end onLoadFinished
@Override
public void onLoaderReset(Loader<Cursor> cursorLoader) {
sAdapter.swapCursor(null);
}
} // end WordMeaning_CursorLoader
wordmeanings.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@android:id/list" android:layout_width="wrap_content" android:layout_height="wrap_content"
></ListView>
</LinearLayout>
wordmeaningrow.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" android:padding="10dp" >
<TextView
android:id="@+id/tvword_wordmeaningrow" android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="word" android:textAppearance="?android:attr/textAppearanceLarge" android:textStyle="bold"
/>
<TextView
android:id="@+id/tvmeaning_wordmeaningrow" android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_marginLeft="10dp" android:text="meaning" android:textAppearance="?android:attr/textAppearanceMedium"
/>
</LinearLayout>
上面提到的昏暗的东西在8:30左右看起来像here。谢谢。我正在使用genymotion模拟器。
快照:
使用以下代码生成第三个快照:
public class WordMeaning_CursorAdapter extends ListActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wordmeanings);
String myUri = "content://com.test.provider.wordmeaning/tbl_word_meaning";
Uri CONTENT_URI = Uri.parse(myUri);
//get ContentResolver instance
ContentResolver crInstance = getContentResolver();
Cursor c = crInstance.query(CONTENT_URI, null, null, null, null);
startManagingCursor(c);
// the desired columns to be bound
String[] columns = new String[] {Constants.WORD_NAME, Constants.MEANING_NAME};
// the xml defined views for each field to be bound to
int[] to = new int[] {R.id.tvword_wordmeaningrow, R.id.tvmeaning_wordmeaningrow};
// create adapter with cursor pointing to desired data
SimpleCursorAdapter cAdapter = new SimpleCursorAdapter(this, R.layout.wordmeaningrow, c, columns, to, 0);
// set this adapter as the list activity's adapter
this.setListAdapter(cAdapter);
} // end onCreate
} // end WordMeaning_CursorAdapter class