怎么在Android应用中模拟一个新闻客户端

发布时间:2020-12-05 17:28:17 作者:Leah
来源:亿速云 阅读:172

这期内容当中小编将会给大家带来有关怎么在Android应用中模拟一个新闻客户端,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

首先我们创建一个JsonParse类用来解析json文件:

package cn.edu.bzu.myapplication.Tools;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.List;

import cn.edu.bzu.myapplication.entity.NewsInfo;

/**
 * Created by Becauseshy on 2017/5/18.
 */

public class JsonParse {
 public static List<NewsInfo> getNewInfo(String json){
  Gson gson=new Gson();
  Type listType=new TypeToken<List<NewsInfo>>(){

  }.getType();
  List<NewsInfo> newsInfos=gson.fromJson(json,listType);


  return newsInfos;
 }
}

创建json文件的实体类:

package cn.edu.bzu.myapplication.entity;

/**
 * Created by Becauseshy on 2017/5/17.
 */

public class NewsInfo {
 private String iconPath;
 private String title;
 private String description;
 private int type;
 private long comment;

 public String getIconPath() {
  return iconPath;
 }

 public void setIconPath(String iconPath) {
  this.iconPath = iconPath;
 }

 public String getTitle() {
  return title;
 }

 public void setTitle(String title) {
  this.title = title;
 }

 public String getDescription() {
  return description;
 }

 public void setDescription(String description) {
  this.description = description;
 }

 public int getType() {
  return type;
 }

 public void setType(int type) {
  this.type = type;
 }

 public long getComment() {
  return comment;
 }

 public void setComment(long comment) {
  this.comment = comment;
 }
}

activity_main.xml:

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<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"
 tools:context=".MainActivity"
 android:orientation="vertical">

 <FrameLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <LinearLayout
   android:id="@+id/loading"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:gravity="center"
   android:orientation="vertical"
   android:visibility="invisible">
   <ProgressBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
   <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="正在加载信息..." />
  </LinearLayout>
  <ListView
   android:id="@+id/lv_news"
   android:layout_width="match_parent"
   android:layout_height="match_parent" />
 </FrameLayout>
</LinearLayout>

item的布局文件:

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="65dp">
 <com.loopj.android.image.SmartImageView
  android:id="@+id/siv_icon"
  android:layout_width="80dp"
  android:layout_height="60dp"
  android:scaleType="centerCrop"
  android:src="@mipmap/ic_launcher"
  android:layout_alignParentLeft="true"
  android:layout_alignParentStart="true"></com.loopj.android.image.SmartImageView>
 <TextView
  android:id="@+id/tv_title"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_marginLeft="5dp"
  android:layout_marginTop="10dp"
  android:layout_toRightOf="@id/siv_icon"
  android:ellipsize="end"
  android:maxLength="20"
  android:singleLine="true"
  android:text="我是标题"
  android:textColor="#000000"
  android:textSize="18sp" />

 <TextView
  android:id="@+id/tv_description"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_below="@id/tv_title"
  android:layout_marginLeft="5dp"
  android:layout_marginTop="5dp"
  android:layout_toRightOf="@id/siv_icon"
  android:ellipsize="end"
  android:maxLength="16"
  android:maxLines="1"
  android:text="我是描述"
  android:textColor="#99000000"
  android:textSize="14sp" />

 <TextView
  android:id="@+id/tv_type"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentBottom="true"
  android:layout_alignParentRight="true"
  android:layout_marginBottom="5dp"
  android:layout_marginRight="10dp"
  android:text="评论"
  android:textColor="#99000000"
  android:textSize="12sp" />

</RelativeLayout>

适配器代码:

package cn.edu.bzu.myapplication.adapter;

/**
 * Created by Becauseshy on 2017/5/17.
 */
import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.loopj.android.image.SmartImageView;

import java.util.List;

import cn.edu.bzu.myapplication.R;
import cn.edu.bzu.myapplication.entity.NewsInfo;


