最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android模擬實現(xiàn)網(wǎng)易新聞客戶端

 更新時間:2017年05月23日 17:01:45   作者:Prince_J  
這篇文章主要為大家詳細介紹了Android模擬實現(xiàn)網(wǎng)易新聞客戶端,具有一定的參考價值,感興趣的小伙伴們可以參考一下

首先我們先看一下要模擬的界面

我們主要實現(xiàn)的就是ListView解析json文件中的數(shù)據(jù),UI布局很簡單不做贅述。
這里我們需要一個服務(wù)器來實現(xiàn)數(shù)據(jù)的動態(tài)更新, 這里我們用到的是Tomcat8.0。
首先我們把需要解析的json文件放置到Tomcat的webapp文件下的ROOT里面,方便我們解析。

首先我們創(chuàng)建一個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;
 }
}

創(chuàng)建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:

<?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"
 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的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<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實現(xiàn)代碼:

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>

最后一定不要忘了添加網(wǎng)絡(luò)訪問權(quán)限, 這很重要, 好多同學都犯了這個錯誤、

這樣基本功能就實現(xiàn)了。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • android音頻編輯之音頻裁剪的示例代碼

    android音頻編輯之音頻裁剪的示例代碼

    本篇文章主要介紹了android音頻編輯之音頻裁剪的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01
  • 安卓版微信小程序跳一跳輔助

    安卓版微信小程序跳一跳輔助

    這篇文章主要為大家詳細介紹了安卓版微信小程序跳一跳輔助,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • RecyclerView滑動到指定Position的方法

    RecyclerView滑動到指定Position的方法

    這篇文章主要為大家詳細介紹了RecyclerView滑動到指定Position的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • AndroidView與Compose框架交互實現(xiàn)介紹

    AndroidView與Compose框架交互實現(xiàn)介紹

    Android Compose自推出正式版本后,google 就一直推薦使用Compose來開發(fā)。正好疫情期間,作為一個 Android 摸魚達人,就來摸索一下Compose的開發(fā)。說實話開發(fā)了2天感覺對Android 開發(fā)人員來說變化是巨大的,但是作為從業(yè)者我們還必須學習和學會,才能不被甩開
    2022-09-09
  • Android圓角按鈕的制作方法

    Android圓角按鈕的制作方法

    這篇文章主要為大家詳細介紹了Android圓角按鈕的制作方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • 關(guān)于Android中WebView遠程代碼執(zhí)行漏洞淺析

    關(guān)于Android中WebView遠程代碼執(zhí)行漏洞淺析

    這篇文章主要給大家介紹了關(guān)于Android中WebView遠程代碼執(zhí)行漏洞的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-05-05
  • Android便攜式熱點的開啟狀態(tài)檢測和SSID的獲取方法

    Android便攜式熱點的開啟狀態(tài)檢測和SSID的獲取方法

    WIFI熱點的開啟狀態(tài)和開啟后的SSID如何獲取呢?接下來通過本文給大家分享Android便攜式熱點的開啟狀態(tài)檢測和SSID的獲取方法,需要的朋友參考下吧
    2017-01-01
  • Android創(chuàng)建Alert框的方法

    Android創(chuàng)建Alert框的方法

    這篇文章主要介紹了Android創(chuàng)建Alert框的方法,實例分析了Android創(chuàng)建alert彈出窗口的相關(guān)技巧,需要的朋友可以參考下
    2015-07-07
  • Android 添加系統(tǒng)服務(wù)的方法詳解

    Android 添加系統(tǒng)服務(wù)的方法詳解

    這篇文章主要介紹了Android 添加系統(tǒng)服務(wù)的方法詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-02-02
  • Android 高仿微信轉(zhuǎn)賬金錢輸入框規(guī)則

    Android 高仿微信轉(zhuǎn)賬金錢輸入框規(guī)則

    這篇文章主要介紹了Android 高仿微信金錢輸入框規(guī)則的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-12-12

最新評論

扎兰屯市| 东平县| 金湖县| 荆门市| 关岭| 新沂市| 富蕴县| 六枝特区| 安多县| 建宁县| 阳春市| 于田县| 鞍山市| 龙山县| 渭南市| 介休市| 藁城市| 沧源| 扎兰屯市| 布尔津县| 启东市| 增城市| 溆浦县| 桐梓县| 增城市| 布尔津县| 荔波县| 松江区| 博客| 巴东县| 锡林浩特市| 赤峰市| 北宁市| 慈溪市| 阿图什市| 商都县| 宜川县| 宜兰县| 大连市| 手游| 宁晋县|