iT邦幫忙

2024 iThome 鐵人賽

DAY 29
0

依據上篇最後的預告,今天我會將CardView、MainActivity與SharedPreferences部分都用好。

這一篇會比較多程式碼,文字部分就佔少數,因為基本上之前能講都講過了,如果這次出現之前沒講這次也沒講的東西,請原諒我一定是我忘記了,事先跟大家說抱歉(┬┬﹏┬┬)


最後的APP

MainActivity

  • 宣告
private Button button;
    private RecyclerView recyclerView;
    private MyListAdapter myListAdapter;
    private TextView link,play;

    private ArrayList<HashMap<String, String>> mArrayList;
    private Context context = this;

    private MainPresenter mainPresenter;
    private SP sp;
  • 綁定元件
link = findViewById(R.id.main_link_tv);
play = findViewById(R.id.main_play_tv);
button = findViewById(R.id.main_edit_btn);
recyclerView = findViewById(R.id.main_list_rl);
  • 使TextView有超連結功能
link.setMovementMethod(LinkMovementMethod.getInstance());
  • Presenter、SharedPreferences等同之前幾篇步驟一樣
mainPresenter =new MainPresenter(this,this);

sp = new SP(this);

mainPresenter.makeData();
setRecyclerView(mArrayList);
  • 這次RecyclerView用呼叫方式
//setRecyclerView
    private void setRecyclerView(ArrayList<HashMap<String, String>> mArrayList) {
        myListAdapter = new MyListAdapter(mArrayList, this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(myListAdapter);
    }
  • 跳轉Activity
 play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, PlayActivity.class);
                startActivity(intent);//將intent 發送到Android,判別後顯示到SA畫面中
            }
        });
  • 按鈕有Dialog功能,Dialog之前也介紹過,只是增加新增功能
button.setOnClickListener((v) -> {
            AlertDialog.Builder dialog = new AlertDialog.Builder(this);
            final EditText editText = new EditText(this);
            //設置標題文字
            dialog.setTitle("文字顯示");
            //使用輸入editText
            dialog.setView(editText);

            //左邊按鈕
            dialog.setNeutralButton("增加上次輸入", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    editText.setText(sp.loadData());
                    String inputText = editText.getText().toString();
                    myListAdapter.add(inputText);
                    Toast.makeText(context, inputText+",新增成功", Toast.LENGTH_SHORT).show();
                }
            });

            //設置 關閉 按鈕和點擊事件
            dialog.setNegativeButton("關閉", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    // TODO: ...
                }
            });
            //設置 確定 按鈕和點擊事件
            dialog.setPositiveButton("確定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    // TODO: ...
                    String inputText = editText.getText().toString();
                    myListAdapter.add(inputText);
                    Toast.makeText(context, inputText+",新增成功", Toast.LENGTH_SHORT).show();
                }
            });
            //顯示Dialog
            dialog.show();
        });

(完整程式碼)

public class MainActivity extends AppCompatActivity implements MainContract.view{
    private Button button;
    private RecyclerView recyclerView;
    private MyListAdapter myListAdapter;
    private TextView link,play;

    private ArrayList<HashMap<String, String>> mArrayList;
    private Context context = this;

