显示从SQLite到Master详细信息流的数据

时间:2014-11-15 15:49:47

标签: java android sqlite master-detail

我已经完成了创建虚拟主细节流程项目。

我尽可能多地尝试,但结果仍有错误。 请告诉我哪些类应该修改哪些内容,或者我应该如何编码?

LecturerEntity.java

public class LecturerEntity {

    private String LecturerID;
    private String LecturerName;
    private String LecturerPosition;
    private int LecturerPhoneNumber;
    private String LecturerEmail;
    private String LecturerRoomNumber;

    public LecturerEntity(){

    }

    public LecturerEntity (String LecturerID, String LecturerName, String LecturerPosition, int LecturerPhoneNumber, String LecturerEmail, String LecturerRoomNumber) {
        this.LecturerID = LecturerID;
        this.LecturerName =LecturerName;
        this.LecturerPosition = LecturerPosition;
        this.LecturerPhoneNumber = LecturerPhoneNumber;
        this.LecturerEmail = LecturerEmail;
        this.LecturerRoomNumber = LecturerRoomNumber;
    }

    public String getLecturerID() {
        return LecturerID;
    }

    public void setLecturerID(String LecturerID) {
        this.LecturerID = LecturerID;
    }

    public String getLecturerName() {
        return LecturerName;
    }

    public void setLecturerName(String LecturerName) {
        this.LecturerName = LecturerName;
    }

    public int getLecturerPhoneNumber() {
        return LecturerPhoneNumber;
    }

    public void setLecturerPhoneNumber(int LecturerPhoneNumber) {
        this.LecturerPhoneNumber = LecturerPhoneNumber;
    }

    public String getLecturerEmail() {
        return LecturerEmail;
    }

    public void setLecturerEmail(String LecturerEmail) {
        this.LecturerEmail = LecturerEmail;
    }

    public String getLecturerRoomNumber() {
        return LecturerRoomNumber;
    }

    public void setLecturerRoomNumber(String LecturerRoomNumber) {
        this.LecturerRoomNumber = LecturerRoomNumber;
    }

    public String getLecturerPosition() {
        return LecturerPosition;
    }

    public void setLecturerPosition(String LecturerPosition) {
        this.LecturerPosition = LecturerPosition;
    }

    @Override
    public String toString() {
        return LecturerName;
    }
}

LecturerController.java

package Controller;

