新闻客户端android

主要文件展示

主界面布局

<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=".MainActivity" >

    <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界面
<?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="65dip" >

    <com.loopj.android.image.SmartImageView
        android:id="@+id/siv_icon"
        android:layout_width="80dip"
        android:layout_height="60dip"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="5dip"
        android:layout_marginLeft="5dip"
        android:layout_marginTop="5dip"
        android:scaleType="centerCrop"
        android:src="@drawable/ic_launcher" >
    </com.loopj.android.image.SmartImageView>

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dip"
        android:layout_marginTop="10dip"
        android:layout_toRightOf="@id/siv_icon"
        android:text="我是标题"
        android:maxLength="20"
        android:singleLine="true"
        android:ellipsize="end"
        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="5dip"
        android:layout_marginTop="5dip"
        android:layout_toRightOf="@id/siv_icon"
        android:text="我是描述"
        android:maxLength="16"
        android:singleLine="true"
        android:ellipsize="end"
        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="5dip"
        android:text="评论"
        android:textColor="#99000000"
        android:textSize="12sp" />

</RelativeLayout>
java部分
package cn.itcast.news;

import java.io.ByteArrayInputStream;
import java.util.List;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import cn.itcast.news.bean.NewsInfo;
import cn.itcast.news.utils.NewsInfoService;

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

public class MainActivity extends Activity {
   private ListView lv_news;
   private LinearLayout loading;
   private List<NewsInfo> newsInfos;
     //ListView������ 
   private class NewsAdapter extends BaseAdapter {
      //listView��item��
      public int getCount() {
         return newsInfos.size();
      }
      //õlistview ��Ŀ��ͼ
      public View getView(int position, View convertView, ViewGroup parent) {
         View view = View.inflate(MainActivity.this, R.layout.news_item,
               null);
         SmartImageView siv = (SmartImageView) view
               .findViewById(R.id.siv_icon);
         TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
         TextView tv_description = (TextView) view
               .findViewById(R.id.tv_description);
         TextView tv_type = (TextView) view.findViewById(R.id.tv_type);
         NewsInfo newsInfo = newsInfos.get(position);
               //SmartImageView����ָ��·��ͼƬ
         siv.setImageUrl(newsInfo.getIconPath(), R.drawable.ic_launcher,
               R.drawable.ic_launcher);
              //�������ű���
         tv_title.setText(newsInfo.getTitle());
              //������������
         tv_description.setText(newsInfo.getDescription());
         int type = newsInfo.getType(); // 1. һ������ 2.ר�� 3.live
              //��ͬ�����������òͬ����ɫͲͬ������
         switch (type) {
         case 1:
            tv_type.setText("����:" + newsInfo.getComment());
            break;
         case 2:
            tv_type.setTextColor(Color.RED);
            tv_type.setText(��");
            break;
         case 3:
            tv_type.setTextColor(Color.BLUE);
            tv_type.setText("LIVE");
            break;
         }
         return view;
      }
      //��Ŀ����
      public Object getItem(int position) {
         return null;
      }
      //��Ŀid
      public long getItemId(int position) {
         return 0;
      }
   }
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      lv_news = (ListView) findViewById(R.id.lv_news);
      loading = (LinearLayout) findViewById(R.id.loading);
      fillData2();
   }
    //ʹ��AsyncHttpClient��������
   private void fillData2() {
         //����AsyncHttpClientʵ��
      AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
         //ʹ��GET��ʽ����
        asyncHttpClient.get(MainActivity.this.getString(R.string.serverurl),  
          new AsyncHttpResponseHandler() {
               public void onSuccess(String content) {
                            //���ʳɹ                  super.onSuccess(content);
                            //���ַ���ת����Byte����
                  byte[] bytes = content.getBytes();
                            //��Byte����ת����������
                  ByteArrayInputStream bais = new ByteArrayInputStream(
                        bytes); 
                            //����NewsInfoService���������xmlļ                  newsInfos = NewsInfoService.getNewsInfos(bais);
                  if (newsInfos == null) {
                     // ����ʧ�� ����toast
                     Toast.makeText(MainActivity.this, 
                                        "����ʧ��", 0).show();
                  } else {
                     // ���½���.
                     loading.setVisibility(View.INVISIBLE);
                     lv_news.setAdapter(new NewsAdapter());
                  }
               }
                         //����ʧ��
               public void onFailure(Throwable error, String content) {   
                  super.onFailure(error, content);
                  Log.e("error",MainActivity.this.getString(R.string.serverurl));
                  Log.e("error", error.toString());
                  Toast.makeText(MainActivity.this, "����ʧ��", 0).show();
               }
            });
   }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值