页面跳转较为简单,但涉及到ListView列表跳转就要注意如何获取点击的item,并实现相对应的item内容实现。举例三个页面之间的相互跳转,重点在意ListView列表跳转到新的Activity并获取相对应的内容。
先来理清思路:
1.由ListView页面跳转到新的Activity页面;
2.在ListView页面获取点击的item的id值;
3.在新的Activity页面接收id值;
4.实现HTTP请求;
5.JSON解析;
6.列表显示,这里又包含了适配器和实体类的创建
这里直接附上main页面的activity_main.xml代码和预览图
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.lenovo.app15.MainActivity">
<RelativeLayout
android:id="@+id/main_top_bar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@mipmap/rgister_btn_bk"
android:gravity="center_vertical"
android:layout_alignParentTop="true">
<ImageView
android:id="@+id/user"
android:layout_width="40dp"
android:layout_height="40dp"
android:paddingLeft="10dp"
android:src="@mipmap/user" />
<TextView
android:id="@+id/search_tv"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@id/user"
android:background="@mipmap/list_bg_normal"
android:gravity="center_vertical"
android:text="请输入你需要搜索的商品"
android:textSize="17sp" />
<TextView
android:id="@+id/search_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@id/search_tv"
android:text="搜索"
android:textColor="#FFFFFF"
android:textSize="25sp" />
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_toRightOf="@id/search_btn"
android:src="@mipmap/goodsdetail_customer_service_nomal" />
</RelativeLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="@mipmap/guide_1"
android:layout_below="@id/main_top_bar"
android:layout_above="@id/main_center_bar"/>
<LinearLayout
android:id="@+id/main_center_bar"
android:layout_width="match_parent"
android:layout_height="120dp"
android:orientation="horizontal"
android:layout_above="@id/main_bottom_bar">
<ImageButton
android:id="@+id/main_articles_btn"
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight="1"
android:src="@mipmap/articles" />
<ImageButton
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight="1"
android:src="@mipmap/food" />
<ImageButton
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight="1"
android:src="@mipmap/agency" />
</LinearLayout>
<LinearLayout
android:id="@+id/main_bottom_bar"
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="horizontal"
android:background="#7F7F7F"
android:layout_alignParentBottom="true">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1">
<ImageView
android:id="@+id/main"
android:layout_width="60dp"
android:layout_height="40dp"
android:src="@mipmap/main_check"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="主页"
android:textSize="15sp"
android:textColor="#FF0033"
android:layout_below="@id/main"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1">
<ImageView
android:id="@+id/unfinished"
android:layout_width="60dp"
android:layout_height="40dp"
android:src="@mipmap/unfinished"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="未完成"
android:textSize="15sp"
android:textColor="#9C9C9C"
android:layout_below="@id/unfinished"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1">
<ImageView
android:id="@+id/completed"
android:layout_width="60dp"
android:layout_height="40dp"
android:src="@mipmap/completed"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="已完成"
android:textSize="15sp"
android:textColor="#9C9C9C"
android:layout_below="@id/completed"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
预览图
我们先来实现第一步由ListView页面跳转到新的Activity页面。
两个不同的Activity之间跳转使用setOnItemClickListener()方法跳转,附上代码
定义对象
private ListView listView;
private List<Articles> articlesList = new ArrayList<>();
由于ListView页面使用的是ListView点击跳转,所以不能够直接使用setOnClickListener()点击事件
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(ArticlesActivity.this,TiaoliaoActivity.class);
int title = articlesList.get(i).getClassifyId();
//这里的get(i)的i值就是点击ListView所相对应的类目的值
}
});
第二步则是在这个ListView页面获取点击的item的id值,id值则是get(i)内的参数,在点击事件内添加Intent传值就可以了
intent.putExtra("titleId",title);
startActivity(intent);
点击的item的id值传给了新的Activity也就是TiaoliaoActivity,这里就要接收传过来的ID
Intent intent = getIntent();
int titleId = intent.getIntExtra("titleId", 0);//将传过来的值给了titleId 这个对象
到这就能够判定你点击的是ListView的哪一个类目,然后需要根据这个id拼接一个动态的API来实现相对应的网络数据,这里给出代码:
API = "http://103.244.59.105:8014/paopaoserver/categorylist?params={\"classify_id\":" + titleId + ",\"page\":1,\"page_count\":10}";
既然得到了API,就要进行HTTP网络请求来得到一串JSON字符串,写一个内部类继承AsyncTask
class MyTask extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... strings) {
StringBuffer stringBuffer = new StringBuffer();
try {
URL url = new URL(strings[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
if (connection.getResponseCode() != 200) {
return "network failed";
}
InputStream inputStream = connection.getInputStream();
InputStreamReader reader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(reader);
String temp = "";
while ((temp=bufferedReader.readLine() )!= null) {
stringBuffer.append(temp);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return stringBuffer.toString();//返回JSON字符串
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
}
}
得到JSON字符串后,执行onPostExecute()方法,首先需要对onProgressUpdate(0方法返回的字符串进行判定
if (s.equals("network failed")) {
Toast.makeText(TiaoliaoActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
} else {
//进行JSON解析
}
开始JSON解析之前,做一下准备工作,创建一个实体类Tiaoliao,java,一个行布局Tiaoliao.xml和一个适配器TiaoliaoAdapter.java,附上代码
Tiaoliao,java
public class Tiaoliao {
private String category_name;
private int city_id;
private int classify_id;
private String classify_name;
private int counties_id;
private String counties_name;
private double nowprice;
private double oldprice;
private int page;
private int page_count;
private int product_id;
private String product_name;
private String small_pic;
public Tiaoliao(String category_name, int city_id, int classify_id, String classify_name, int counties_id, String counties_name, double nowprice, double oldprice, int page, int page_count, int product_id, String product_name, String small_pic) {
this.category_name = category_name;
this.city_id = city_id;
this.classify_id = classify_id;
this.classify_name = classify_name;
this.counties_id = counties_id;
this.counties_name = counties_name;
this.nowprice = nowprice;
this.oldprice = oldprice;
this.page = page;
this.page_count = page_count;
this.product_id = product_id;
this.product_name = product_name;
this.small_pic = small_pic;
}
public String getCategory_name() {
return category_name;
}
public void setCategory_name(String category_name) {
this.category_name = category_name;
}
public int getCity_id() {
return city_id;
}
public void setCity_id(int city_id) {
this.city_id = city_id;
}
public int getClassify_id() {
return classify_id;
}
public void setClassify_id(int classify_id) {
this.classify_id = classify_id;
}
public String getClassify_name() {
return classify_name;
}
public void setClassify_name(String classify_name) {
this.classify_name = classify_name;
}
public int getCounties_id() {
return counties_id;
}
public void setCounties_id(int counties_id) {
this.counties_id = counties_id;
}
public String getCounties_name() {
return counties_name;
}
public void setCounties_name(String counties_name) {
this.counties_name = counties_name;
}
public double getNowprice() {
return nowprice;
}
public void setNowprice(double nowprice) {
this.nowprice = nowprice;
}
public double getOldprice() {
return oldprice;
}
public void setOldprice(double oldprice) {
this.oldprice = oldprice;
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public int getPage_count() {
return page_count;
}
public void setPage_count(int page_count) {
this.page_count = page_count;
}
public int getProduct_id() {
return product_id;
}
public void setProduct_id(int product_id) {
this.product_id = product_id;
}
public String getProduct_name() {
return product_name;
}
public void setProduct_name(String product_name) {
this.product_name = product_name;
}
public String getSmall_pic() {
return small_pic;
}
public void setSmall_pic(String small_pic) {
this.small_pic = small_pic;
}
}
行布局Tiaoliao_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="130dp">
<ImageView
android:id="@+id/tiaoliao_image"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:src="@mipmap/ic_launcher" />
<RelativeLayout
android:id="@+id/tiaoliao_center_bar"
android:layout_width="190dp"
android:layout_height="match_parent"
android:layout_toRightOf="@id/tiaoliao_image"
android:padding="10dp">
<TextView
android:id="@+id/tiaoliao_title_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="标题"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="@+id/tiaoliao_type_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tiaoliao_title_tv"
android:layout_toRightOf="@id/tiaoliao_image"
android:text="日常用品-调料干货"
android:textSize="15sp"
tools:ignore="NotSibling" />
<LinearLayout
android:id="@+id/tiaoliao_add_tv"
android:layout_width="70dp"
android:layout_height="30dp"
android:layout_below="@id/tiaoliao_type_tv"
android:layout_toRightOf="@id/tiaoliao_image"
android:gravity="center_vertical"
tools:ignore="NotSibling">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@mipmap/lessen" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1"
android:textSize="16dp" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@mipmap/add" />
</LinearLayout>
<TextView
android:id="@+id/tiaoliao_price_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tiaoliao_add_tv"
android:text="价格"
android:textColor="#FF0033"
android:textSize="20sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="@id/tiaoliao_center_bar"
android:padding="10dp" >
<Button
android:layout_width="match_parent"
android:layout_height="30dp"
android:text="购买"
android:background="@color/colorAccent"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
适配器TiaoliaoAdapter.java
public class TiaoliaoAdapter extends BaseAdapter {
private Context context;
private List<Tiaoliao> tiaoliaoList;
public TiaoliaoAdapter(Context context, List<Tiaoliao> tiaoliaoList) {
this.context = context;
this.tiaoliaoList = tiaoliaoList;
}
@Override
public int getCount() {
return tiaoliaoList.size();
}
@Override
public Object getItem(int i) {
return tiaoliaoList.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View v = LayoutInflater.from(context).inflate(R.layout.tiaoliao_item, null);
TextView titleTV = v.findViewById(R.id.tiaoliao_title_tv);
TextView typeTV = v.findViewById(R.id.tiaoliao_type_tv);
TextView priceTV = v.findViewById(R.id.tiaoliao_price_tv);
Tiaoliao tiaoliao = tiaoliaoList.get(i);
titleTV.setText(tiaoliao.getProduct_name());
typeTV.setText(tiaoliao.getCategory_name()+"-"+tiaoliao.getClassify_name());
priceTV.setText(tiaoliao.getNowprice()+"");
return v;
}
}
到这里就开始JSON解析的步骤了
JSONObject object = null;
try {
Log.e("sfsvd", "doInBackground: 线程"+s);
object = new JSONObject(s);
JSONArray array = object.getJSONArray("datas");
for (int i = 0; i < array.length(); i++) {
String category_name = array.getJSONObject(i).getString("category_name");
int city_id = array.getJSONObject(i).getInt("city_id");
int classify_id = array.getJSONObject(i).getInt("classify_id");
String classify_name = array.getJSONObject(i).getString("classify_name");
int counties_id = array.getJSONObject(i).getInt("counties_id");
String counties_name = array.getJSONObject(i).getString("counties_name");
int nowprice = array.getJSONObject(i).getInt("nowprice");
double oldprice = array.getJSONObject(i).getDouble("oldprice");
int page = array.getJSONObject(i).getInt("page");
int page_count = array.getJSONObject(i).getInt("page_count");
int product_id = array.getJSONObject(i).getInt("product_id");
String product_name = array.getJSONObject(i).getString("product_name");
String small_pic = array.getJSONObject(i).getString("small_pic");
Tiaoliao tiaoliao = new Tiaoliao( category_name, city_id, classify_id, classify_name, counties_id, counties_name, nowprice, oldprice, page, page_count, product_id, product_name, small_pic);
tiaoliaoList.add(tiaoliao);//列表填充数据
}
} catch (JSONException e) {
e.printStackTrace();
}
这里需要注意一点,绑定适配器的步骤必须要放在onPostExecute()方法中,因为如果放在onCreate()方法中,就会造成子线程的HTTP网络请求过程与适配器填充数据同时进行,无法判定HTTP网络请求合适结束,也就不能正常的对适配器进行数据填充,就会报错。解决方法就是将适配器和列表填充JSON数据同样放在onPostExecute()方法中,等onProgressUpdate()执行完HTTP网络请求后,由onPostExecute()方法来完成适配器的填充,这样就不会报错。
//刷新ListView
adapter = new TiaoliaoAdapter(TiaoliaoActivity.this, tiaoliaoList);
listView.setAdapter(adapter);
到这里就可以完成ListView页面跳转至新页面了(图片加载除外)
下面单独写图片加载部分,这一部分都在适配器TiaoliaoAdapter.java中完成
首先弄明白下载拥有什么,需要达到的目的是什么。已有图片的URL地址,需要将图片显示出来,所以需要根据URL地址来使用HTTP网络请求,得到一个Bitmap类型对象,将这个对象赋值给ListView列表中的ImageView对象来更新UI。附上完整代码
public class TiaoliaoAdapter extends BaseAdapter {
private Context context;
private List<Tiaoliao> tiaoliaoList;
private Bitmap bitmap;
private ImageView imageView;
public TiaoliaoAdapter(Context context, List<Tiaoliao> tiaoliaoList) {
this.context = context;
this.tiaoliaoList = tiaoliaoList;
}
@Override
public int getCount() {
return tiaoliaoList.size();
}
@Override
public Object getItem(int i) {
return tiaoliaoList.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View v = LayoutInflater.from(context).inflate(R.layout.tiaoliao_item, null);
TextView titleTV = v.findViewById(R.id.tiaoliao_title_tv);
TextView typeTV = v.findViewById(R.id.tiaoliao_type_tv);
TextView priceTV = v.findViewById(R.id.tiaoliao_price_tv);
imageView = v.findViewById(R.id.tiaoliao_image);//定义全局变量imageView,绑定ID
Tiaoliao tiaoliao = tiaoliaoList.get(i);
titleTV.setText(tiaoliao.getProduct_name());
typeTV.setText(tiaoliao.getCategory_name()+"-"+tiaoliao.getClassify_name());
priceTV.setText(tiaoliao.getNowprice()+"");
String img_url = "http://103.244.59.105:8014/paopaoserver"+tiaoliao.getSmall_pic();//拼接图片的网络地址
new ImgTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,img_url);//并行启动AsyncTask,传入地址
return v;
}
//已有图片的URL地址,需要将图片显示出来
class ImgTask extends AsyncTask<String,String,Integer>{
//HTTP网络请求
@Override
protected Integer doInBackground(String... strings) {
try {
URL url = new URL(strings[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream inputStream = null;
if (connection.getResponseCode()!=200){
return -1;
}
inputStream = connection.getInputStream();
//网络请求得到的输入流转换成Bitmap类型的对象,定义全局变量
bitmap = BitmapFactory.decodeStream(inputStream);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Integer integer) {
super.onPostExecute(integer);
//更新主线程
imageView.setImageBitmap(bitmap);
}
}
}
声明两个全局变量
private Bitmap bitmap;
private ImageView imageView;
由于图片的网络地址不完整,所以需要拼接图片的网络地址,赋值给img_url 对象
String img_url = "http://103.244.59.105:8014/paopaoserver"+tiaoliao.getSmall_pic();
并行启动AsyncTask,传入地址
new ImgTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,img_url);
HTTP网络请求
@Override
protected Integer doInBackground(String... strings) {
try {
URL url = new URL(strings[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream inputStream = null;
if (connection.getResponseCode()!=200){
return -1;
}
inputStream = connection.getInputStream();
//网络请求得到的输入流转换成Bitmap类型的对象,定义全局变量
bitmap = BitmapFactory.decodeStream(inputStream);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Integer integer) {
super.onPostExecute(integer);
//更新主线程
imageView.setImageBitmap(bitmap);
}