记性真的不行啊...贴上来有时间多复习复习

主活动

package com.example.com.webtext;

import android.content.Intent;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast; import com.bumptech.glide.Glide; import org.json.JSONArray;
import org.json.JSONObject; import java.util.ArrayList;
import java.util.List; public class MainActivity extends AppCompatActivity {
private List<massage > Mymassage=new ArrayList<>() ;
private List<massage > Mymassage1=new ArrayList<>() ;
private SwipeRefreshLayout swipeRefresh;
TextView nameText;
TextView summaryText;
ImageView img;
String adress;
adapter massageAdapter;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu .toolbar ,menu ) ;
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item .getItemId() ){
case R.id.backup :
Toast.makeText(this,"You clicked Backup",Toast .LENGTH_SHORT ).show() ;
break;
case R.id.delete :
Toast.makeText(this,"You clicked Delete",Toast .LENGTH_SHORT ).show() ;
break;
case R.id.settings :
Toast.makeText(this,"You clicked Settings",Toast .LENGTH_SHORT ).show() ;
break;
default :
}
return true;
}
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar =(Toolbar) findViewById(R.id.toolbar) ;
setSupportActionBar(toolbar);
adress ="https://qcloud.waydrow.com/LoveInn/index.php/Home/App/getInfoList";
nameText =(TextView) findViewById(R.id.name ) ;
summaryText =(TextView) findViewById(R.id.summery);
img=(ImageView) findViewById(R.id.photo) ;
RecyclerView recyclerView =(RecyclerView ) findViewById(R.id.recycler_view);
StaggeredGridLayoutManager LayoutManager=new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL) ;
recyclerView .setLayoutManager(LayoutManager );
massageAdapter =new adapter(Mymassage );
recyclerView .setAdapter(massageAdapter ) ;
getHTTP() ;
/*massageAdapter.setOnItemClickListener(new adapter.OnItemClickListener(){
@Override
public void onItemClick(View view, int position) {
massage massage =Mymassage .get(position );
Intent intent =new Intent(MainActivity .this,DetailMassage.class );
intent .putExtra("id",massage.getId()) ;
MainActivity .this.startActivity(intent);
Toast.makeText(MainActivity.this, "dfghjk",Toast .LENGTH_SHORT ).show();
}
});*/
swipeRefresh=(SwipeRefreshLayout) findViewById(R.id.swipe_refresh );
swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(750);
} catch (InterruptedException e) {
e.printStackTrace();
}
runOnUiThread(new Runnable() {
@Override
public void run() {
getHTTP1() ;
swipeRefresh.setRefreshing(false);
}
});
}
}).start();
}
});
}
public void getHTTP(){
HttpUtil.sendHttpRequest(adress ,new HttpCallbackListener(){
@Override
public void onFinish(String response){
parseJSONWithJSONObject(response );
}
@Override
public void onError(Exception e){
e.printStackTrace();
Toast .makeText(MainActivity .this,"There is no detial!",Toast .LENGTH_LONG).show() ;
}
});
} public void getHTTP1(){
HttpUtil.sendHttpRequest(adress ,new HttpCallbackListener(){
@Override
public void onFinish(String response){
parseJSONWithJSONObject1(response );
runOnUiThread(new Runnable() {
@Override
public void run() {
massageAdapter.notifyDataSetChanged();
}
}) ;
}
@Override
public void onError(Exception e){
e.printStackTrace();
Toast .makeText(MainActivity .this,"There is no detial!",Toast .LENGTH_SHORT ).show() ;
}
}); }
private void parseJSONWithJSONObject1(final String jsonData){
try{
JSONArray jsonArray =new JSONArray(jsonData ) ;
for(int i=0;i<jsonArray .length() ;i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String name = jsonObject.getString("name");
String summary = jsonObject.getString("summary");
String imgUrl = "https://qcloud.waydrow.com" + jsonObject.getString("photo");
String id=jsonObject .getString("id") ;
massage massage1 = new massage(name, summary, imgUrl,id);
Mymassage .add(massage1 ) ;
}
Mymassage1 =Mymassage ;
}
catch (Exception e){
e.printStackTrace() ;
}
}
private void parseJSONWithJSONObject(final String jsonData){
try{
JSONArray jsonArray =new JSONArray(jsonData ) ;
for(int i=0;i<jsonArray .length() ;i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String name = jsonObject.getString("name");
String summary = jsonObject.getString("summary");
String imgUrl = "https://qcloud.waydrow.com" + jsonObject.getString("photo");
String id=jsonObject .getString("id") ;
massage massage1 = new massage(name, summary, imgUrl,id);
Mymassage .add(massage1 ) ;
}
setUI();
}
catch (Exception e){
e.printStackTrace() ;
}
}
public void setUI(){
runOnUiThread(new Runnable() {
@Override
public void run() {
setRecyclerview();
}
}) ;
}
public void setRecyclerview(){
RecyclerView recyclerView =(RecyclerView ) findViewById(R.id.recycler_view);
StaggeredGridLayoutManager LayoutManager=new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL) ;
recyclerView .setLayoutManager(LayoutManager );
if(Mymassage .size() ==0){
Toast .makeText(MainActivity .this,"There is no detial!",Toast .LENGTH_SHORT ).show() ;
String summary = "summary";
String imgUrl = " " ;
massage massage1 = new massage("", summary, imgUrl,"0");
Mymassage.add(massage1 );
massageAdapter = new adapter(Mymassage);
recyclerView.setAdapter(massageAdapter);
Mymassage =new ArrayList<>();
}
else {
massageAdapter = new adapter(Mymassage);
recyclerView.setAdapter(massageAdapter);
}
runOnUiThread(new Runnable() {
@Override
public void run() {
massageAdapter.setOnItemClickListener(new adapter.OnItemClickListener(){
@Override
public void onItemClick(View view, int position) {
massage massage1 =Mymassage1 .get(position );
Intent intent =new Intent(MainActivity .this,DetailMassage.class );
intent .putExtra("id",massage1.getId()) ;
MainActivity .this.startActivity(intent);
}
});
}
});
Mymassage1 =Mymassage ;
Mymassage =new ArrayList<>();
}
}

