冲刺二十一天

实现书籍显示

整体书籍展示

冲刺二十一天

 

 BookListActivity

冲刺二十一天
package com.lh.std_everything.ui.home.hometype.studyshare.share;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.lh.std_everything.HttpUtil.HttpUtil;
import com.lh.std_everything.R;
import com.lh.std_everything.ui.home.hometype.news.adapter.NewsAdapter;

import org.json.JSONArray;
import org.json.JSONException;

public class BookListActivity extends AppCompatActivity {
    public ImageView upload,search;
    public RecyclerView rc_book;
    public BooksAdapter adapter;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_list);
        init();
        upload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(BookListActivity.this,bookuploadActivity.class);
                startActivity(intent);
            }
        });
        rc_book.setLayoutManager(new LinearLayoutManager(BookListActivity.this));
        adapter = new BooksAdapter();
        getThreadData();
        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(@NonNull Message msg) {     //线程回调更新数据
                if (msg.what == 200) {
                    adapter.setJsonArray((JSONArray) msg.obj);
                    rc_book.setAdapter(adapter);
                }
                return true;
            }
        });

    }
    public void init()
    {
        upload=findViewById(R.id.uploadimg);
        search=findViewById(R.id.searchimg);
        rc_book=findViewById(R.id.bookrv);
    }

    private void getThreadData() {                     //线程封装网络请求
        new Thread(new Runnable() {
            @Override
            public void run() {
                String reqdata = "";
                String address="http://116.62.178.231:8080/BaiXiaoSheng/getallbooks";
                try {
                    final JSONArray json = new JSONArray(String.valueOf(HttpUtil.sendPost(address, reqdata)));
                    Log.d("tag", json.toString());
                    Message msg = new Message();
                    msg.what = 200;
                    msg.obj = json;
                    handler.sendMessage(msg);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
View Code

BooksAdapter

冲刺二十一天
package com.lh.std_everything.ui.home.hometype.studyshare.share;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

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

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.lh.std_everything.R;
import com.lh.std_everything.ui.home.hometype.news.adapter.NewsAdapter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class BooksAdapter extends RecyclerView.Adapter<BooksAdapter.MyViewHolder>{
    JSONArray jsonArray=new JSONArray();
    private View itemview;
    public String arr[]={"begin","one","two","three","four","five","six","seven","eight","nine","ten","others"};
    public void setJsonArray(JSONArray jsonArray) {
        this.jsonArray = jsonArray;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater=LayoutInflater.from(parent.getContext());
        itemview=layoutInflater.inflate(R.layout.cell_book,parent,false);
        return new BooksAdapter.MyViewHolder(itemview);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        JSONObject bean=null;
        try {
            bean= jsonArray.getJSONObject(position);
            holder.textViewName.setText(bean.getString("bookname"));
            holder.textViewAuthor.setText(bean.getString("bookauthor"));
            holder.textViewPublish.setText(bean.getString("bookpublish"));
            String []urls=bean.getString("head").split("\\$");
            RequestOptions options = new RequestOptions()
                    .error(R.drawable.error)
                    .placeholder(R.drawable.loading);
            if(urls.length!=0) {
                for(int i=0;i<urls.length;i++)
                {
                    if(!urls[i].equals("null"))
                    {
                        Log.i("url",urls[i]);
                        Glide.with(itemview)
                                .load(urls[i])
                                .apply(options)
                                .into(holder.bookphoto);
                        break;
                    }
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle=new Bundle();
                Intent intent=new Intent(itemview.getContext(), ShowBookActivity.class);
                try {
                    bundle.putString("bookname", jsonArray.getJSONObject(position).getString("bookname"));
                    bundle.putString("bookauthor", jsonArray.getJSONObject(position).getString("bookauthor"));
                    bundle.putString("bookpublish", jsonArray.getJSONObject(position).getString("bookpublish"));
                    bundle.putString("username", jsonArray.getJSONObject(position).getString("username"));
                    bundle.putString("head", jsonArray.getJSONObject(position).getString("head"));
                    Log.i("bundle中的username:", ""+jsonArray.getJSONObject(position).getString("username"));
                    for(int i=1;i<=11;i++)
                    {
                        if(jsonArray.getJSONObject(position).has(arr[i]))
                        {
                            bundle.putString(arr[i], jsonArray.getJSONObject(position).getString(arr[i]));
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                intent.putExtras(bundle);
                itemview.getContext().startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return jsonArray.length();
    }


    static class MyViewHolder extends RecyclerView.ViewHolder{
        ImageView bookphoto;
        TextView textViewName,textViewAuthor,textViewPublish;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            bookphoto=itemView.findViewById(R.id.bookimg);
            textViewName=itemView.findViewById(R.id.booktitle);
            textViewAuthor=itemView.findViewById(R.id.bookauthor);
            textViewPublish= itemView.findViewById(R.id.bookpublish);
        }
    }
}
View Code

对应书籍章节展示

冲刺二十一天

 

 

showbookactivity

冲刺二十一天
package com.lh.std_everything.ui.home.hometype.studyshare.share;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.lh.std_everything.R;

public class ShowBookActivity extends AppCompatActivity{
    public TextView textViewbookname,textViewbookauthor,textViewbookpublish,textViewbookusername;
    public ImageView bookphoto;
    public LinearLayout lin_tree;
    public Bundle bundle;
    public String arr[]={"begin","one","two","three","four","five","six","seven","eight","nine","ten","others"};
    public String harr[]={"heh","第一章","第二章","第三章","第四章","第五章","第六章","第七章","第八章","第九章","第十章","其他"};
    public Integer iarr[]={0,1,2,3,4,5,6,7,8,9,11};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_book);
        init();

        lin_tree.setOrientation(LinearLayout.VERTICAL);
        lin_tree.removeAllViews();
        if(bundle!=null)
        {
            for(int i=1;i<=11;i++)
            {
                if(bundle.containsKey(arr[i]))
                {
                    String key=bundle.getString(arr[i]);
                    TextView zhangjie=new TextView(this);
                    zhangjie.setText(harr[i]);
                    Log.i("章节展示:", ""+harr[i]);
                    zhangjie.setId(iarr[i]);
                    zhangjie.setTextSize(20);
                    zhangjie.setPadding(10,10,0,0);
                    lin_tree.addView(zhangjie);
                    zhangjie.setOnClickListener(v -> {
                        Bundle b1=new Bundle();
                        b1.putString("photos",key);
                        Intent intent =new Intent(ShowBookActivity.this,ShowBookAnswerActivity.class);
                        intent.putExtras(b1);
                        startActivity(intent);
                    });
                }
            }
        }
    }
    public void init()
    {
        textViewbookauthor=findViewById(R.id.tv_bookauthor);
        textViewbookname=findViewById(R.id.tv_bookname);
        textViewbookpublish=findViewById(R.id.tv_bookpublish);
        textViewbookusername=findViewById(R.id.tv_username);
        bookphoto=findViewById(R.id.bookphoto);
        lin_tree=findViewById(R.id.lin_tree);

        bundle = getIntent().getExtras();
        if(bundle!=null)
        {
            Log.i("接收端的bundle的username", bundle.getString("username"));
            textViewbookusername.setText("上传者:"+bundle.getString("username"));
            textViewbookpublish.setText("出版社:"+bundle.getString("bookpublish"));
            textViewbookname.setText(bundle.getString("bookname"));
            textViewbookauthor.setText("作者:"+bundle.getString("bookauthor"));
            String urls[]=bundle.getString("head").split("\\$");
            RequestOptions options = new RequestOptions()
                    .error(R.drawable.error)
                    .placeholder(R.drawable.loading);
            if(urls.length!=0) {
                for(int i=0;i<urls.length;i++)
                {
                    if(!urls[i].equals("null"))
                    {
                        Log.i("url",urls[i]);
                        Glide.with(this)
                                .load(urls[i])
                                .apply(options)
                                .into(bookphoto);
                        break;
                    }
                }
            }
        }
    }
}
View Code

对应章节答案展示

冲刺二十一天

 

 showbookanswerActivity

冲刺二十一天
package com.lh.std_everything.ui.home.hometype.studyshare.share;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.lh.std_everything.R;
import com.lh.std_everything.ui.home.hometype.adapter.GridViewAdapter.GridViewAdapter;
import com.lh.std_everything.ui.home.hometype.news.shownews.ShowNewsActivity;
import com.lxj.xpopup.XPopup;

public class ShowBookAnswerActivity extends AppCompatActivity {
    public LinearLayout lin_imgs;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_book_answer);
        Bundle bundle = getIntent().getExtras();
        String urls[]=bundle.getString("photos").split("\\$");
        lin_imgs=findViewById(R.id.lin_imgs);
        RequestOptions options = new RequestOptions()
                .error(R.drawable.error)
                .placeholder(R.drawable.loading);
        if(urls.length!=0) {
            for(int i=0;i<urls.length;i++)
            {
                if(!urls[i].equals("null"))
                {
                    ImageView item=new ImageView(this);
                    Log.i("url",urls[i]);
                    Glide.with(this)
                            .load(urls[i])
                            .apply(options)
                            .into(item);
                    int finalI = i;
                    item.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            new XPopup.Builder(ShowBookAnswerActivity.this)
                                    .asImageViewer(item,urls[finalI],new GridViewAdapter.ImageLoader())
                                    .show();
                        }
                    });
                    lin_imgs.addView(item);
                }
            }
        }
    }
}
View Code

 

本次APP开发先告一段落,要开始复习了!!!!!

 

上一篇:Linux常用命令


下一篇:摩斯的解密和加密