ListView和BaseAdapter 把新闻数据添加到ListView

ListView和BaseAdapter 把新闻数据添加到ListView

//布局界面有2个xml文件 1 -- activity_main.xml
                       2 -- item_activity.xml

//注意添加联网权限

1、activity_main.xml文件 布局

代码

<RelativeLayout 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="${relativePackage}.${activityClass}" >

   
    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
   

</RelativeLayout>
----------------------------

2、item_activity.xml文件 布局

代码

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

    <TextView
        android:id="@+id/subject"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"/>
   
     <TextView
        android:id="@+id/summary"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="17sp"/>
    
      <TextView
        android:id="@+id/changed"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="10sp"/>

</LinearLayout>
-------------------------

3、MainActivity 类

代码

public class MainActivity extends Activity {
private ListView listview;
private MyBaseAdapter adapter;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  this.listview = (ListView) this.findViewById(R.id.listview);
  
  String url = "http://litchiapi.jstv.com/api/GetFeeds?column=0&PageSize=20&pageIndex=1&val=100511D3BE5301280E0992C73A9DEC41";

  new MyAsyncTask().execute(url);
 }
 //自定义的工具类 继承AsyncTask
//用于联网 下载需要的数据 操作
 class MyAsyncTask extends AsyncTask<String, Void, byte[]>{

  @Override
  protected byte[] doInBackground(String... params) {
   String url = params[0];
   HttpGet get = new HttpGet(url);
   HttpClient client = new DefaultHttpClient();
   try {
    HttpResponse response = client.execute(get);
    if(response.getStatusLine().getStatusCode() == 200){
     return EntityUtils.toByteArray(response.getEntity());
    }
   } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   
   return null;
  }
  @Override
  protected void onPostExecute(byte[] result) {
   if(result != null){
    
//把下载好 需要的数据 给jsonObject 自己定义的解析方法
//把需要解析好的数据 赋值给一个集合
    List<Map<String, Object>> data = jsonObject(new String(result));
//把集合里的数据添加到baseadapter 适配器
    adapter = new MyBaseAdapter(data);

//把适配器 绑定到listview
    listview.setAdapter(adapter);
    
   }
    
   }
//MyBaseAdapter 类继承 BaseAdapter 类
  class MyBaseAdapter extends BaseAdapter{
   
   private List<Map<String, Object>> list;
   public MyBaseAdapter(List<Map<String, Object>> data) {
    this.list = data;
    Log.i("data", "" + list.size());
   }
   @Override
   public int getCount() {
    return this.list.size();
   }
   
   @Override
   public Object getItem(int position) {
    return this.list.get(position);
   }
   
   @Override
   public long getItemId(int position) {
    return position;
   }
   
   @Override
   public View getView(int position, View convertView,
     ViewGroup parent) {
    //声明 ViewHolder 对象
        ViewHolder viewholder = null;
//这里利用判断 当第一屏的时候 就执行下列的方法
    if(convertView == null){
     //第一屏的时候
     viewholder = new ViewHolder();
//找到自定义的布局文件item_activity.xml文件 方法1
     convertView = getLayoutInflater().inflate(R.layout.item_activity, null);
//方法2 :convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_activity, null);

     TextView subject_text = (TextView) convertView.findViewById(R.id.subject);
     TextView summary_text = (TextView) convertView.findViewById(R.id.summary);
     TextView changed_text = (TextView) convertView.findViewById(R.id.changed);
    
     viewholder.text_subject = subject_text;
     viewholder.text_summary = summary_text;
     viewholder.text_changed = changed_text;
     
     convertView.setTag(viewholder);
//不是第一屏的时候 就 直接 使用第一屏封装好的数据
//这样就不用每次加载一项Item的时候 都调用 上面的 方法 -- 这样 内存消耗就比较低
    }else{
     //第二屏 后 调用
     viewholder = (ViewHolder) convertView.getTag();
    }
    //把 当前的 内容 设置 给 个个 TextView
    viewholder.text_subject.setText(this.list.get(position).get("subject").toString());
    viewholder.text_summary.setText(this.list.get(position).get("summary").toString());
    viewholder.text_changed.setText(this.list.get(position).get("changed").toString());
    
    return convertView;
   }
   //ViewHolder 类  -- 用于 存储 布局元素中的数据 -- 封装类
//这样更加好 或 方便 取数据 可读性也 好些
   class ViewHolder{
    private TextView text_subject;
    private TextView text_summary;
    private TextView text_changed;
   }
  }
//json解析 解析需要的数据
  private List<Map<String, Object>> jsonObject(String string) {
   List<Map<String, Object>> data_list = new ArrayList<Map<String, Object>>();
   try {
    JSONObject obj = new JSONObject(string);
    JSONObject obj_paramz = obj.getJSONObject("paramz");
    JSONArray array_feeds = obj_paramz.getJSONArray("feeds");
    for(int i = 0;i<array_feeds.length();i++){
     JSONObject object = array_feeds.getJSONObject(i);
     JSONObject obj_data = object.getJSONObject("data");
     Map<String,Object> map = new HashMap<String, Object>();
     map.put("subject", obj_data.getString("subject"));
     map.put("summary", obj_data.getString("summary"));
     map.put("changed", obj_data.getString("changed"));
     
     data_list.add(map);
    }
   } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   
   return data_list;
  }
 }
}

转载于:https://my.oschina.net/u/2542711/blog/603643

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值