使用mvc模型实现简易新闻客户端功能(本地)

本文章思路是根据MVC(model、view、controler)模型实现一个新闻客户端的原始功能(ListView的跳转匹配)
model端包含图片、标题、内容域,代码如下:

package com.example.listviewextenddemo;

public class Resource {
    private int image;
    private int title;
    private int content;
    public Resource(int image,int title,int content){
        this.image=image;
        this.title=title;
        this.content=content;
    }

    public int getContent() {
        return content;
    }

    public int getImage() {
        return image;
    }

    public int getTitle() {
        return title;
    }

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

    public void setImage(int image) {
        this.image = image;
    }

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

controler端包含listview显示界面和跳转后的内容显示界面:
1.listview界面代码:

package com.example.listviewextenddemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends Activity {
    private ListView mListView;
    private Resource[] res={new Resource(R.mipmap.aim,R.string.title_01,R.string.content_01),
            new Resource(R.mipmap.blogger,R.string.title_02,R.string.content_02),
            new Resource(R.mipmap.behance,R.string.title_03,R.string.content_03)};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mListView=(ListView)findViewById(R.id.listview);
        SimpleAdapter adapter=new SimpleAdapter(this,getData(),R.layout.layout_unit,new String[]{"image","text"},new int[]{R.id.image,R.id.text});
        mListView.setAdapter(adapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(MainActivity.this,SecondActivity.class);
                intent.putExtra("title",res[position].getTitle());
                intent.putExtra("content",res[position].getContent());
                startActivity(intent);
            }
        });
    }
    private List<Map<String,Object>> getData(){
        List<Map<String,Object>> target=new ArrayList<Map<String,Object>>();
        Map<String,Object> map=null;
        for(int i=0;i<res.length;i++){
            map=new HashMap<String,Object>();
            map.put("image",res[i].getImage());
            map.put("text",getResources().getString(res[i].getTitle()));
            target.add(map);
        }
        return target;
    }
}

内容界面代码:

package com.example.listviewextenddemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;


public class SecondActivity extends Activity {
    private TextView title;
    private TextView content;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_second);
        title=(TextView)findViewById(R.id.title);
        content=(TextView)findViewById(R.id.content);
        Intent intent=getIntent();
        int i_title=intent.getIntExtra("title",1);
        int i_content=intent.getIntExtra("content",1);
        title.setText(i_title);
        content.setText(i_content);
    }
}

view端xml文件:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</RelativeLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
    android:orientation="horizontal">
    <ImageView
        android:id="@+id/image"
        android:layout_width="30dp"
        android:layout_height="30dp"/>
    <TextView
        android:id="@+id/text"
        android:singleLine="true"
        android:layout_gravity="center"
        android:layout_marginLeft="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textSize="20sp"/>
    <ScrollView
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:id="@+id/content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </ScrollView>
</LinearLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值