Android Open Source - sloop-sql Table List Fragment






From Project

Back to project page sloop-sql.

License

The source code is released under:

SloopSQL is released into the Public Domain. There are no restrictions on how you may use this code, and there is no warranty or guarantee of fitness for anything. USE AT YOUR OWN RISK (and enjoy).

If you think the Android project sloop-sql listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.megginson.sloopsql;
/*w  ww. j a va  2  s  .c om*/
import android.app.DialogFragment;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

/**
 * Dialog fragment to display a list of tables.
 */
public class TableListFragment extends DialogFragment
{

  //
  // Internal fragment state
  //

  /**
   * The fragment's root view. set in {@link #onCreateView}
   */
   private ViewGroup mFragmentView;

  /**
   * The database to query.
   */
  private SQLiteDatabase mDatabase;

  /**
   * Any existing query result.
   */
  private Cursor mCursor;


  //
  // Fragment lifecycle methods
  //

  /**
   * Lifecycle event: fragment first created.
   */
  @Override
  public void onCreate(Bundle savedInstanceState)
  {  
    super.onCreate(savedInstanceState);

    mDatabase = new DatabaseHandler(getActivity()).getReadableDatabase();
  }

  /**
   * Lifecycle event: fragment destroyed.
   */
  @Override
  public void onDestroy()
  {
    super.onDestroy();

    if (mCursor != null)
    {
      mCursor.close();
      mCursor = null;
    }

    if (mDatabase != null)
    {
      mDatabase.close();
      mDatabase = null;
    }
  }

  /**
   * Lifecycle event: fragment creates or recreates its view.
   */
  @Override
  public View onCreateView(LayoutInflater inflator, ViewGroup container, Bundle savedInstanceState)
  {
    mFragmentView = (ViewGroup)inflator.inflate(R.layout.table_list, container, false);
    setup_ui();
    return mFragmentView;
  }

  @Override
  public void onResume()
  {
    super.onResume();

    // Generate a list of tables
    do_list_tables();
  }


  //
  // Actions
  //

  /**
   * Show a list of tables and views.
   */
  private void do_list_tables()
  {
    try
    {
      mCursor = mDatabase.rawQuery("select type, name from sqlite_master where type in ('table', 'view')", null);
      get_list_view().setAdapter(new TableListAdapter(mCursor));
    }
    catch (Throwable t)
    {
      Util.toast(getActivity(), t.getMessage());
    }
  }

  /**
   * Select a table.
   */
  private void do_select_table_row(int position)
  {
    mCursor.moveToPosition(position);
    String tableName = mCursor.getString(mCursor.getColumnIndex("name"));
    ((Listener)getActivity()).onTableSelected(tableName);
    dismiss();
  }


  //
  // UI methods
  //

  /**
   * Set up the UI components of the activity.
   */
  private void setup_ui()
  {
    getDialog().setTitle(R.string.title_table_list);
    get_list_view().setOnItemClickListener(new ListView.OnItemClickListener() {
        public void onItemClick(AdapterView parent, View view, int position, long id)
        {
          do_select_table_row(position);
        }
      });
  }

  /**
   * Get the table list view.
   */
  private ListView get_list_view()
  {
    return (ListView)mFragmentView.findViewById(R.id.table_list);
  }

  /**
   * Callback listener for the activity to implement.
   */
  interface Listener
  {  

    /**
     * Called when the user selects a table and closes the dialog.
     *
     * The parent activity must implement this interface.
     */
    public void onTableSelected(String tableName);
  }

}




Java Source Code List

com.megginson.sloopsql.AsyncResult.java
com.megginson.sloopsql.CSVCursorSerializer.java
com.megginson.sloopsql.DatabaseHandler.java
com.megginson.sloopsql.MainActivity.java
com.megginson.sloopsql.QueryFragment.java
com.megginson.sloopsql.QueryResultAdapter.java
com.megginson.sloopsql.ScriptFragment.java
com.megginson.sloopsql.TabListener.java
com.megginson.sloopsql.TableListAdapter.java
com.megginson.sloopsql.TableListFragment.java
com.megginson.sloopsql.Util.java