新闻客户端News

AsyncHttpClient 和 SmartImageView 的使用

运行界面

新闻客户端News_第1张图片

界面交互代码

MainActivity


ListView 的 setOnItemClickListener() 方法

 用于监听Item的点击事件 使用时 需要传入一个OnItemClickListener 实现类对象 且需要实现onItemClick() 方法

ListView 的 setSelection() 方法

 设置当前选中的条目


public class MainActivity extends AppCompatActivity {
    ListView lv_news;
    LinearLayout Loading;

    @Override
    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);
        fillData3();
    }

    private void fillData3() {

        System.out.println(">>>>>>>>>>>>>执行了");

        AsyncHttpClient asynchttpClient = new AsyncHttpClient();


        asynchttpClient.get("http://10.61.19.112:8080/NewsInfo.json", new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, org.apache.http.Header[] headers, byte[] bytes) {
                try {
                    String json = new String(bytes, "utf-8");
                    List newsInfos = JsonParse.getNewsInfo(json);

                    if (newsInfos == null) {
                        Toast.makeText(MainActivity.this, "解析失败!", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(MainActivity.this, "获取成功!", Toast.LENGTH_SHORT).show();
                        Loading.setVisibility(View.INVISIBLE);
                        lv_news.setAdapter(new newsAdapter(MainActivity.this, newsInfos));
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int i, org.apache.http.Header[] headers, byte[] bytes, Throwable throwable) {
                System.out.println(">>>>>>>>>获取失败!");
            }
        });


    }
}

newsInfo类

定义了 IconPath 图片路径 新闻标题 描述 等等描述


public class newsInfo {

    private String icon;
    private String title;
    private String content;
    private int type;
    private long comment;

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public String getTitle() {
        return title;
    }

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

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    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;
    }
}

JsonParse类



android

public class JsonParse {

    public static List getNewsInfo(String json){
        Gson gson =new Gson();

        Type listType = new TypeToken>(){


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

        return newsInfos;
    }
}

newsAdapter类

android
public class newsAdapter extends ArrayAdapter{

    public newsAdapter(Context context,  List objects) {
        super(context, R.layout.news_item , objects);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        newsInfo news= getItem(position);
        View view;
        ViewHolder viewHolder;
        if (convertView == null) {
            //任务 补充完整
            view = LayoutInflater.from(getContext()).inflate(R.layout.news_item, null);
            viewHolder = new ViewHolder();
            //获取控件
            viewHolder.siv = (SmartImageView) view.findViewById(R.id.siv_icon);
            viewHolder.tvtitle = (TextView) view.findViewById(R.id.tv_title);
            viewHolder.tvdescription = (TextView) view.findViewById(R.id.tv_description);
            viewHolder.tvtype = (TextView) view.findViewById(R.id.tv_type);
            view.setTag(viewHolder);
        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.siv.setImageUrl(news.getIcon());
        viewHolder.tvtitle.setText(news.getTitle());
        viewHolder.tvdescription.setText(news.getContent());
        viewHolder.tvtype.setText(news.getType()+"");
        return view;
    }

    class ViewHolder {
        SmartImageView siv;
        TextView tvtitle;
        TextView tvdescription;
        TextView tvtype;

    }
}

布局文件

Activity_main.xml

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"
    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>

*news_item.xml


TextView用于显示数据库中的某条数据的详细信息
ImageView用于增加减少金额和删除金额


android

<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>

你可能感兴趣的:(Android)