跑跑APP的第一步
前提是把所有的图片先拷到mipmap文件下。
第一步就是先创建一个新的RechangActivity和RRActivity,和其匹配的XML文件。底下是XML文件的代码。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context="myapplication.com.example.json.RechangActivity">
<RelativeLayout
android:id="@+id/shouye_lay"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#F33407">
<ImageView
android:id="@+id/t1"
android:layout_width="60dp"
android:layout_height="60dp"
android:paddingLeft="10dp"
android:src="@mipmap/user" />
<TextView
android:id="@+id/t3"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginRight="10dp"
android:layout_toRightOf="@id/t1"
android:gravity="center"
android:text="搜索"
android:textColor="#FFFFFF"
android:textSize="30sp" />
<ImageView
android:id="@+id/t4"
android:layout_width="50dp"
android:layout_height="60dp"
android:layout_toRightOf="@id/t3"
android:gravity="top"
android:paddingLeft="10dp"
android:src="@mipmap/goodsdetail_customer_service_nomal" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="280dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/cai" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:padding="5dp">
<ImageView
android:id="@+id/richang_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="centerInside"
android:src="@mipmap/articles" />
<ImageView
android:id="@+id/tu2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@mipmap/food" />
<ImageView
android:id="@+id/tu3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@mipmap/agency" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#000000"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/main_check" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="主页"
android:textColor="#E22319" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@mipmap/unfinished" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#6A6A6A"
android:text="未完成" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@mipmap/completed" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#6A6A6A"
android:text="已完成" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout 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"
android:orientation="vertical"
tools:context="myapplication.com.example.json.RRActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#F33407"
android:orientation="horizontal">
<ImageView
android:id="@+id/img"
android:layout_width="50dp"
android:layout_height="40dp"
android:layout_marginTop="5dp"
android:paddingLeft="10dp"
android:src="@mipmap/back_btn_white" />
<TextView
android:id="@+id/re_tv"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="5dp"
android:gravity="center"
android:paddingLeft="5dp"
android:text="日常用品"
android:textColor="#E8DFD1"
android:textSize="30sp" />
</LinearLayout>
<ListView
android:id="@+id/rr_listview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
第二步接下来就是在两个页面上绑定id和添加监听事件。因为我们要从第一个页面跳到第二个页面,所以说我们需要用intent来跳转传值,拿到相应的id。同样的从第二个人页面跳转到第三个页面都是用intent的知识。传值是putExtra,接收是getIntExtra。
private ImageView richangBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rechang);
bindID();
}
private void bindID() {
richangBtn = findViewById(R.id.richang_btn);
richangBtn.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.richang_btn:
Intent intent = new Intent(RechangActivity.this, RRActivity.class);
startActivity(intent);
break;
}
}
第三步就是创建一个item的布局文件和一个RR的实体类。在实体类中定义他们的属性,和定义属性,构造方法。在item布局文件里面添加一个TextView控件,在rr布局文件里面添加listview的id。
public class RR {
private int categoryId;
private int classifyId;
private String classifyName;
public RR(int categoryId, int classifyId, String classifyName) {
this.categoryId = categoryId;
this.classifyId = classifyId;
this.classifyName = classifyName;
}
public int getCategoryId() {
return categoryId;
}
public void setCategoryId(int categoryId) {
this.categoryId = categoryId;
}
public int getClassifyId() {
return classifyId;
}
public void setClassifyId(int classifyId) {
this.classifyId = classifyId;
}
public String getClassifyName() {
return classifyName;
}
public void setClassifyName(String classifyName) {
this.classifyName = classifyName;
}
}
<LinearLayout 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"
android:orientation="vertical"
tools:context="myapplication.com.example.json.RRActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#F33407"
android:orientation="horizontal">
<ImageView
android:id="@+id/img"
android:layout_width="50dp"
android:layout_height="40dp"
android:layout_marginTop="5dp"
android:paddingLeft="10dp"
android:src="@mipmap/back_btn_white" />
<TextView
android:id="@+id/re_tv"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="5dp"
android:gravity="center"
android:paddingLeft="5dp"
android:text="日常用品"
android:textColor="#E8DFD1"
android:textSize="30sp" />
</LinearLayout>
<ListView
android:id="@+id/rr_listview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/rr_item_tv"
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center_vertical"
android:textSize="16sp" />
</LinearLayout>
第四步就是获得HTTP的请求。写一个RRTask继承AsyncTask,然后在doInBackground方法里面创建URL对象。在此之前需要定义api,用来底下的解析。并且需要给listview定义一下属性,还有给list列表初始化一下。需要记得在onCreate方法里面绑定id。
private ListView rrlistview;
private List<RR> rrList = new ArrayList<>();
private String api = "http://103.244.59.105:8014/paopaoserver/articles?params={\"page\":1,\"page_count\":10}";
rrlistview = findViewById(R.id.rr_listview);
在inputstream里面判断一下,这样网络返回码注册,才可以进行底下的操作。
try {
URL url = new URL(api);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream inputStream = null;
if (connection.getResponseCode() == 200) {
inputStream = connection.getInputStream();//只有网络正常且返回数据正常时,我们才能创建输入流
} else {
return "network_failed";//网络连接失败
}
InputStreamReader reader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(reader);
String temp = "";
while ((temp = bufferedReader.readLine()) != null) {
stringBuffer.append(temp);
}
bufferedReader.close();
reader.close();
inputStream.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return stringBuffer.toString();
}
第五步就是先创建一个RRAdapter继承BaseAdapter,在里面因为数据比较小,所以在这里我们不要用缓存原理。然后在onPostExecute方法里面进行JSON解析,最后在主线程里面绑定id,在子线程里面启动RRtask。
public class RRAdapter extends BaseAdapter {
private Context context;
private List<RR> rrList;
public RRAdapter(Context context, List<RR> rrList) {
this.context = context;
this.rrList = rrList;
}
@Override
public int getCount() {
return rrList.size();
}
@Override
public Object getItem(int i) {
return rrList.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.rr_item, null);
TextView tv = v.findViewById(R.id.rr_item_tv);
tv.setText(rrList.get(i).getClassifyName());
return v;
}
@Override
protected void onPostExecute(String s) {
try {
JSONObject object = new JSONObject(s);
JSONArray array = object.getJSONArray("datas");
for (int i = 0; i < array.length(); i++) {
JSONObject obj = array.getJSONObject(i);
//创建RR对象
RR rr = new RR(obj.getInt("category_id"), obj.getInt("classify_id"), obj.getString("classify_name"));
rrList.add(rr);
}
RRAdapter adapter = new RRAdapter(RRActivity.this, rrList);
rrlistview.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
上面的代码都是RechangActivity跳转到RRActivity里面的。接下来就是从RRActivity跳转到GoodsActivity里面。原理其实都差不多,就是在GoodsActivity里面加一个IMgTask类部类,用来加载网上的图片的。
**接下来就是goods和item的布局文件。**
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="100dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/goods_item_iv"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_margin="10dp"
android:src="@mipmap/articles" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="4"
android:minHeight="80dp"
android:orientation="vertical">
<TextView
android:id="@+id/goods_item_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="福临门食用调和油"
android:textSize="20sp" />
<TextView
android:id="@+id/goods_item_type"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:text="日常用品-调料干货"
android:textSize="14sp" />
<TextView
android:id="@+id/goods_item_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="¥39.8"
android:textColor="@color/colorAccent"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2">
<Button
android:id="@+id/goods_item_buy_btn"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_gravity="bottom"
android:layout_marginRight="10dp"
android:background="@color/colorAccent"
android:text="购买"
android:textColor="#fff" />
</LinearLayout>
</LinearLayout>
<LinearLayout 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"
android:orientation="vertical"
tools:context="myapplication.com.example.json.GoodsActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#F33407"
android:orientation="horizontal">
<ImageView
android:id="@+id/img"
android:layout_width="50dp"
android:layout_height="40dp"
android:layout_marginTop="5dp"
android:paddingLeft="10dp"
android:src="@mipmap/back_btn_white" />
<TextView
android:id="@+id/re_tv"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="5dp"
android:gravity="center"
android:paddingLeft="5dp"
android:text="调料干货"
android:textColor="#E8DFD1"
android:textSize="30sp" />
</LinearLayout>
<ListView
android:id="@+id/goods_lv"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
创建一个实体类、适配器、activity。
public class GoodsActivity extends AppCompatActivity {
private List<Goods> goodsList = new ArrayList<>();
private ListView listView;
private GoodsAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_goods);
//取值---classifyId
Intent intent = getIntent();
int id = intent.getIntExtra("id", 0);
listView = findViewById(R.id.goods_lv);
//构造类目的API
String api = "http://103.244.59.105:8014/paopaoserver/categorylist?params={\"classify_id\":" + id + ",\"page\":1,\"page_count\":10}";
//HTTP请求
new MyTask().execute(api);
}
class MyTask extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... strings) {
StringBuffer stringBuffer = new StringBuffer();
//http请求
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);
}
bufferedReader.close();
reader.close();
inputStream.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return stringBuffer.toString();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if (s.equals("network_failed")) {
Toast.makeText(GoodsActivity.this, "网络连接失败", Toast.LENGTH_SHORT).show();
} else {
//JSON解析
try {
JSONObject object = new JSONObject(s);
JSONArray array = object.getJSONArray("datas");
for (int i = 0; i < array.length(); i++) {
String categoryName = array.getJSONObject(i).getString("category_name");
int classifyId = array.getJSONObject(i).getInt("classify_id");
int cityId = array.getJSONObject(i).getInt("city_id");
double nowPrice = array.getJSONObject(i).getDouble("nowprice");
double oldPrice = array.getJSONObject(i).getDouble("oldprice");
int productId = array.getJSONObject(i).getInt("product_id");
int countiesId = array.getJSONObject(i).getInt("counties_id");
String classifyName = array.getJSONObject(i).getString("classify_name");
String productName = array.getJSONObject(i).getString("product_name");
String smallPic = array.getJSONObject(i).getString("small_pic");
Goods goods = new Goods(categoryName, classifyId, cityId, nowPrice, oldPrice, productId, countiesId, classifyName, productName, smallPic);
goodsList.add(goods);//填充list列表
}
//刷新listview
GoodsAdapter adapter = new GoodsAdapter(GoodsActivity.this, goodsList);
listView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
接下来最最重要的的加载图片。写一个ImgTask继承AsyncTask,返回的是Bitmap类型。
public class GoodsAdapter extends BaseAdapter {
private List<Goods> goodsList;
private Context context;
public GoodsAdapter(Context context, List<Goods> goodsList) {
this.context = context;
this.goodsList = goodsList;
}
@Override
public int getCount() {
return goodsList.size();
}
@Override
public Object getItem(int i) {
return goodsList.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View view1 = LayoutInflater.from(context).inflate(R.layout.goods_item, null);
ImageView imageView = view1.findViewById(R.id.goods_item_iv);
TextView titleTV = view1.findViewById(R.id.goods_item_title);
TextView typeTV = view1.findViewById(R.id.goods_item_type);
TextView priceTV = view1.findViewById(R.id.goods_item_price);
Button buyBtn = view1.findViewById(R.id.goods_item_buy_btn);
Goods goods = goodsList.get(i);
titleTV.setText(goods.getProductName());
typeTV.setText(goods.getCategoryName() + "-" + goods.getClassifyName());
priceTV.setText(goods.getNowPrice() + "");
Bitmap bmp = null;
String img_url = "http://103.244.59.105:8014/paopaoserver" + goods.getSmallPic();
new ImgTask(imageView).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, img_url);
return view1;
}
class ImgTask extends AsyncTask<String, String, Integer> {
private ImageView imageView;
private Bitmap bmp;
public ImgTask(ImageView imageView) {
this.imageView = imageView;
}
@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();
bmp = 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(bmp);
}
}