public class LecturerController extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "LecturerStudentAppointment";
    private static final String TABLE_LECTURER = "Lecturer";

    private static final String LecturerID = "LecturerID";
    private static final String LecturerName = "LecturerName";
    private static final String LecturerPosition = "LecturerPosition";
    private static final String LecturerPhoneNumber = "LecturerPhoneNumber";
    private static final String LecturerEmail = "LecturerEmail";
    private static final String LecturerRoomNumber = "LecturerRoomNumber" ;

    public LecturerController(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    //CREATING TABLES
    public void onCreate(SQLiteDatabase db) {
        String CREATE_LECTURER_TABLE = "CREATE TABLE" + TABLE_LECTURER + "("
                + LecturerID + "INTEGER PRIMARY KEY," 
                + LecturerName + "TEXT,"
                + LecturerPosition + "TEXT,"
                + LecturerPhoneNumber + "INTEGER,"
                + LecturerEmail + "TEXT,"
                + LecturerRoomNumber + "TEXT" + ")";
    }

    //UPGRADING DATABASE
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_LECTURER);

        // Create tables again
        onCreate(db);
    }

    /**
     * All CRUD(Create, Read, Update, Delete) Operations
     */

    //ADD LECTURER
    void addLecturer(LecturerEntity LecturerEntity) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(LecturerID, LecturerEntity.getLecturerID()); // Contact Name
        values.put(LecturerName, LecturerEntity.getLecturerName());
        values.put(LecturerPosition, LecturerEntity.getLecturerPosition());
        values.put(LecturerPhoneNumber, LecturerEntity.getLecturerPhoneNumber());
        values.put(LecturerEmail, LecturerEntity.getLecturerEmail());
        values.put(LecturerRoomNumber, LecturerEntity.getLecturerRoomNumber());

        // Inserting Row
        db.insert(TABLE_LECTURER, null, values);
        db.close(); // Closing database connection
    }

    //GET ONE LECTURER
    LecturerEntity getOneLecturer(int lecturerID) {
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(TABLE_LECTURER, new String[] { LecturerID,
                LecturerName, LecturerPosition,LecturerPhoneNumber, LecturerEmail, LecturerRoomNumber}, LecturerID + "=?",
                new String[] { String.valueOf(lecturerID) }, null, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();

        LecturerEntity LecturerEntity = new LecturerEntity(cursor.getString(0),
                cursor.getString(1), cursor.getString(2), cursor.getInt(3), cursor.getString(4), cursor.getString(5));
        // return contact
        return LecturerEntity;
    }

    // Getting All data
    public List<LecturerEntity> getAllLecturerEntity() {
        List<LecturerEntity> lecturerList = new ArrayList<LecturerEntity>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_LECTURER;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                LecturerEntity LecturerEntity = new LecturerEntity();
                LecturerEntity.setLecturerID(cursor.getString(0));
                LecturerEntity.setLecturerName(cursor.getString(1));
                LecturerEntity.setLecturerPosition(cursor.getString(2));
                LecturerEntity.setLecturerPhoneNumber(cursor.getInt(3));
                LecturerEntity.setLecturerEmail(cursor.getString(4));
                LecturerEntity.setLecturerRoomNumber(cursor.getString(5));
                // Adding contact to list
                lecturerList.add(LecturerEntity);
            } while (cursor.moveToNext());
        }

        // return contact list
        return lecturerList;
    }

    public int updateLecturer(LecturerEntity LecturerEntity) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(LecturerID, LecturerEntity.getLecturerID()); // Contact Name
        values.put(LecturerName, LecturerEntity.getLecturerName());
        values.put(LecturerPosition, LecturerEntity.getLecturerPosition());
        values.put(LecturerPhoneNumber, LecturerEntity.getLecturerPhoneNumber());
        values.put(LecturerEmail, LecturerEntity.getLecturerEmail());
        values.put(LecturerRoomNumber, LecturerEntity.getLecturerRoomNumber());

        // updating row
        return db.update(TABLE_LECTURER, values, LecturerID + " = ?",
                new String[] { String.valueOf(LecturerEntity.getLecturerID()) });
    }

 // Deleting single lecturer
    public void deleteLecturer(LecturerEntity LecturerEntity) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_LECTURER, LecturerID + " = ?",
                new String[] { String.valueOf(LecturerEntity.getLecturerID()) });
        db.close();
    }

 // Getting lecturer Count
    public int getContactsCount() {
        String countQuery = "SELECT  * FROM " + TABLE_LECTURER;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        cursor.close();

        // return count
        return cursor.getCount();
    }
}

ItemListActivity.java

private boolean mTwoPane;

@Override
public void onItemSelected(String id) {
    if (mTwoPane) {
        // In two-pane mode, show the detail view in this activity by
        // adding or replacing the detail fragment using a
        // fragment transaction.
        Bundle arguments = new Bundle();
        arguments.putString(ItemDetailFragment.ARG_ITEM_ID, id);
        ItemDetailFragment fragment = new ItemDetailFragment();
        fragment.setArguments(arguments);
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.item_detail_container, fragment)
                .commit();

    } else {
        // In single-pane mode, simply start the detail activity
        // for the selected item ID.
        Intent detailIntent = new Intent(this, ItemDetailActivity.class);
        detailIntent.putExtra(ItemDetailFragment.ARG_ITEM_ID, id);
        startActivity(detailIntent);
    }
}

ItemDetailActivity.java

public class ItemDetailActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_item_detail);

        // Show the Up button in the action bar.
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        // savedInstanceState is non-null when there is fragment state
        // saved from previous configurations of this activity
        // (e.g. when rotating the screen from portrait to landscape).
        // In this case, the fragment will automatically be re-added
        // to its container so we don't need to manually add it.
        // For more information, see the Fragments API guide at:
        //
        // http://developer.android.com/guide/components/fragments.html
        //
        if (savedInstanceState == null) {
            // Create the detail fragment and add it to the activity
            // using a fragment transaction.
            Bundle arguments = new Bundle();
            arguments.putString(ItemDetailFragment.ARG_ITEM_ID,
                    getIntent().getStringExtra(ItemDetailFragment.ARG_ITEM_ID));
            ItemDetailFragment fragment = new ItemDetailFragment();
            fragment.setArguments(arguments);
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.item_detail_container, fragment)
                    .commit();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            // This ID represents the Home or Up button. In the case of this
            // activity, the Up button is shown. Use NavUtils to allow users
            // to navigate up one level in the application structure. For
            // more details, see the Navigation pattern on Android Design:
            //
            // http://developer.android.com/design/patterns/navigation.html#up-vs-back
            //
            NavUtils.navigateUpTo(this, new Intent(this, ItemListActivity.class));
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

