首页 > 其他分享 >团队开发冲刺第八天(实现评论功能)

团队开发冲刺第八天(实现评论功能)

时间:2022-11-29 14:39:04浏览次数:30  
标签:comment getString 第八天 冲刺 new msg import android 团队

团队开发冲刺第八天(实现评论功能)_json

 

 

昨天在设计评论的UI,以及查阅网址看看如何添加评论

今天完成了评论的功能

遇到的问题:多线程并发的问题,导致第一次输出评论没有及时的显示在屏幕上,之后在进入该页面时,之前的数据会少一条

明天解决这个bug

明天要完成,点赞,收藏,关注等功能。

shownewsActivity.java

package com.example.newbsh.UI.home.hometype.news.shownews;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.newbsh.HttpUtil.HttpUtil;
import com.example.newbsh.MainActivity;
import com.example.newbsh.R;
import com.example.newbsh.UI.home.hometype.news.shownews.comment.BlogComment;

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

import java.lang.ref.WeakReference;
import java.sql.Driver;

public class ShowNewsActivity extends AppCompatActivity {
TextView textViewtitle,textViewdoc,textViewdate;
LinearLayout piclinearLayout,commentLayout;
TextView msg;
Button send;
private String title;
private String username= MainActivity.getUserName();
private ShowBlogHandler blogHandler;
String url;
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_news);
blogHandler = new ShowBlogHandler(this);
init();
showComment();
}



private void init() {
textViewtitle = findViewById(R.id.NewsTitle);
textViewdoc = findViewById(R.id.Newsdoc);
textViewdate = findViewById(R.id.Newsdate);
piclinearLayout=findViewById(R.id.picturelinearlayout);
commentLayout=findViewById(R.id.comment_layout);
textViewdoc.setMovementMethod(new ScrollingMovementMethod());
msg=findViewById(R.id.msg);
send=findViewById(R.id.send);
initdoc();
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String comment=msg.getText().toString();
if(comment.isEmpty()){
Toast.makeText(ShowNewsActivity.this, "发送内容不能为空", Toast.LENGTH_SHORT).show();
return;
}
writeComment(comment); //开启线程
}
});

}
private void writeComment(String comment)
{
new Thread(new Runnable() {
@Override
public void run() {
String reqdata="http://39.97.181.86/BaiXiaoSheng/sendnewscomment?username="+username+"&comment="+comment+"&title="+title;
String json=String.valueOf(HttpUtil.sendPost(reqdata,""));
Message msg=new Message();
msg.what=1000;
msg.obj=json;
blogHandler.sendMessage(msg);
Log.d("writecomment",json);
}
}).start();
}
private void showComment() {
new Thread(new Runnable() {
@Override
public void run() {
Log.d("data", "评论");
String requrl="http://39.97.181.86/BaiXiaoSheng/findallcomments?title="+title;
String json = String.valueOf(HttpUtil.sendPost(requrl, ""));
Message msg = new Message();
msg.what = 600;
msg.obj = json;
blogHandler.sendMessage(msg);
Log.d("评论", json);
}
}).start();
}

/*
加载新闻文章
*/
private void initdoc() {
Bundle bundle = getIntent().getExtras();
/*
获取图片的链接
*/
url=bundle.getString("url");
String []urls=url.split(" ");

/*
设置文章的内容
*/
if (bundle != null) {
title=bundle.getString("title");
textViewtitle.setText(bundle.getString("title"));
textViewdoc.setText(bundle.getString("doc"));
textViewdate.setText("文章发布时间为:"+bundle.getString("date"));
} else {
Log.i("bundle", "为空。。。。。。");
}
/*
动态加载图片
*/

RequestOptions options = new RequestOptions()
.error(R.drawable.error)
.placeholder(R.drawable.loading);
for(int i=0;i<urls.length;i++)
{
ImageView imageView=new ImageView(this);
Glide.with(this)
.load(urls[i])
.apply(options)
.into(imageView);
LinearLayout.LayoutParams layout = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layout.setMargins(0, 10, 0, 0);
imageView.setLayoutParams(layout);
piclinearLayout.addView(imageView);
}
}

static class ShowBlogHandler extends Handler { //防止内存泄漏
private final ShowNewsActivity mcontext;

ShowBlogHandler(ShowNewsActivity activity) {
WeakReference<ShowNewsActivity> weakReference = new WeakReference<>(activity);
mcontext = weakReference.get();
}

@SuppressLint("SetTextI18n")
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);

