0

I am trying to display all the data from the second column of my database to my ListView inside a ScrollView.

I am using a fragment, but I'm having a problem using getSupportLoaderManager().initLoader(1, data, this); it sais "getSupportLoaderManager() is undefined method", so I create a new class inside this fragment just as an experiment, and the result is there is no error but when I execute it, there's nothing happening as well ie. the expected data is not showing in my ListView.

Here is my layout:

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>

and the 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;
    }
}
ᴘᴀɴᴀʏɪᴏᴛɪs
  • 5,854
  • 7
  • 43
  • 71
bernzkie
  • 1,079
  • 2
  • 14
  • 32
  • Are you trying to load listview inside scrollview??? its very bad practice.make your parent layout as linear or relative – Madhu Jun 13 '15 at 10:09
  • ok...thanks for this tip, but how about the scroll? i'm afraid if it will be gone? – bernzkie Jun 13 '15 at 10:25
  • ListView itself give scroll to your data, so no need to use scrollview in listview. – Madhu Jun 13 '15 at 10:27
  • ahh. ok. thanks! so, how about my question? can you help me about this? im getting stock because of this `getSupportLoaderManager().initLoader(1, data, this)` this is works on `FragmentActivity` but not on my `Fragment`. it say undefined method in Fragment. – bernzkie Jun 13 '15 at 10:35
  • getLoaderManager().initLoader(1, data, this)); try to use this, In fragment u need to use Loadermanager instead of supportLoader – Madhu Jun 13 '15 at 10:58
  • an error again in `initLoader` hayyzst... – bernzkie Jun 13 '15 at 11:13
  • Whats is your error ??? is it undefined method ?? – Madhu Jun 13 '15 at 11:29
  • ok, this driving me crazy. it is possible to create a listView adapter from a class and just call it to use as a listView adapter for fragment? if so, can you give me a reference? – bernzkie Jun 13 '15 at 11:58
  • http://stackoverflow.com/questions/16333754/how-to-customize-listview-using-baseadapter refer this link its very basic example to load data into adapter. – Madhu Jun 13 '15 at 12:03
  • `initLoader` error. it says, this method is not applicable for the arguments(int, Bundle, Main). – bernzkie Jun 13 '15 at 15:07

1 Answers1

0

First, you have to put the info in your database in a String []

Go over the columns first with the cursor to get the count

Set the String[] length like this, String [] values= new String [int count]

Move over the cursor again with a for and insert the string of each column in each position

When you have filled the info you need from the database, define ArrayAdapter ... Then setlistadapter