adapter,里面定义的接口很重要

package com.example.com.webtext;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView; import com.bumptech.glide.Glide; import java.util.ArrayList;
import java.util.List; /**
* Created by 定不负相思懿 on 2017/5/11.
*/ public class adapter extends RecyclerView.Adapter<adapter .ViewHolder >implements View.OnClickListener{
private List<massage> mymassage=new ArrayList<>();
public adapter (List<massage> fruit3){
mymassage=fruit3;
}
private OnItemClickListener mOnItemClickListener = null;
public void setOnItemClickListener(OnItemClickListener listener) {
this.mOnItemClickListener = listener;
}
public static interface OnItemClickListener {
void onItemClick(View view , int position);
} static class ViewHolder extends RecyclerView.ViewHolder {
ImageView image;
TextView name;
TextView summary;
View fruitview;
Context context1;
public ViewHolder(View view) {
super(view);
fruitview =view;
image=(ImageView ) view.findViewById(R.id.photo) ;
name =(TextView ) view.findViewById(R.id.name) ;
summary =(TextView ) view.findViewById(R.id.summery) ;
}
} @Override
public ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent .getContext() ).inflate(R.layout.recyclerview_layout,parent ,false ) ;
ViewHolder holder=new ViewHolder(view );
holder.context1=parent .getContext() ;
view.setOnClickListener(this);
/* holder.fruitview.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
int position=holder.getAdapterPosition() ;
massage massage =mymassage .get(position );
Intent intent =new Intent(parent .getContext(),DetailMassage.class );
intent .putExtra("id",massage.getId()) ;
parent.getContext().startActivity(intent);
}
}) ;*/
return holder ;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
massage mymassage1=mymassage.get(position );
holder.itemView.setTag(position);
holder .name.setText(mymassage1 .getname() ) ;
holder .summary .setText(mymassage1 .getsummary() ) ;
Glide.with(holder .context1).load(mymassage1.getphoto()).into(holder.image );
}
@Override
public int getItemCount() {
return mymassage .size() ;
}
@Override
public void onClick(View v) {
if (this.mOnItemClickListener != null) {
this.mOnItemClickListener.onItemClick(v,(int)v.getTag() );
}
}
}

post方法,还是用比较简单的字符串拼接吧(这个和刚才的不是同一个程序,是一个用户名和密码发送给服务器的登录)

package com.example.com.webtext;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder; /**
* Created by 定不负相思懿 on 2017/5/17.
*/ public class HttpPost {
public static void sendHttpRequest(final String adress, final String name, final String password, final InterfacePost linsener) {
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection connection =null ;
try {
URL url=new URL(adress);
String a=url.toString() ;
connection =(HttpURLConnection ) url.openConnection() ;
connection .setRequestMethod("POST");
connection .setConnectTimeout(8000);
connection .setReadTimeout(8000);
connection .setDoInput(true);
connection .setDoOutput(true);
String requestStringBytes="username="+name +"&password="+password;
OutputStream outputStream = connection.getOutputStream();
outputStream.write(requestStringBytes.getBytes());
outputStream.close();
InputStream in=connection.getInputStream();
BufferedReader reader =new BufferedReader(new InputStreamReader(in)) ;
StringBuilder response=new StringBuilder();
String line;
while((line=reader .readLine()) !=null){
response .append(line) ;
}
if(linsener !=null) {
linsener.onFinish(response.toString());
}
} catch (Exception e) {
linsener.onError(e);
} finally{
if(connection!=null ){
connection .disconnect();
}
}
}
}).start();
}
}

recyclerview 主活动里监听点击事件的更多相关文章

  1. IOS之UI--自定义按钮实现代理监听点击事件

    前言: Objective-C提供的按钮监听事件的方法是 不含参数的监听方法 [button实例对象 addTarget:self action:@selector(func) forControlE ...

  2. Android监听点击事件实现的三种方法

    监听点击事件实现的三种方法:1.匿名内部类2.外部类3.直接实现接口 1.匿名内部类: package com.jereh.calculator; import android.content.Con ...

  3. swift项目第六天:中间发布按钮的封装以及监听点击事件

    import UIKit /* 总结:1:给UIButton写分类,新建文件swiftFile,一般为了区分起名字都是名字-Extension,要想调用UI控件需要导入 import UIKit框架, ...

  4. Flex 自动获取焦点 监听全局键盘事件

    在mxml里监听addedToStage事件 protected function application1_addedToStageHandler(event:Event):void { this. ...

  5. [置顶] flex4事件监听与自定义事件分发(三)

    1.我们来说一下自定义事件子类.什么时候创建新的事件类以及怎么创建. (1)说一下,我们什么时候需要自定义事件类.举例说明,在flex中事件的基类是Event,而当我们点击某个按钮或者是单击某个组件的 ...

  6. Python——pyHook监听鼠标键盘事件

    pyHook包为Windows中的全局鼠标和键盘事件提供回调. 底层C库报告的信息包括事件的时间,事件发生的窗口名称,事件的值,任何键盘修饰符等. 而正常工作需要pythoncom等操作系统的API的 ...

  7. 三种方式监听NGUI的事件方法

    NGUI研究院之三种方式监听NGUI的事件方法(七) NGUI事件的种类很多,比如点击.双击.拖动.滑动等等,他们处理事件的原理几乎万全一样,本文只用按钮来举例. 1.直接监听事件 把下面脚本直接绑定 ...

  8. javascript 原生方法监听DOM结构改变事件

    js原生方法监听DOM结构改变事件 document.addEventListener('DOMNodeInserted',function(){alert(1)},false);document.a ...

  9. WPF之监听组合键事件

    private void KeyDown(object sender, KeyEventArgs e) { if (e.Key== Key.Up && Keyboard.Modifie ...

随机推荐

  1. PAT (Basic Level) Practice (中文)1002 写出这个数 (20 分)

    题目链接:https://pintia.cn/problem-sets/994805260223102976/problems/994805324509200384 #include <iost ...

  2. Leetcode 树(102, 637)

    637: 二叉树的层平均值 给定一个非空二叉树,返回一个由每层节点平均值组成的数组: https://leetcode-cn.com/problems/average-of-levels-in-bin ...

  3. hashlib hmac configparser subprocess xlrd xlwt

    hashlib模块:加密 import hashlib # 基本使用 cipher = hashlib.md5('需要加密的数据的二进制形式'.encode('utf-8')) print(ciphe ...

  4. Parhaps you are running on a JRE rather than a JDK?

    maven项目启动时报错 解决方案: 第一步:在启动项目上右击 第二步:修改JRE为JDK,双击划线部分 第三步:如果没有配置JDK,进行以下操作 第四步:从本地添加JDK 第五步:应用JDK 选择好 ...

  5. SSM-网站前台博客系统制作(1)---前台+Google的Kaptcha

    前提: 1天半时间简单自己手写了一下前端布局和后台验证码的基本工作,简要说明一下遇到的问题和收获吧. 这次基本就是前台设计(首页)+Kaptcha图片验证码(之前弄了一个reCaptcha验证码 但是 ...

  6. 复旦大学2018--2019学年第一学期高等代数I期末考试情况分析

    一.期末考试成绩90分以上的同学(共21人) 周烁星(99).封清(99).叶雨阳(97).周子翔(96).王捷翔(96).张思哲(95).丁思成(94).陈宇杰(94).谢永乐(93).张哲维(93 ...

  7. 复旦高等代数 I(18级)每周一题

    [问题2018A01]  计算下列 $n+1$ 阶行列式的值: $$|A|=\begin{vmatrix} 0 & 1 & 1 & \cdots & 1 \\ 1 &a ...

  8. Session &cookie introduction,usage

    Cookie 1)什么是Cookie?      服务器为了识别用户身份而临时存放在浏览器端的少量数据.     2)工作原理         浏览器访问服务器时,服务器将一些数据以set-cooki ...

  9. list set map区别及适用场景

    list与Set.Map区别及适用场景   1.List,Set都是继承自Collection接口,Map则不是 2.List特点:元素有放入顺序,元素可重复 ,Set特点:元素无放入顺序,元素不可重 ...

  10. 性能测试监控工具nmon安装及使用方法

    1.概述 监控,在检查系统问题或优化系统性能工作上是一个不可缺少的部分.通过操作系统监控工具监视操作系统资源的使用情况,间接地反映了各服务器程序的运行情况.根据运行结果分析可以帮助我们快速定位系统问题 ...