switch (msg.what) {

case 600:
try {
JSONArray comments = new JSONArray(msg.obj.toString());
Log.i("length:", ""+comments.length());
for (int i = 0; i < comments.length(); i++) {
JSONObject object = comments.getJSONObject(i);
BlogComment blogComment = new BlogComment(mcontext);
if (!object.isNull("imgurl")) {
Glide.with(mcontext)
.load(object.getString("imgurl"))
.circleCrop()
.into(blogComment.getImageView());
}
blogComment.getName().setText(object.getString("username"));
blogComment.getComment().setText(object.getString("comment"));
blogComment.getDate().setText(object.getString("date"));
Log.i("次数:", ""+object.getString("comment"));
mcontext.commentLayout.addView(blogComment,0);

}
} catch (JSONException e) {
e.printStackTrace();
}
break;

case 1000:
try {
JSONObject object=new JSONObject(msg.obj.toString());
BlogComment blogComment=new BlogComment(mcontext);
blogComment.getDate().setText(object.getString("date"));
blogComment.getComment().setText(object.getString("comment"));
blogComment.getName().setText(object.getString("username"));
if(!object.isNull("imgurl")){
if(object.getString("imgurl").length()>0)
Glide.with(mcontext)
.load(object.getString("imgurl"))
.circleCrop()
.into(blogComment.getImageView());
}
mcontext.commentLayout.addView(blogComment,0);
mcontext.msg.setText("");
mcontext.msg.clearFocus();
InputMethodManager imm= (InputMethodManager) mcontext.getSystemService(Context.INPUT_METHOD_SERVICE); //键盘收缩
if (imm != null) {
imm.hideSoftInputFromWindow(mcontext.msg.getWindowToken(),0);
}
Toast toast = Toast.makeText(mcontext, "评论成功", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
} catch (JSONException e) {
e.printStackTrace();
}
break;
default:
break;
}
}
}

}

View Code

Blogcomment.java

package com.example.newbsh.UI.home.hometype.news.shownews.comment;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.example.newbsh.R;

public class BlogComment extends ConstraintLayout {
private ImageView imageView;
private TextView name;
private TextView date;
private TextView comment;
public BlogComment(Context context) {
super(context);
LayoutInflater inflater= LayoutInflater.from(context);
View view=inflater.inflate(R.layout.shownews_comment_layout,this);
imageView=view.findViewById(R.id.comment_userimg);
name=view.findViewById(R.id.comment_name);
date=view.findViewById(R.id.comment_date);
comment=view.findViewById(R.id.comment_comment);
}

public ImageView getImageView() {
return imageView;
}

public TextView getName() {
return name;
}

public TextView getDate() {
return date;
}

public TextView getComment() {
return comment;
}
}

View Code

 



标签:comment,getString,第八天,冲刺,new,msg,import,android,团队
From: https://blog.51cto.com/u_14824425/5894959

相关文章

  • 团队冲刺开发第二天
    今天花费将近3个多小时的时间,将以前的Android知识回顾了一遍,同时自己针对自己的任务做了一个小Demo。主要用到的是ViewModel来存储页面数据,Respositry仓库来进行数据的增删......
  • .NET团队送给.NET开发人员的云原生学习资源
    企业正在迅速采用云的功能来满足用户需求,提高应用程序的可伸缩性和可用性。要完全拥抱云并优化节约成本,就需要在设计应用程序时考虑到云的环境,也就是要用云原生的应用开发方......
  • 首发:徐亦达团队新论文推荐(ECCV2020):端到端多人多视角3d动态匹配网络
    徐亦达团队在ECCV2020上发表了一篇机器学习论文End-to-endDynamicMatchingNetworkforMulti-viewMulti-person3dPoseEstimation(端到端多人多视角3d动态匹配网络)论文......
  • 【首发】徐亦达团队新论文推荐:模限界矩阵分解
    徐亦达团队在IEEETransactionsonKnowledgeandDataEngineering的发表了一篇机器学习论文MagnitudeBoundedMatrixFactorisationforRecommenderSystems(模限界矩阵......
  • 从康威定律看团队架构
    康威定律是马尔文·康威1967提出的:“设计系统的架构受制于产生这些设计的组织的沟通结构。”中文直译的意思是:设计系统的组织,其产生的设计等同于组织之内、组织之间的沟通......
  • [D0g3] 第五届安洵杯wp - HashRun安全团队
    第五届安洵杯WriteUp-HashRun安全团队前言HashRun安全团队最终排名56。Crypto@S1gMa知识点\(sha256\)掩码爆破。解题过程\(step1\):先\(nc\)链接获取密文,然后......
  • 用例技术(四)——用例编写工作过程和大型团队中用例收集的建议
     一.实际编写用例,每个开发组都应该形成并制定一套工作习惯,一种好的方式是由开发组和个人分工合作,因为组比个人有两个优点,一是便于集中讨论,二是便于对于问题达成共......
  • 带领团队
    团队一个有口才的人对着一群有耳朵的人讲话,这就是最基本的团队。 言归正传。将带领团队的一些想法记录下来,与君探讨。长夜漫漫,我有故事,你有酒。一、如何提升项目交付......
  • 跨团队协作(7)
    跨团队协作案例:CodeWang-NC想让团队外的CodeWang-QL一起协作git-csdn项目整体思路:CodeWang-QL通过CodeWang-NC远程仓库链接中叉入需要协作的项目带自己的仓库中(默......
  • Git团队协作机制(5)
    Git团队协作机制团队内合作跨团队协作......