使用片段在ListView中显示数据库中的数据

时间:2015-06-13 09:45:07

标签: java android listview android-fragments android-listview

我正在尝试将数据库第二列的所有数据显示在ListView内的ScrollView

我正在使用片段, 但是我在使用getSupportLoaderManager().initLoader(1, data, this);时遇到问题#getSupportLoaderManager()是未定义的方法",所以我在这个片段中创建了一个新类,就像一个实验,结果没有错误,但是当我执行它时,也没有任何事情发生,即。我的ListView中没有显示预期的数据。

这是我的布局:

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_body"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:fillViewport="true"
    android:background="@drawable/b">

   <ListView
        android:id="@+id/lv_countries_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</ScrollView>

和Main.Java

public class Main extends Fragment {    
    public Main() {
    }

    Button btn;
    TextView tx;
    EditText InputSearch;
    ArrayAdapter<String> dataAdapter = null;
    View rootView;
    ScrollView mainBody;
    ListView mLVCountries;
    SimpleCursorAdapter mCursorAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.main, container, false);
        setHasOptionsMenu(true);
        getActivity().setTitle("Search");

        mLVCountries = (ListView)rootView.findViewById(R.id.lv_countries_main); 
        mainBody = (ScrollView)rootView.findViewById(R.id.main_body);
    return rootView;
}

public class MainMain extends FragmentActivity implements LoaderCallbacks<Cursor> {
        public void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            mLVCountries.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent countryIntent = new Intent(getActivity().getApplicationContext(), CountryActivity.class);
                    // Creating a uri to fetch country details corresponding to selected listview item
                    Uri data = Uri.withAppendedPath(CountryContentProvider.CONTENT_URI, String.valueOf(id));
                    // Setting uri to the data on the intent
                    countryIntent.setData(data);
                    // Open the activity
                    startActivity(countryIntent);
                }
            });
            // Defining CursorAdapter for the ListView      
            mCursorAdapter = new SimpleCursorAdapter(getActivity().getBaseContext(),
                    android.R.layout.simple_list_item_1,
                    null,
                    new String[] { SearchManager.SUGGEST_COLUMN_TEXT_1},
                    new int[] { android.R.id.text1}, 0);
            // Setting the cursor adapter for the country listview
            mLVCountries.setAdapter(mCursorAdapter);
            // Getting the intent that invoked this activity
            Intent intent = getActivity().getIntent();      
            // If this activity is invoked by selecting an item from Suggestion of Search dialog or 
            // from listview of SearchActivity

            if(intent.getAction().equals(Intent.ACTION_VIEW)){ 
                Intent countryIntent = new Intent(getActivity(), CountryActivity.class);
                countryIntent.setData(intent.getData());
                startActivity(countryIntent);
                getActivity().finish();         
            }else if(intent.getAction().equals(Intent.ACTION_SEARCH)){ // If this activity is invoked, when user presses "Go" in the Keyboard of Search Dialog
                String query = intent.getStringExtra(SearchManager.QUERY);
                doSearch(query);
            }   
        }

        private void doSearch(String query){
            Bundle data = new Bundle();
            data.putString("query", query);
            // Invoking onCreateLoader() in non-ui thread
            getSupportLoaderManager().initLoader(1, data, this);        
        }


        /** This method is invoked by initLoader() */
        @Override
        public Loader<Cursor> onCreateLoader(int arg0, Bundle data) {
            Uri uri = CountryContentProvider.CONTENT_URI;       
            return new CursorLoader(getBaseContext(), uri, null, null , new String[]{data.getString("query")}, null);   
        }
        /** This method is executed in ui thread, after onCreateLoader() */
        @Override
        public void onLoadFinished(Loader<Cursor> arg0, Cursor c) { 
            mCursorAdapter.swapCursor(c);       
        }
        @Override
        public void onLoaderReset(Loader<Cursor> arg0) {

        }
    }
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
       // inflater.inflate.getMenuInflater();
       inflater.inflate(R.menu.main_search_view, menu); //Do not ADD this otherwise the searcView will be duplicate
       MenuItem menuItem = menu.findItem(R.id.search);
       SearchView searchView = (SearchView) menuItem.getActionView();
       searchView.setIconifiedByDefault(false);
       searchView.setQueryHint("Type a something...");

        int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_plate", null, null);
        View searchPlate = searchView.findViewById(searchPlateId);
        if (searchPlate!=null) {
            searchPlate.setBackgroundColor(Color.DKGRAY);
            int searchTextId = searchPlate.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
            TextView searchText = (TextView) searchPlate.findViewById(searchTextId);
            if (searchText!=null) {
                searchText.setTextColor(Color.WHITE);
                searchText.setHintTextColor(Color.WHITE);
            }
        }

     // Associate searchable configuration with the SearchView
        SearchManager searchManager =
               (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
        searchView =
                (SearchView) menu.findItem(R.id.search).getActionView();
        searchView.setSearchableInfo(
                searchManager.getSearchableInfo(getActivity().getComponentName()));

        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar actions click
        switch (item.getItemId()) {
        case R.id.action_settings:
            return false;
        case R.id.search:
            return true;    
        default:
            break;
        }
        return false;
    }
}

1 个答案:

答案 0 :(得分:0)

首先,您必须将数据库中的信息放在String []

首先使用光标检查列以获取计数

像这样设置String []长度,String [] values = new String [int count]

使用for再次移动光标并在每个位置插入每列的字符串

当您从数据库填写所需信息时,请定义ArrayAdapter ... 然后setlistadapter