Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
[email protected] webmail now available. Want one? Go here.
Cannot use outlook/hotmail/live here to register as they blocking our mail servers. #microsoftdeez
Obey the Epel!

Paste

Pasted as Plain Text by Anthony Tannoury ( 12 years ago )
This function will fetch the data from SQLite to my ListView :

     private void FetchAllData() { 

      final DatabaseHelper myDbHelper = new DatabaseHelper(this);
      myDbHelper.openDataBase();

         Cursor cursor = myDbHelper.returnAll("wordss");

         String[] from = new String[] { DatabaseHelper.KEYWORD,
                                  DatabaseHelper.DETAILS };

         // Specify the corresponding layout elements where we want the columns to go
         int[] to = new int[] { R.id.title,
                                R.id.details };

         // Create a simple cursor adapter for the definitions and apply them to the ListView
         words = new SimpleCursorAdapter(this,
                                       R.layout.list_item_with_description, cursor, from, to, 0);

         
         mListView.setAdapter(words);
         mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
         mListView.setSelection(0);

         
         myFilter.addTextChangedListener(new TextWatcher() {
        
          public void afterTextChanged(Editable s) {
          }
        
          public void beforeTextChanged(CharSequence s, int start, 
            int count, int after) {
          }
        
          public void onTextChanged(CharSequence s, int start, 
            int before, int count) {
           words.getFilter().filter(s.toString());
          }
          
          
         });

         
         words.setFilterQueryProvider(new FilterQueryProvider() {
             public Cursor runQuery(CharSequence constraint) {
                 return myDbHelper.fetchWordsByKeyword(constraint.toString());


             }
             
         });
         
          viewer = (DetailsFragment) getSupportFragmentManager().findFragmentById(R.id.details_fragment);
          Bundle args=new Bundle();
          args.putString("index",  "0");
          viewer.setArguments(args);
          
         // Define the on-click listener for the list items
         mListView.setOnItemClickListener(new OnItemClickListener() {
             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
             Log.d("click_row_id = ",""+id);  
             data = Uri.withAppendedPath(DictionaryProvider.CONTENT_URI,
                     String.valueOf(id));
             row_id = String.valueOf(id) ;
             
             // ------------------------------ To support Fragments ----------------------------------- 

             
                 
                 if (viewer == null || !viewer.isInLayout()) {
                  Intent showDetails = new Intent(getApplicationContext(), DetailsActivity.class);
                     showDetails.setData(data);
                     startActivity(showDetails);
                 } else {
  
//                     viewer.updateUrl(data);
//                     Bundle args=new Bundle();
//                     args.putString("id",  String.valueOf(id));
//                     Log.d("PASSED VARIABLE = ", String.valueOf(id));
//                     viewer.setArguments(args);
                    addDynamicFragment();
                     
                     
                 }
                 
             
             // ---------------------------------------------------------------------------------------


             }
         });
    
         myDbHelper.close();
         
         } // END FetchAllData () function

And I'm calling it here :

    @Override
    public void onCreate(Bundle savedInstanceState) {
     
     super.onCreate(savedInstanceState);
      setContentView(R.layout.wordlist_fragment_layout);
        HomeBackground = (ImageView) findViewById(R.id.home_background);
        //setDefaultOrientation();
        
        myFilter = (EditText) findViewById(R.id.myFilter);
        mListView = (ListView) findViewById(R.id.list);
        myFilter.setOnEditorActionListener(new DoneOnEditorActionListener());
      

      // -----------------------------------------------------------------------------------
      
        /* To force the "Overflow" menu button to appear in the Action Bar, even if the application is
        running on a device having a menu button */
     try {
         ViewConfiguration config = ViewConfiguration.get(this);
         Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
         if(menuKeyField != null) {
             menuKeyField.setAccessible(true);
             menuKeyField.setBoolean(config, false);
         }
     } catch (Exception ex) {
         // Ignore
     }
     // ----------------------------------------------------------------------------------------
     
     
     // Get the Database
     DatabaseHelper myDbHelper = new DatabaseHelper(this);
    

 
        try {
 
         myDbHelper.createDataBase();
 
  } catch (IOException ioe) {
 
   throw new Error("Unable to create database");
 
  }
        FetchAllData(); // <----------------------------------------- CALLING THE FUNCTION
}

 

Revise this Paste

Your Name: Code Language: