案例——SQLite游戏玩家管理

  • 需求分析
    我们设计的简单的游戏玩家属性
    Game Player
    编号 id
    玩家 player
    分数 score
    关数 level
  • 功能设计
    1、显示游戏玩家裂变,按分数降序
    2、管理员可手动修改每个玩家的数据
    3、管理员可单机后单独查看一个玩家的数据
    4、管理员可删除一个玩家的数据
  • 功能实现
    1、定义元数据类:GameMetaData和表结构数据内部类GamePlayer
    2、定义数据库操作类 DatabaseAd阿佩尔和DatabaseHelper
    3、MainActivity和GamePlayerFragment用于界面显示
    4、AddFragment中使用Dialog来添加玩家
    5、UpdateFragment显示一个玩家数据并修改显示
    案例展示
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    代码
    DatabaseAdapter
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.example.sqlite_gameplayer.entity.GamePlayer;

import java.util.ArrayList;

public class DatabaseAdapter {
    private DatabaseHelper dbHelper;
    public DatabaseAdapter(Context context){
        dbHelper=new DatabaseHelper(context);
    }
    //添加操作
    public void add(GamePlayer gamePlayer){
        //获取操作数据库的工具类
        SQLiteDatabase db= dbHelper.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put(GameMetaData.GamePlayer.PLAYER,gamePlayer.getPlayer());
        values.put(GameMetaData.GamePlayer.SCORE,gamePlayer.getScore());
        values.put(GameMetaData.GamePlayer.LEVEL,gamePlayer.getLevel());
        db.insert(GameMetaData.GamePlayer.TABLE_NAME,null,values);
        db.close();
    }
    //删除操作
    public void delete(int id){
        SQLiteDatabase db= dbHelper.getWritableDatabase();
        String whereClause=GameMetaData.GamePlayer._ID+"=?";
        String[] whereArgs={String.valueOf(id)};
        //表名,条件,条件的值
        db.delete(GameMetaData.GamePlayer.TABLE_NAME,whereClause,whereArgs);
        db.close();
    }
    //更新操作
    public void update(GamePlayer gamePlayer){
        SQLiteDatabase db= dbHelper.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put(GameMetaData.GamePlayer.PLAYER,gamePlayer.getPlayer());
        values.put(GameMetaData.GamePlayer.SCORE,gamePlayer.getScore());
        values.put(GameMetaData.GamePlayer.LEVEL,gamePlayer.getLevel());
        String whereClause=GameMetaData.GamePlayer._ID+"=?";
        String[] whereArgs={String.valueOf(gamePlayer.getId())};
        //表名,ContentValues,条件,条件的值
        db.update(GameMetaData.GamePlayer.TABLE_NAME,values,whereClause,whereArgs);
        db.close();
    }
    //根据ID查询单个记录
    public GamePlayer findById(int id){
        SQLiteDatabase db=dbHelper.getReadableDatabase();
        //是否去除重复记录,表名,要查询的列,查询条件,查询条件的值,分组条件,分组条件的值,排序,分页条件
        Cursor c= db.query(true,GameMetaData.GamePlayer.TABLE_NAME,null,GameMetaData.GamePlayer._ID+"=?",new String[]{String.valueOf(id)},null,null,null,null);
        GamePlayer gamePlayer=null;
        if (c.moveToNext()){
            gamePlayer=new GamePlayer();
            gamePlayer.setId(c.getInt(c.getColumnIndexOrThrow(GameMetaData.GamePlayer._ID)));
            gamePlayer.setPlayer(c.getString(c.getColumnIndexOrThrow(GameMetaData.GamePlayer.PLAYER)));
            gamePlayer.setScore(c.getInt(c.getColumnIndexOrThrow(GameMetaData.GamePlayer.SCORE)));
            gamePlayer.setLevel(c.getInt(c.getColumnIndexOrThrow(GameMetaData.GamePlayer.LEVEL)));
        }
        c.close();
        db.close();
        return gamePlayer;
    }
    //查询所有
    public ArrayList<GamePlayer> findAll(){
        String sql="select _id,player,score,level from player_table order by score desc";
        SQLiteDatabase db=dbHelper.getReadableDatabase();
        Cursor c= db.rawQuery(sql,null);
        ArrayList<GamePlayer> gamePlayers=new ArrayList<>();
        GamePlayer gamePlayer=null;
        while (c.moveToNext()){
            gamePlayer=new GamePlayer();
            gamePlayer.setId(c.getInt(c.getColumnIndexOrThrow(GameMetaData.GamePlayer._ID)));
            gamePlayer.setPlayer(c.getString(c.getColumnIndexOrThrow(GameMetaData.GamePlayer.PLAYER)));
            gamePlayer.setScore(c.getInt(c.getColumnIndexOrThrow(GameMetaData.GamePlayer.SCORE)));
            gamePlayer.setLevel(c.getInt(c.getColumnIndexOrThrow(GameMetaData.GamePlayer.LEVEL)));
            gamePlayers.add(gamePlayer);
        }
        c.close();
        db.close();
        return gamePlayers;
    }
    //获取总记录数(暂未使用)
    public int getCount(){
        int count=0;
        String sql="select count(_id) from player_table";
        SQLiteDatabase db=dbHelper.getReadableDatabase();
        Cursor c=db.rawQuery(sql,null);
        c.moveToFirst();
        count=c.getInt(0);
        c.close();
        db.close();
        return count;
    }
}

DatabaseHelper

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {
    private static final String DB_NAME="game.db";
    private static final int VERSION=1;

    private static final String CREATE_TABLE_PLATER="CREATE TABLE IF NOT EXISTS player_table("+
            "_id INTEGER PRIMARY KEY AUTOINCREMENT,"+
            "player TEXT,score INTEGER,level INTEGER)";
    private static final String DROP_TABLE_PLAYER="DROP TABLE IF EXISTS player_table";

    public DatabaseHelper(Context context) {
        super(context, DB_NAME, null, VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE_PLATER);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(DROP_TABLE_PLAYER);
        db.execSQL(CREATE_TABLE_PLATER);
    }
}

GameMetaData

import android.provider.BaseColumns;

public final class GameMetaData {
    private GameMetaData(){}
    public static abstract class GamePlayer implements BaseColumns{
        public static final String TABLE_NAME="player_table";
        public static final String PLAYER="player";
        public static final String SCORE="score";
        public static final String LEVEL="level";
    }
}

GamePlayer

public class GamePlayer {
    private int id;
    private String player;
    private int score;
    private int level;

    public GamePlayer(int id, String player, int score, int level) {
        this.id = id;
        this.player = player;
        this.score = score;
        this.level = level;
    }

    public GamePlayer(String player, int score, int level) {
        this.player = player;
        this.score = score;
        this.level = level;
    }

    public GamePlayer() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getPlayer() {
        return player;
    }

    public void setPlayer(String player) {
        this.player = player;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    @Override
    public String toString() {
        return "GamePlayer{" +
                "id=" + id +
                ", player='" + player + '\'' +
                ", score=" + score +
                ", level=" + level +
                '}';
    }
}

AddFragment

import android.app.Activity;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.example.sqlite_gameplayer.R;
import com.example.sqlite_gameplayer.entity.GamePlayer;

import static android.support.v4.os.LocaleListCompat.create;


public class AddFragment extends DialogFragment {
    private AddFragmentListener addFragmentListener;

    public static interface AddFragmentListener {
        public void add(GamePlayer gamePlayer);
    }

    public AddFragment() {
        // Required empty public constructor
    }

    public static AddFragment newInstance() {
        AddFragment frag = new AddFragment();
        return frag;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            addFragmentListener = (AddFragmentListener) activity;
        } catch (ClassCastException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final View view = LayoutInflater.from(getActivity()).inflate(R.layout.create_gameplayer_dialog, null);
        return new AlertDialog.Builder(getActivity())
                .setIcon(android.R.drawable.ic_input_add)
                .setView(view)
                .setTitle(R.string.title_new_game_player)
                .setPositiveButton(R.string.save,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                EditText et_player = view.findViewById(R.id.editText_player);
                                EditText et_score = view.findViewById(R.id.editText2_score);
                                EditText et_level = view.findViewById(R.id.editText3_level);
                                GamePlayer gamePlayer = new GamePlayer();
                                gamePlayer.setPlayer(et_player.getText().toString());
                                gamePlayer.setScore(Integer.parseInt(et_score.getText().toString()));
                                gamePlayer.setLevel(Integer.parseInt(et_level.getText().toString()));
                                addFragmentListener.add(gamePlayer);
                                dialog.dismiss();
                            }
                        }
                        )
            .setNegativeButton(R.string.cancel,
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    }
                    )
                    .create();
    }
}

GamePlayerFragment

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.example.sqlite_gameplayer.R;
import com.example.sqlite_gameplayer.entity.GamePlayer;

import java.util.ArrayList;

public class GamePlayerFragment extends Fragment {


    private GamePlayerFragmentListener gamePlayerFragmentListener;
    private GamePlayerAdapter gamePlayerAdapter;
    public static interface GamePlayerFragmentListener{
        public void showGamePlayerFragment();
        public void showUpdateFragment(int id);
        public void delete(int id);
        public ArrayList<GamePlayer> findAll();
    }

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

        try {
            gamePlayerFragmentListener= (GamePlayerFragmentListener) activity;
        } catch (ClassCastException e) {
            e.printStackTrace();
        }
    }

    public GamePlayerFragment() {
        // Required empty public constructor
    }
    public static GamePlayerFragment newInstance(){
        GamePlayerFragment frag=new GamePlayerFragment();
        return frag;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ArrayList<GamePlayer> gamePlayers=gamePlayerFragmentListener.findAll();
        gamePlayerAdapter=new GamePlayerAdapter(getActivity(),gamePlayers);
    }
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.setHeaderTitle("修改/删除");
        menu.setHeaderIcon(android.R.drawable.ic_menu_edit);
        getActivity().getMenuInflater().inflate(R.menu.listview_context_menu,menu);
    }
    public void onDetach(){
        super.onDetach();
        gamePlayerFragmentListener=null;
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.delete_menu:
                AdapterView.AdapterContextMenuInfo info= (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
                TextView textView_id=info.targetView.findViewById(R.id.textView_id);
                int id=Integer.parseInt(textView_id.getText().toString());
                gamePlayerFragmentListener.delete(id);
                changedData();
                break;
            case R.id.update_menu:
                info= (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
                textView_id=info.targetView.findViewById(R.id.textView_id);
                id=Integer.parseInt(textView_id.getText().toString());
                gamePlayerFragmentListener.showUpdateFragment(id);
                break;
        }
        return super.onContextItemSelected(item);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view= inflater.inflate(R.layout.fragment_game_player, container, false);
        ListView listView=view.findViewById(R.id.listView);
        registerForContextMenu(listView);
        listView.setAdapter(gamePlayerAdapter);
                return view;
    }
    public void changedData(){
        gamePlayerAdapter.setGamePlayers(gamePlayerFragmentListener.findAll());
        gamePlayerAdapter.notifyDataSetChanged();
    }

    private static class GamePlayerAdapter extends BaseAdapter{
        private Context context;
        private ArrayList<GamePlayer> gamePlayers;

        public void setGamePlayers(ArrayList<GamePlayer> gamePlayers){
            this.gamePlayers=gamePlayers;
        }
        public GamePlayerAdapter(Context context,ArrayList<GamePlayer> gamePlayers){
            this.context=context;
            this.gamePlayers=gamePlayers;
        }

        @Override
        public int getCount() {
            return gamePlayers.size();
        }

        @Override
        public Object getItem(int position) {
            return gamePlayers.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder vh=null;
            if(convertView==null){
                convertView=LayoutInflater.from(context).inflate(R.layout.game_player_list_item_layout,null);
                vh=new ViewHolder();
                vh.tv_id=convertView.findViewById(R.id.textView_id);
                vh.tv_player=convertView.findViewById(R.id.textView_player);
                vh.tv_score=convertView.findViewById(R.id.textView_score);
                vh.tv_level=convertView.findViewById(R.id.textView_level);
                convertView.setTag(vh);
            }else {
                vh= (ViewHolder) convertView.getTag();
            }
            GamePlayer g=gamePlayers.get(position);
            vh.tv_id.setText(String.valueOf(g.getId()));
            vh.tv_player.setText(g.getPlayer());
            vh.tv_score.setText(String.valueOf(g.getScore()));
            vh.tv_level.setText(String.valueOf(g.getLevel()));
            return convertView;
        }
        private static class ViewHolder{
            TextView tv_id;
            TextView tv_player;
            TextView tv_score;
            TextView tv_level;
        }
    }

}

