Android Listview将填充,但适配器声明为空
所以我在夏天开发了一个应用程序。它将项目存储在数据库中,在listview中显示它们(否则会使Toast告诉用户其为空),并允许用户通过单击一个项目来查看详细信息。最终,我计划让搜索条件缩小列表的范围,但我还没有做到 我的问题是listview似乎正在填充,但应用程序会制作Toast ResultsActivity.javaAndroid Listview将填充,但适配器声明为空,android,listview,android-contentprovider,simplecursoradapter,android-listfragment,Android,Listview,Android Contentprovider,Simplecursoradapter,Android Listfragment,所以我在夏天开发了一个应用程序。它将项目存储在数据库中,在listview中显示它们(否则会使Toast告诉用户其为空),并允许用户通过单击一个项目来查看详细信息。最终,我计划让搜索条件缩小列表的范围,但我还没有做到 我的问题是listview似乎正在填充,但应用程序会制作Toast ResultsActivity.java public class ResultsActivity extends FragmentActivity implements ResultsFragment.OnPla
public class ResultsActivity extends FragmentActivity implements ResultsFragment.OnPlantSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_results);
}// method: FragmentActivity.onCreate()
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.results, menu);
return true;
}// method: FragmentActivity.onCreateOptionsMenu()
/* Custom methods */
//@Override
public void onPlantSelected(long id){
Intent intent = new Intent(this, PlantViewActivity.class);
//TODO confirm id
intent.putExtra(DBContentProvider.KEY_PLANT_ID, id);
startActivity(intent);
}// method: ResultsFragment.OnPlantSelectedListener.onPlantSelected(long)
}// class: ResultsActivity extends ListActivity
ResultsFragment.java
public class ResultsFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor>{
private static OnPlantSelectedListener emptyPlantSelectedListener = new OnPlantSelectedListener(){
@Override
public void onPlantSelected(long id){}
};
private static OnPlantSelectedListener plantSelectedListener = emptyPlantSelectedListener;
private static final String STATE_ACTIVATED_POSITION = "activated_position";
private int activatedPosition = ListView.INVALID_POSITION;
private SimpleCursorAdapter adapter;
public ResultsFragment(){}
/* Fragment Lifespan */
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
String[] dbPlantColumns = {DBContentProvider.KEY_GENUS_TEXT, DBContentProvider.KEY_SPECIES,
DBContentProvider.KEY_COMMON_NAME_GROUP};
int[] fragPlantFields = {R.id.result_item_txt_genus, R.id.result_item_txt_species,
R.id.result_item_txt_common_name};
getLoaderManager().initLoader(0x01, null, this);//null can be replaced by a Bundle of search criteria
adapter = new SimpleCursorAdapter(getActivity().getApplicationContext(), R.layout.list_item_results,
null, dbPlantColumns, fragPlantFields, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
setListAdapter(adapter);
if(adapter.isEmpty())//This returns true
Toast.makeText(getActivity().getApplicationContext(), "Nothing returned from the database",
Toast.LENGTH_LONG).show();
}// method: Activity.onCreate(Bundle)
@Override
public void onViewCreated(View view, Bundle savedInstanceState){
super.onViewCreated(view, savedInstanceState);
if (savedInstanceState != null && savedInstanceState.containsKey(STATE_ACTIVATED_POSITION)){
setActivatedPosition(savedInstanceState.getInt(STATE_ACTIVATED_POSITION));
}
}// method: ListFragment.onViewCreated(View, Bundle)
@Override
public void onAttach(Activity activity){
super.onAttach(activity);
if (!(activity instanceof OnPlantSelectedListener)){
throw new IllegalStateException("Activity must implement fragment's listener.");
}
plantSelectedListener = (OnPlantSelectedListener) activity;
}// method: ListFragment.onAttach(Activity)
@Override
public void onDetach(){
super.onDetach();
plantSelectedListener = emptyPlantSelectedListener;
}// method: ListFragment.onDetach()
@Override
public void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
if (activatedPosition != ListView.INVALID_POSITION){
outState.putInt(STATE_ACTIVATED_POSITION, activatedPosition);
}
}// method: ListFragment.onSaveInstanceState(Bundle)
/* LoaderManager abstract methods */
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args){
//TODO parse args for searching. Second null is String[]; index 0-17 are search arguments.
String[] arguments = new String[18];
String[] dbPlantColumns = {DBContentProvider.TABLE_PLANT + "." + DBContentProvider.KEY_PLANT_ID,
DBContentProvider.KEY_FAMILY_TEXT, DBContentProvider.KEY_GENUS_TEXT, DBContentProvider.KEY_SPECIES,
DBContentProvider.KEY_COMMON_NAME_GROUP, DBContentProvider.KEY_GROWTH_FORM_TEXT};
CursorLoader cursorLoader = new CursorLoader(getActivity(), Uri.parse(DBContentProvider.CONTENT_URI_STRING +
"search"), dbPlantColumns, null, arguments, null);
return cursorLoader;
}// method: LoaderCallbacks.onCreateLoader(int, Bundle)
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor){
adapter.swapCursor(cursor);
}// method: LoaderCallbacks.onLoadFinished(Loader<Cursor>, Cursor)
@Override
public void onLoaderReset(Loader<Cursor> loader){
adapter.swapCursor(null);
}// method: LoaderCallbacks.onLoaderReset(Loader<Cursor>)
/* ListFragment response methods */
@Override
public void onListItemClick(ListView listView, View view, int position, long id){
super.onListItemClick(listView, view, position, id);
plantSelectedListener.onPlantSelected(id /*Get id of plant at this position*/);
}// method: ListFragment.onListItemClick(Listview, View, int, long)
public void setActivateOnItemClick(boolean activateOnItemClick){
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}// method: setActivateOnItemClick(boolean)
private void setActivatedPosition(int position){
if(position == ListView.INVALID_POSITION){
getListView().setItemChecked(activatedPosition, false);
}
else{
getListView().setItemChecked(position, true);
}
activatedPosition = position;
}//method: setActiviatedPosition(int)
/**
* OnPlantSelectedListener is used by the fragment and the activity to
* make sure that the selection of a plant is dealt with correctly.
*/
public interface OnPlantSelectedListener{
public void onPlantSelected(long id);
}// interface: OnPlantSelectedListener
}// class: ResultsFragment extends ListFragment
公共类ResultsFragment扩展ListFragment实现LoaderManager.LoaderCallbacks{
私有静态OnPlantSelectedListener emptyPlantSelectedListener=新建OnPlantSelectedListener(){
@凌驾
已选定的公用无效(长id){}
};
私有静态OnPlantSelectedListener plantSelectedListener=emptyPlantSelectedListener;
私有静态最终字符串状态\u ACTIVATED\u POSITION=“ACTIVATED\u POSITION”;
private int activatedPosition=ListView.INVALID_POSITION;
专用SimpleCursorAdapter适配器;
public ResultsFragment(){}
/*碎片寿命*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
String[]dbPlantColumns={DBContentProvider.KEY\u generis\u TEXT,DBContentProvider.KEY\u SPECIES,
DBContentProvider.KEY\u COMMON\u NAME\u GROUP};
int[]fragPlantFields={R.id.result\u item\u txt\u属,R.id.result\u item\u txt\u种,
R.id.result\u item\u txt\u common\u name};
getLoaderManager().initLoader(0x01,null,this);//null可以由一组搜索条件替换
adapter=new SimpleCursorAdapter(getActivity().getApplicationContext(),R.layout.list\u item\u结果,
null、dbPlantColumns、fragPlantFields、CursorAdapter.FLAG\u REGISTER\u CONTENT\u OBSERVER);
setListAdapter(适配器);
if(adapter.isEmpty())//返回true
Toast.makeText(getActivity().getApplicationContext(),“数据库未返回任何内容”,
Toast.LENGTH_LONG).show();
}//方法:Activity.onCreate(Bundle)
@凌驾
已创建视图上的公共void(视图,捆绑保存状态){
super.onViewCreated(视图,savedInstanceState);
if(savedInstanceState!=null&&savedInstanceState.containsKey(状态激活位置)){
setActivatedPosition(savedInstanceState.getInt(STATE_ACTIVATED_POSITION));
}
}//方法:ListFragment.onViewCreated(视图,捆绑)
@凌驾
公共事务主任(活动){
超级转速计(活动);
if(!(PlantSelectedListener上的活动实例)){
抛出新的IllegalStateException(“活动必须实现片段的侦听器”);
}
plantSelectedListener=(OnPlantSelectedListener)活动;
}//方法:ListFragment.onAttach(活动)
@凌驾
公共无效连接(){
super.onDetach();
plantSelectedListener=emptyPlantSelectedListener;
}//方法:ListFragment.onDetach()
@凌驾
SaveInstanceState上的公共无效(束超出状态){
super.onSaveInstanceState(超出状态);
if(activatedPosition!=列表视图。位置无效){
outState.putInt(状态激活位置,激活位置);
}
}//方法:ListFragment.onSaveInstanceState(Bundle)
/*LoaderManager抽象方法*/
@凌驾
公共加载器onCreateLoader(int-id,Bundle-args){
//TODO解析用于搜索的参数。第二个null是字符串[];索引0-17是搜索参数。
字符串[]参数=新字符串[18];
字符串[]dbPlantColumns={DBContentProvider.TABLE_PLANT+““+DBContentProvider.KEY_PLANT_ID,
DBContentProvider.KEY_族_文本,DBContentProvider.KEY_属_文本,DBContentProvider.KEY_物种,
DBContentProvider.KEY\u COMMON\u NAME\u GROUP,DBContentProvider.KEY\u GROWTH\u FORM\u TEXT};
CursorLoader CursorLoader=新的CursorLoader(getActivity(),Uri.parse(DBContentProvider.CONTENT\u Uri\u STRING+
“搜索”),dbPlantColumns,null,arguments,null);
返回游标装入器;
}//方法:LoaderCallbacks.onCreateLoader(int,Bundle)
@凌驾
public void onLoadFinished(加载器,光标){
适配器.swapCursor(游标);
}//方法:LoaderCallbacks.onLoadFinished(加载器,光标)
@凌驾
公共void onLoaderReset(加载器){
适配器.swapCursor(空);
}//方法:LoaderCallbacks.onLoaderReset(Loader)
/*列表片段响应方法*/
@凌驾
public void onListItemClick(ListView ListView、视图视图、int位置、长id){
super.onListItemClick(列表视图、视图、位置、id);
plantSelectedListener.onPlantSelected(id/*获取此位置的植物id*/);
}//方法:ListFragment.onListItemClick(Listview、View、int、long)
public void setActivateOnItemClick(布尔activateOnItemClick){
getListView().setChoiceMode(ListView.CHOICE\u MODE\u SINGLE);
}//方法:setActivateOnItemClick(布尔值)
私有void setActivatedPosition(内部位置){
if(position==ListView.INVALID_position){
getListView().setItemChecked(activatedPosition,false);
}
否则{
getListView().setItemChecked(位置,true);
}
激活位置=位置;
}//方法:setActivatedPosition(int)
/**
*片段和活动使用OnPlantSelectedListener来
*确保正确处理植物的选择。
*/
PlantSelectedListener上的公共接口{
已选定的公用id(长id);
}//接口:OnPlantSelectedListener
}//类:ResultsFragment扩展ListFragment
activity_results.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".ResultsActivity"
android:id="@+id/Results_Lin"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- @android:id/list is the necessary id for the activity to find it. -->
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
</ListView>
<TextView
android:id="@+id/results_empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txt_no_results"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="edu.dordt.grassrootsplantid.ResultsFragment"
android:id="@+id/Result_Fragment"/>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/Result_Item_Lin"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<TextView
android:id="@+id/result_item_txt_lbl_common_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/lbl_common_name"/>
<TextView
android:id="@+id/result_item_txt_common_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/result_item_txt_lbl_common_name"/>
<TextView
android:id="@+id/result_item_txt_lbl_scientific_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/result_item_txt_lbl_common_name"
android:text="@string/lbl_scientific_name"/>
<TextView
android:id="@+id/result_item_txt_genus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/result_item_txt_lbl_scientific_name"
android:layout_below="@id/result_item_txt_lbl_common_name"/>
<TextView
android:id="@+id/result_item_txt_species"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/result_item_txt_genus"
android:layout_below="@id/result_item_txt_lbl_common_name"
android:layout_marginLeft="5dp"/>
</RelativeLayout>
fragment_results.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".ResultsActivity"
android:id="@+id/Results_Lin"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- @android:id/list is the necessary id for the activity to find it. -->
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
</ListView>
<TextView
android:id="@+id/results_empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txt_no_results"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="edu.dordt.grassrootsplantid.ResultsFragment"
android:id="@+id/Result_Fragment"/>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/Result_Item_Lin"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<TextView
android:id="@+id/result_item_txt_lbl_common_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/lbl_common_name"/>
<TextView
android:id="@+id/result_item_txt_common_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/result_item_txt_lbl_common_name"/>
<TextView
android:id="@+id/result_item_txt_lbl_scientific_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/result_item_txt_lbl_common_name"
android:text="@string/lbl_scientific_name"/>
<TextView
android:id="@+id/result_item_txt_genus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/result_item_txt_lbl_scientific_name"
android:layout_below="@id/result_item_txt_lbl_common_name"/>
<TextView
android:id="@+id/result_item_txt_species"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/result_item_txt_genus"
android:layout_below="@id/result_item_txt_lbl_common_name"
android:layout_marginLeft="5dp"/>
</RelativeLayout>
list_item_results.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".ResultsActivity"
android:id="@+id/Results_Lin"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- @android:id/list is the necessary id for the activity to find it. -->
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
</ListView>
<TextView
android:id="@+id/results_empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txt_no_results"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="edu.dordt.grassrootsplantid.ResultsFragment"
android:id="@+id/Result_Fragment"/>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/Result_Item_Lin"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<TextView
android:id="@+id/result_item_txt_lbl_common_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/lbl_common_name"/>
<TextView
android:id="@+id/result_item_txt_common_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/result_item_txt_lbl_common_name"/>
<TextView
android:id="@+id/result_item_txt_lbl_scientific_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/result_item_txt_lbl_common_name"
android:text="@string/lbl_scientific_name"/>
<TextView
android:id="@+id/result_item_txt_genus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/result_item_txt_lbl_scientific_name"
android:layout_below="@id/result_item_txt_lbl_common_name"/>
<TextView
android:id="@+id/result_item_txt_species"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/result_item_txt_genus"
android:layout_below="@id/result_item_txt_lbl_common_name"
android:layout_marginLeft="5dp"/>
</RelativeLayout>
我可能误解了