ItemDetailFragment.java

public class ItemDetailFragment extends Fragment {

    public ItemDetailFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments().containsKey(ARG_ITEM_ID)) {
            // Load the dummy content specified by the fragment
            // arguments. In a real-world scenario, use a Loader
            // to load content from a content provider.
            mItem = DummyContent.ITEM_MAP.get(getArguments().getString(ARG_ITEM_ID));
            mItem = ((ItemListApplication)getActivity().getApplication()).getItem(getArguments().getInt(
                    ARG_ITEM_ID));
        }
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_item_detail, container, false);

        // Show the dummy content as text in a TextView.
        if (LecturerItem != null) {
            ((TextView) rootView.findViewById(R.id.item_detail)).setText(LecturerItem.getLecturerName());
        }
        return rootView;
    }
}

ItemListFragment.java

public class ItemListFragment extends ListFragment {

    private static final String STATE_ACTIVATED_POSITION = "activated_position";

    private Callbacks mCallbacks = sDummyCallbacks;

    private int mActivatedPosition = ListView.INVALID_POSITION;

    public interface Callbacks {
        public void onItemSelected(String id);
    }

    private static Callbacks sDummyCallbacks = new Callbacks() {
        @Override
        public void onItemSelected(String id) {
        }
    };

    public ItemListFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        FragmentActivity activity = getActivity();
        setListAdapter(((ItemListApplication)activity.getApplication()).getAdapter(activity)); 

        // TODO: replace with a real list adapter.
        /*setListAdapter(new ArrayAdapter<DummyContent.DummyItem>(
                getActivity(),
                android.R.layout.simple_list_item_activated_1,
                android.R.id.text1,
                DummyContent.ITEMS));  */
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        // Restore the previously serialized activated item position.
        if (savedInstanceState != null
                && savedInstanceState.containsKey(STATE_ACTIVATED_POSITION)) {
            setActivatedPosition(savedInstanceState.getInt(STATE_ACTIVATED_POSITION));
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        // Activities containing this fragment must implement its callbacks.
        if (!(activity instanceof Callbacks)) {
            throw new IllegalStateException("Activity must implement fragment's callbacks.");
        }

        mCallbacks = (Callbacks) activity;
    }

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

        // Reset the active callbacks interface to the dummy implementation.
        mCallbacks = sDummyCallbacks;
    }

    @Override
    public void onListItemClick(ListView listView, View view, int position, long id) {
        super.onListItemClick(listView, view, position, id);

        // Notify the active callbacks interface (the activity, if the
        // fragment is attached to one) that an item has been selected.
        mCallbacks.onItemSelected(LecturerController.getAllLecturerEntity.getLecturerID());
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (mActivatedPosition != ListView.INVALID_POSITION) {
            // Serialize and persist the activated item position.
            outState.putInt(STATE_ACTIVATED_POSITION, mActivatedPosition);
        }
    }

    public void setActivateOnItemClick(boolean activateOnItemClick) {
        // When setting CHOICE_MODE_SINGLE, ListView will automatically
        // give items the 'activated' state when touched.
        getListView().setChoiceMode(activateOnItemClick
                ? ListView.CHOICE_MODE_SINGLE
                : ListView.CHOICE_MODE_NONE);
    }

    private void setActivatedPosition(int position) {
        if (position == ListView.INVALID_POSITION) {
            getListView().setItemChecked(mActivatedPosition, false);
        } else {
            getListView().setItemChecked(position, true);
        }

        mActivatedPosition = position;
    }
}

** @Michal Ciuba @Der Golem我刚刚插入了LecturerController.onCreate因为要在 ItemListFragment.java 中创建LecturerEntity的表,它应该在此更改吗? onItemSelected(DummyContent.ITEMS.get(position).id) ItemDetailFragment 应更改为此DummyContent.ITEM_MAP.get(getArguments()。getString(ARG_ITEM_ID) ); _ **

0 个答案:

没有答案