UpdateFragment

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.sqlite_gameplayer.R;
import com.example.sqlite_gameplayer.entity.GamePlayer;

public class UpdateFragment extends Fragment implements View.OnClickListener {

    private EditText et_player,et_score,et_level;
private UpdateFragmentListener updateFragmentListener;
private GamePlayer gamePlayer;

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button_save:
                save();
                break;
            case R.id.button_cancel:
                getActivity().getSupportFragmentManager().popBackStack();
                break;
        }
    }

    private void save() {
        GamePlayer g=new GamePlayer();
        g.setId(gamePlayer.getId());
        g.setPlayer(et_player.getText().toString());
        g.setScore(Integer.parseInt(et_score.getText().toString()));
        g.setLevel(Integer.parseInt(et_level.getText().toString()));
        updateFragmentListener.update(g);
        getActivity().getSupportFragmentManager().popBackStack();


    }

    public static interface UpdateFragmentListener{
    public void update(GamePlayer gamePlayer);
    public GamePlayer findById(int id);
}

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            updateFragmentListener= (UpdateFragmentListener) activity;
        } catch (ClassCastException e) {
            e.printStackTrace();
        }
    }

    public UpdateFragment() {
        // Required empty public constructor
    }
    public static UpdateFragment newInstance(int id){
        UpdateFragment frag=new UpdateFragment();
        Bundle b=new Bundle();
        b.putInt("id",id);
        frag.setArguments(b);
        return frag;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        updateFragmentListener=null;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int id=getArguments().getInt("id");
        gamePlayer=updateFragmentListener.findById(id);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view= inflater.inflate(R.layout.fragment_update, container, false);
        TextView tv_id=view.findViewById(R.id.textView_id);
        et_player=view.findViewById(R.id.editText_player);
        et_score=view.findViewById(R.id.editText2_score);
        et_level=view.findViewById(R.id.editText3_level);
        Button button_save=view.findViewById(R.id.button_save);
        Button button_cancel=view.findViewById(R.id.button_cancel);
        button_save.setOnClickListener(this);
        button_cancel.setOnClickListener(this);
        tv_id.setText(String.valueOf(gamePlayer.getId()));
        et_player.setText(gamePlayer.getPlayer());
        et_score.setText(String.valueOf(gamePlayer.getScore()));
        et_level.setText(String.valueOf(gamePlayer.getLevel()));
        return view;
    }

}

MainActivity