    private MainPresenter mainPresenter;
    private SP sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);

        link = findViewById(R.id.main_link_tv);
        play = findViewById(R.id.main_play_tv);
        button = findViewById(R.id.main_edit_btn);
        recyclerView = findViewById(R.id.main_list_rl);

        link.setMovementMethod(LinkMovementMethod.getInstance());

        mainPresenter =new MainPresenter(this,this);

        sp = new SP(this);

        mainPresenter.makeData();
        setRecyclerView(mArrayList);

        play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, PlayActivity.class);
                startActivity(intent);//將intent 發送到Android,判別後顯示到SA畫面中
            }
        });

        button.setOnClickListener((v) -> {
            AlertDialog.Builder dialog = new AlertDialog.Builder(this);
            final EditText editText = new EditText(this);
            //設置標題文字
            dialog.setTitle("文字顯示");
            //使用輸入editText
            dialog.setView(editText);

            //左邊按鈕
            dialog.setNeutralButton("增加上次輸入", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    editText.setText(sp.loadData());
                    String inputText = editText.getText().toString();
                    myListAdapter.add(inputText);
                    Toast.makeText(context, inputText+",新增成功", Toast.LENGTH_SHORT).show();
                }
            });

            //設置 關閉 按鈕和點擊事件
            dialog.setNegativeButton("關閉", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    // TODO: ...
                }
            });
            //設置 確定 按鈕和點擊事件
            dialog.setPositiveButton("確定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    // TODO: ...
                    String inputText = editText.getText().toString();
                    myListAdapter.add(inputText);
                    Toast.makeText(context, inputText+",新增成功", Toast.LENGTH_SHORT).show();
                }
            });
            //顯示Dialog
            dialog.show();
        });
    }

    //setRecyclerView
    private void setRecyclerView(ArrayList<HashMap<String, String>> mArrayList) {
        myListAdapter = new MyListAdapter(mArrayList, this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(myListAdapter);
    }

    @Override
    public void getData(ArrayList arrayList) {
        mArrayList = arrayList;
    }

MainPresenter

跟之前MVP架構介紹沒多大差別,這裡就不多述。

public class MainPresenter implements MainContract.presenter{
    private MainContract.view callBack;
    ArrayList<HashMap<String, String>> mArrayList = new ArrayList<>();
    private Activity activity;

    public MainPresenter(MainContract.view view, Activity activity){
        //接收View。
        this.callBack =view;
        this.activity = activity;
    }

    //makeData
    @Override
    public void makeData() {
        for (int i = 0; i < 10; i++) {
            HashMap<String, String> hashMap = new HashMap<>();
            hashMap.put("msg",activity.getResources().obtainTypedArray(R.array.msg).getString(i));
            mArrayList.add(hashMap);
        }
        callBack.getData(mArrayList);
    }
}

這裡我利用getResources().obtainTypedArray(R.array.msg).getString(i) 將我之前寫在strings.xml中的文字一一放進去。

MainContract

public interface MainContract {
    interface view{
        void getData(ArrayList arrayList);
    }

    interface presenter{
        void makeData();
    }
}

SP(SharedPreferences)

跟之前說的沒有特別大差別,不了解可以往前翻翻,這裡是幫我記錄上次新增的文字,之後方便拿取與儲存。

public class SP {
    private SharedPreferences spf;
    private Context context;
    private static String MSG = "msg";

    public SP(Context context) {
        this.context = context;
        spf = this.context.getSharedPreferences(context.getString(R.string.app_name), Context.MODE_PRIVATE);
    }

    public void saveData(String msg) {
        spf.edit().putString(MSG, msg).apply();
    }

    public String loadData() {
        return spf.getString(MSG," ");
    }
}

MyListAdapter

這個在RecyclerView那篇有介紹過了,不了解可以回去看看,有如何建立跟它那些功能之類的,這裡我增加了新增與更改的建構元。

  • 新增與更改
//新增
    public void add(String text) {
        HashMap<String, String> map = new HashMap<>();
        map.put(KEY, text);
        mArrayList.add(map);
        notifyItemInserted(mArrayList.size() - 1);

        sp.saveData(text);
    }

    //更改
    public void update(int position, String text) {
        if (position >= 0 && position < mArrayList.size()) {
            HashMap<String, String> map = mArrayList.get(position);
            map.put(KEY, text);
            notifyItemChanged(position);
        }
    }

(完整程式碼)

public class MyListAdapter extends RecyclerView.Adapter<MyListAdapter.ViewHolder> {
    private ArrayList<HashMap<String, String>> mArrayList;
    private Activity activity;
    private SP sp;
    private static final String KEY = "msg";

    public MyListAdapter(ArrayList<HashMap<String, String>> arrayList, Activity activity){
        this.mArrayList = arrayList;
        this.activity = activity;
        sp = new SP(activity);
    }

    //新增
    public void add(String text) {
        HashMap<String, String> map = new HashMap<>();
        map.put(KEY, text);
        mArrayList.add(map);
        notifyItemInserted(mArrayList.size() - 1);

        sp.saveData(text);
    }

    //更改
    public void update(int position, String text) {
        if (position >= 0 && position < mArrayList.size()) {
            HashMap<String, String> map = mArrayList.get(position);
            map.put(KEY, text);
            notifyItemChanged(position);
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView msg;

        public ViewHolder(View itemView) {
            super(itemView);
            msg = itemView.findViewById(R.id.rl_msg_tv);
            }
    }

    @Override
    public MyListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.recycleritem_cardview, parent, false);
        return new MyListAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyListAdapter.ViewHolder holder, int position) {
        HashMap<String, String> item = mArrayList.get(position);
        String msg = item.get(KEY); // 從 HashMap 中取出 msg
        holder.msg.setText(msg);

        holder.itemView.setOnClickListener((v)->{
            AlertDialog.Builder dialog = new AlertDialog.Builder(activity);
            final EditText editText = new EditText(activity);
            //設置標題文字
            dialog.setTitle("文字顯示");
            //使用輸入editText
            dialog.setView(editText);
            editText.setText(msg);

            //設置左邊按鈕和點擊事件
            dialog.setNegativeButton("關閉", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    // TODO: ...
                }
            });
            //設置右邊按鈕和點擊事件
            dialog.setPositiveButton("確定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    // TODO: ...
                    String inputText = editText.getText().toString();
                    update(position, inputText);
                }
            });
            //顯示Dialog
            dialog.show();
        });
    }

    @Override
    public int getItemCount() {
        return mArrayList.size();
    }

}

以上就是中篇所有內容,接著就是最終篇小遊戲部分完成就大功告成了,明天也是最後一天,加油~( •̀ ω •́ )✧


上一篇
最後的APP(上篇) Day28
下一篇
最終篇 Day30
系列文
Android 元件總動員 - 運用與實踐元件指南30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言