public class NewAdapter extends ArrayAdapter<NewsInfo>{
private int resourceID;
 public NewAdapter(Context context, int resource, List<NewsInfo> objects) {
  super(context, resource, objects);
  resourceID=resource;
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
  NewsInfo fruit=getItem(position);
  View view;
  ViewHolder viewHolder;
  if(convertView==null){
    view=LayoutInflater.from(getContext()).inflate(resourceID,null);
    viewHolder=new ViewHolder();
   viewHolder.siv=(SmartImageView)view.findViewById(R.id.siv_icon);
   viewHolder.tv_title=(TextView)view.findViewById(R.id.tv_title);
   viewHolder.tv_description=(TextView)view.findViewById(R.id.tv_description);
   viewHolder.tv_type=(TextView)view.findViewById(R.id.tv_type);
   view.setTag(viewHolder);

  }else{
   view=convertView;
   viewHolder= (ViewHolder) view.getTag();

  }
  viewHolder.siv.setImageUrl(fruit.getIconPath(),R.drawable.a,R.drawable.ic_launcher);
  viewHolder.tv_title.setText(fruit.getTitle());
  viewHolder.tv_description.setText(fruit.getDescription());
  int type=fruit.getType();
  switch (type){

   case 1:
    viewHolder.tv_type.setText("评论:"+fruit.getComment());
    viewHolder.tv_type.setTextColor(Color.BLUE);
    break;
   case 2:
    viewHolder.tv_type.setText("专题");
    viewHolder.tv_type.setTextColor(Color.BLACK);
    break;
   case 3:
    viewHolder.tv_type.setText("LIVE");
    viewHolder.tv_type.setTextColor(Color.RED);
    break;
  }

  return view;

 }
 class ViewHolder{
  SmartImageView siv;
  TextView tv_title;
  TextView tv_description;
  TextView tv_type;
 }
}

MainActivity实现代码:

package cn.edu.bzu.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import cn.edu.bzu.myapplication.Tools.JsonParse;
import cn.edu.bzu.myapplication.adapter.NewAdapter;
import cn.edu.bzu.myapplication.entity.NewsInfo;
import cn.edu.bzu.myapplication.model.Fruit;

public class MainActivity extends AppCompatActivity {
 private ListView Iv_news;
 private NewAdapter newAdapter;
 private List<NewsInfo> newInfos;
 private LinearLayout loading;
 private JsonParse jsonParse;


 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  Iv_news= (ListView) findViewById(R.id.lv_news);
  newAdapter =new NewAdapter(this,R.layout.news_item,newInfos);
  loading= (LinearLayout) findViewById(R.id.loading);
  prepareData();


 }

 private void prepareData() {
  //fruitList=new ArrayList<>();
  //Fruit apple=new Fruit("Apple",R.drawable.apple_pic);
  // fruitList.add(apple);
  AsyncHttpClient asyncHttpClient=new AsyncHttpClient();
  asyncHttpClient.get(getString(R.string.serverurl), new AsyncHttpResponseHandler() {

   @Override
   public void onSuccess(int i, cz.msebera.android.httpclient.Header[] headers, byte[] bytes) {
    try {
     String json=new String(bytes,"utf-8");
     newInfos=jsonParse.getNewInfo(json);
     if(newInfos==null){
      Toast.makeText(MainActivity.this,"解析失败",Toast.LENGTH_SHORT).show();
     }
     else {
      loading.setVisibility(View.INVISIBLE);
      Iv_news.setAdapter(newAdapter);


     }
    } catch (UnsupportedEncodingException e) {
     e.printStackTrace();
    }


   }

   @Override
   public void onFailure(int i, cz.msebera.android.httpclient.Header[] headers, byte[] bytes, Throwable throwable) {
    Toast.makeText(MainActivity.this,"请求失败",Toast.LENGTH_SHORT).show();

   }
  });


 }

}

在values文件加下的String.xml文件中添加:

<string name="serverurl">http://172.16.26.58:8080/newInfo.xml</string>

最后一定不要忘了添加网络访问权限

上述就是小编为大家分享的怎么在Android应用中模拟一个新闻客户端了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. 怎么在VirtualBox中模拟一个Linux集群
  2. 怎么在Android应用中实现一个图案解锁功能

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android roi

上一篇:hadoop1.x如何在VMware虚拟机中安装

下一篇:怎么在Android应用中实现一个网页图片浏览功能

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》