import android.content.Intent;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.ShareActionProvider;
import android.view.ContextMenu;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.example.sqlite_gameplayer.db.DatabaseAdapter;
import com.example.sqlite_gameplayer.entity.GamePlayer;
import com.example.sqlite_gameplayer.fragments.AddFragment;
import com.example.sqlite_gameplayer.fragments.GamePlayerFragment;
import com.example.sqlite_gameplayer.fragments.UpdateFragment;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements AddFragment.AddFragmentListener, GamePlayerFragment.GamePlayerFragmentListener, UpdateFragment.UpdateFragmentListener {
private GamePlayerFragment gamePlayerFragment;
private UpdateFragment updateFragment;
private DatabaseAdapter dbAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dbAdapter=new DatabaseAdapter(this);
        showGamePlayerFragment();
    }




    @Override
    public void add(GamePlayer gamePlayer) {
        dbAdapter.add(gamePlayer);
        gamePlayerFragment.changedData();
    }

    @Override
    public void showGamePlayerFragment() {
        gamePlayerFragment=GamePlayerFragment.newInstance();
        FragmentTransaction ft=getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.main_layout,gamePlayerFragment);
        ft.addToBackStack(null);
        ft.commit();

    }

    @Override
    public void showUpdateFragment(int id) {
        updateFragment=UpdateFragment.newInstance(id);
        FragmentTransaction ft=getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.main_layout,updateFragment);
        ft.addToBackStack(null);
        ft.commit();
    }

    @Override
    public void delete(int id) {
        dbAdapter.delete(id);
        gamePlayerFragment.changedData();
    }

    @Override
    public ArrayList<GamePlayer> findAll() {

        return dbAdapter.findAll();
    }

    @Override
    public void update(GamePlayer gamePlayer) {
    dbAdapter.update(gamePlayer);
    gamePlayerFragment.changedData();
    }

    @Override
    public GamePlayer findById(int id) {
        return dbAdapter.findById(id);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode==KeyEvent.KEYCODE_BACK){
            if (getFragmentManager().getBackStackEntryCount()==1){
                finish();
                return true;
            }else {
                getFragmentManager().popBackStack();
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id=item.getItemId();
        switch (id){
            case R.id.add:
                AddFragment createGamePlayerFragment=AddFragment.newInstance();
                createGamePlayerFragment.show(getFragmentManager(),null);
                break;
        }
        return super.onOptionsItemSelected(item);

    }
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

}

button_bg

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@color/blue_lite"></item>
    <item android:drawable="@android:color/transparent"></item>
</selector>

activity_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/main_layout">

    </FrameLayout>
</RelativeLayout>

create_gameplayer_dialog

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:hint="@string/hint_input_player_name"
        android:ems="10"
        android:id="@+id/editText_player"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"/>
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10"
        android:layout_marginTop="5dp"
        android:hint="@string/hint_input_player_score"
        android:id="@+id/editText2_score"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/editText_player"
        android:layout_alignParentEnd="true"
        />
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10"
        android:hint="@string/hint_input_player_level"
        android:layout_marginTop="5dp"
        android:id="@+id/editText3_level"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/editText2_score"
        android:layout_alignParentEnd="true"/>
</RelativeLayout>

fragment_add

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragments.AddFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>

fragment_game_player

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragments.GamePlayerFragment">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listView"
        android:layout_gravity="center"
        android:choiceMode="none"/>
</FrameLayout>

fragment_update

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/relative_layout"
    tools:context=".fragments.UpdateFragment" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView_id"
        android:layout_alignParentEnd="true"
        android:layout_toEndOf="@id/textView6"
        android:layout_alignParentTop="true"/>
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:hint="请输入玩家名称"
        android:id="@+id/editText_player"
        android:layout_below="@+id/textView_id"
        android:layout_marginTop="5dp"
        android:layout_alignParentEnd="true"
        android:layout_toEndOf="@+id/textView6"/>
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10"
        android:layout_marginTop="5dp"
        android:hint="请输入玩家分数"
        android:id="@+id/editText2_score"
        android:layout_below="@id/editText_player"
        android:layout_alignParentEnd="true"
        android:layout_toEndOf="@id/textView7"/>
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10"
        android:hint="请输入玩家关卡数"
        android:layout_marginTop="5dp"
        android:id="@+id/editText3_level"
        android:layout_below="@+id/editText2_score"
        android:layout_alignParentEnd="true"
        android:layout_toEndOf="@id/textView8"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:id="@+id/linearLayout">
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="取消"
            android:background="@drawable/button_bg"
            android:id="@+id/button_cancel"
            />
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="更新"
            android:id="@+id/button_save"
            android:background="@drawable/button_bg"
            />
    </LinearLayout>

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView_id"
        android:layout_alignParentStart="true"
        android:text="序号:" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="玩家:"
        android:id="@+id/textView6"
        android:layout_alignBaseline="@+id/editText_player"
        android:layout_alignParentStart="true"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="分数:"
    android:id="@+id/textView7"
    android:layout_alignBaseline="@+id/editText2_score"
    android:layout_alignParentStart="true"/>
    <TextView
        android:id="@+id/textView8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:text="关卡:"
        android:layout_alignBaseline="@+id/editText3_level"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="提示:您可以修改玩家,分数和关卡,序号不能修改,修改完成后单击保存按钮即可,否则单击取消按钮返回列表界面。"
        android:id="@+id/textView9"`在这里插入代码片`
        android:textColor="@android:color/darker_gray"
        android:layout_marginBottom="50dp"
        android:layout_above="@+id/linearLayout"
        android:layout_alignParentStart="true"
        />
