10天冲刺第六天

今天做了什么:

咨询师预约界面:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".ui.dashboard.DashboardFragment">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:background="@color/white"/>
    <ScrollView android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        tools:ignore="MissingConstraints">
        <LinearLayout
            android:id="@+id/ll_con"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        </LinearLayout>
    </ScrollView>

</LinearLayout>

 咨询师自定义资源界面:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    tools:context=".consulantActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/img_person"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:scaleType="centerCrop"/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:orientation="vertical">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:orientation="horizontal">
                <TextView
                    android:id="@+id/tv_name"
                    android:layout_width="140dp"
                    android:layout_height="match_parent"
                    android:textSize="20dp"/>
                <TextView
                    android:id="@+id/tv_money"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:textSize="20dp"/>
            </LinearLayout>
            <TextView
                android:id="@+id/tv_intro"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
        </LinearLayout>

    </LinearLayout>
    <LinearLayout
        android:id="@+id/myLinear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

    </LinearLayout>

</LinearLayout>

  代码:

package com.example.psychological.ui.dashboard;

import static android.content.Context.MODE_PRIVATE;
import static android.os.SystemClock.sleep;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.example.psychological.R;
import com.example.psychological.approveActivity;
import com.example.psychological.bookActivity;
import com.example.psychological.consulantActivity;
import com.example.psychological.databinding.FragmentDashboardBinding;
import com.example.psychological.javaClass.Consultant;
import com.example.psychological.javaClass.Dao;
import com.example.psychological.loginActivity;

import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class DashboardFragment extends Fragment {

    private static final String NET="192.168.129.224";
    private FragmentDashboardBinding binding;
    private Consultant[] consultants =new Consultant[100];
    private Bitmap[] bitmaps = new Bitmap[100];
    private LinearLayout ll_con;
    private LinearLayout myLinear;
    private int sum=0;
    private int length;
    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case 1:
                    int n=msg.getData().getInt("bit");
                    bitmaps[n]=(Bitmap) msg.obj;
                    break;
                case 2:
                    int i=0;
                    Log.d("1111", "handleMessage: ");
                    while (consultants[i].getId()!=null) {
                        myLinear = (LinearLayout) LayoutInflater.from(getActivity()).inflate(R.layout.consultant,null);
                        TextView tv_name = myLinear.findViewById(R.id.tv_name);
                        TextView tv_money = myLinear.findViewById(R.id.tv_money);
                        TextView tv_intro = myLinear.findViewById(R.id.tv_intro);
                        Button bt_book = myLinear.findViewById(R.id.bt_book);
                        ImageView img_person = myLinear.findViewById(R.id.img_person);
                        tv_name.setText(consultants[i].getName());
                        tv_money.setText("¥"+consultants[i].getMoney());
                        tv_intro.setText(consultants[i].getIntroduction());
                        img_person.setImageBitmap(bitmaps[i]);
                        int finalI = i;
                        bt_book.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                SharedPreferences sharedPreferences = getContext().getSharedPreferences("data",MODE_PRIVATE);
                                SharedPreferences.Editor editor = sharedPreferences.edit();
                                editor.putString("bid",consultants[finalI].getId());
                                editor.commit();
                                startActivity(new Intent(getActivity(), bookActivity.class));
                            }
                        });
                        ll_con.addView(myLinear);
                        i++;
                    }
                    break;
            }
            return true;
        }
    });

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

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        DashboardViewModel dashboardViewModel =
                new ViewModelProvider(this).get(DashboardViewModel.class);

        binding = FragmentDashboardBinding.inflate(inflater, container, false);
        View root = binding.getRoot();
        for(int i=0;i<100;i++) {
            consultants[i]=new Consultant();
        }
        ll_con = root.findViewById(R.id.ll_con);
        initData();
        return root;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
    public void initData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Dao d =new Dao();
                try {
                    consultants=d.allConyes();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
                int j=0;
                while (consultants[j].getId()!=null){
                    length++;
                    j++;
                }
                int i=0;
                while (consultants[i].getId()!=null) {
                    OkHttpClient okHttpClient = new OkHttpClient();
                    Request request1 = new Request.Builder()
                            .url("http://"+NET+":8080/apply/download")
                            .addHeader("id", consultants[i].getId())
                            .build();
                    int finalI = i;
                    int finalI1 = i;
                    okHttpClient.newCall(request1).enqueue(new Callback() {
                        public void onFailure(Call call, IOException e) {

                        }

                        public void onResponse(Call call, Response response) throws IOException {
                            InputStream inputStream = response.body().byteStream();//得到图片的流
                            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                            Bundle bundle = new Bundle();
                            bundle.putInt("bit",finalI);
                            Message msg = new Message();
                            msg.what=1;
                            msg.obj=bitmap;
                            msg.setData(bundle);
                            sum++;
                            handler.sendMessage(msg);
                            if(length==sum) {
                                Message msg1 = new Message();
                                msg1.what = 2;
                                handler.sendMessage(msg1);
                            }
                        }
                    });
                    i++;
                }
            }
        }).start();
    }
}

  明天做什么:

社区 投稿的提交和展示

遇到的问题:图片加载不同步

posted @ 2024-04-25 19:47  umiQa  阅读(1)  评论(0编辑  收藏  举报