学习进度条

今日所花时间:一小时
今日代码量:100行
博客量:1篇
了解到的知识点: 手机端APP开发
继续对之前个人作业学习APP开发的代码进行优化
1.学生用户,能够查看自己每周设置的目标
activity_set_goal.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".SetGoalActivity">

    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:text="设置每周目标"
        android:textSize="24sp"
        android:textStyle="bold"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tvWeekStartDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="本周开始日期:"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tvTitle" />

    <DatePicker
        android:id="@+id/datePicker"
        android:layout_width="347dp"
        android:layout_height="297dp"
        android:layout_marginTop="24dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tvWeekStartDate" />

    <TextView
        android:id="@+id/tvGoalContent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="目标内容:"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@id/datePicker" />

    <EditText
        android:id="@+id/etGoalContent"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:hint="输入本周学习目标"
        android:inputType="textMultiLine"
        android:minLines="3"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tvGoalContent" />

    <Button
        android:id="@+id/btnSetGoal"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="设置目标"
        android:layout_marginTop="24dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/etGoalContent" />
    <Button
        android:id="@+id/btnViewGoals"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="查看历史目标"
        android:layout_marginTop="16dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/btnSetGoal" />

</androidx.constraintlayout.widget.ConstraintLayout>

WeeklyGoalsActivity

package com.example.study0327;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.study0327.api.ApiService;
import com.example.study0327.api.RetrofitClient;
import com.example.study0327.modules.WeeklyGoal;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class WeeklyGoalsActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private WeeklyGoalsAdapter adapter;
    private String token;

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

        // 获取用户token
        SharedPreferences prefs = getSharedPreferences("user_prefs", MODE_PRIVATE);
        token = "Bearer " + prefs.getString("auth_token", "").trim();

        initViews();
        loadWeeklyGoals();
    }

    private void initViews() {
        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new WeeklyGoalsAdapter();
        recyclerView.setAdapter(adapter);
    }

    private void loadWeeklyGoals() {
        ApiService apiService = RetrofitClient.getClient().create(ApiService.class);
        Call<List<WeeklyGoal>> call = apiService.getWeeklyGoals(token);

        call.enqueue(new Callback<List<WeeklyGoal>>() {
            @Override
            public void onResponse(Call<List<WeeklyGoal>> call, Response<List<WeeklyGoal>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    adapter.setGoals(response.body());
                } else {
                    Toast.makeText(WeeklyGoalsActivity.this, "获取周目标失败", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<List<WeeklyGoal>> call, Throwable t) {
                Toast.makeText(WeeklyGoalsActivity.this, "网络错误: " + t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}

WeeklyGoalsAdapter

package com.example.study0327;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.study0327.modules.WeeklyGoal;

import java.util.ArrayList;
import java.util.List;

public class WeeklyGoalsAdapter extends RecyclerView.Adapter<WeeklyGoalsAdapter.ViewHolder> {
    private List<WeeklyGoal> goals = new ArrayList<>();

    public void setGoals(List<WeeklyGoal> goals) {
        this.goals = goals;
        notifyDataSetChanged();
    }

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        WeeklyGoal goal = goals.get(position);
        holder.tvWeekStartDate.setText("周开始日期: " + goal.getWeekStartDate());
        holder.tvGoalContent.setText(goal.getGoalContent());
    }

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

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvWeekStartDate;
        TextView tvGoalContent;

        ViewHolder(View itemView) {
            super(itemView);
            tvWeekStartDate = itemView.findViewById(R.id.tvWeekStartDate);
            tvGoalContent = itemView.findViewById(R.id.tvGoalContent);
        }
    }
}

posted @ 2025-04-09 19:55  haoyinuo  阅读(11)  评论(0)    收藏  举报