</RelativeLayout>

game_player_list_item_layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:id="@+id/textView1"
        android:layout_alignParentTop="true"
        android:layout_alignBaseline="@id/textView_id"
        android:text="序号:"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView_id"
        android:layout_toEndOf="@id/textView1"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:text=""/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:id="@+id/textView2"
        android:layout_alignParentTop="true"
        android:layout_alignBaseline="@id/textView_player"
        android:text="名称:"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView_player"
        android:layout_below="@+id/textView_id"
        android:layout_alignParentEnd="true"
        android:layout_toEndOf="@id/textView2"
        android:layout_marginTop="5dp"
        android:text=""/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView3"
        android:layout_alignParentStart="true"
        android:layout_marginTop="5dp"
        android:layout_alignParentTop="true"
        android:layout_alignBaseline="@id/textView_score"
        android:text="分数:"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView_score"
        android:layout_below="@+id/textView_player"
        android:layout_alignParentEnd="true"
        android:layout_toEndOf="@id/textView3"
        android:layout_marginTop="5dp"
        android:text=""/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView4"
        android:layout_alignParentStart="true"
        android:layout_marginTop="5dp"
        android:layout_alignParentTop="true"
        android:layout_alignBaseline="@id/textView_level"
        android:text="关卡数:"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView_level"
        android:layout_below="@+id/textView_score"
        android:layout_alignParentEnd="true"
        android:layout_toEndOf="@id/textView4"
        android:layout_marginTop="5dp"
        android:text=""/>
</RelativeLayout>

listview_context_menu

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/delete_menu"
        android:orderInCategory="100" android:title="删除"
        android:icon="@android:drawable/ic_menu_delete"/>
    <item android:id="@+id/update_menu"
        android:orderInCategory="100" android:title="修改"
        android:icon="@android:drawable/ic_menu_edit"/>
</menu>

menu_main

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity">
<item android:id="@+id/add" android:title="@string/add"
    android:icon="@android:drawable/ic_menu_add"
    android:orderInCategory="100" app:showAsAction="always"/>
</menu>

colors

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
<color name="blue_lite">#09D0E9</color>
</resources>

strings

<resources>
    <string name="app_name">SQLite_gamePlayer</string>

    <!-- TODO: Remove or change this placeholder text -->
    <string name="hello_blank_fragment">Hello blank fragment</string>
    <string name="add">添加</string>
    <string name="hint_input_player_name">请输入玩家名称</string>
    <string name="hint_input_player_score">请输入玩家分数</string>
    <string name="hint_input_player_level">请输入玩家关卡数</string>
    <string name="title_new_game_player">新增游戏玩家</string>
    <string name="save">保存</string>
    <string name="cancel">取消</string>
</resources>

styles文件没用到所以就不写了
很多地方都没有采用国际式的写法,而是直接把文本内容写在上面,想修改的可以把文本内容写到strings里,到时候引用对应的值就好了。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值