0

所以我在夏天开发了一个应用程序。它将项目存储在数据库中,在列表视图中显示它们(否则 Toast 会告诉用户它是空的),并允许用户通过单击一个来查看详细信息。最终我计划让搜索条件缩小列表,但我还没有。

我的问题是列表视图似乎正在填充,但该应用程序制作了 Toast....

结果Activity.java

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

结果片段.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

活动结果.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

<?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"/>

list_item_results.xml

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

我可能误解了如何使用片段,但为了使用我所遵循的使用内容提供程序的教程,这是必要的。我没有在此处输入内容提供程序,但我很确定这是有效的(嗯......在其他地方坏了,但我想我可以处理那个)。如果有人想看到它,我可以编辑我的问题。

如果它完全相关,则列表项有它们的数据并且是可点击的,但它们看起来是灰色的,好像它们是不可点击的。也许这是一个样式问题,但我认为他们会使用应用程序其余部分共享的样式。

但重申我的问题,列表视图正在填充,但适配器声称是空的。我不确定这是/将会有多大的问题,但我想要一种让他们同意的方法,或者解释为什么他们可能不需要。

4

1 回答 1

0

或解释为什么他们可能不需要。

他们似乎不同意,因为在isEmpty调用适配器时确实是空的。AndroidLoaders在单独的线程(异步)上完成工作。

我建议设置空视图而不是使用 toast。

于 2013-06-17T22:15:31.857 回答