ListView和BaseAdapter 把新聞數據添加到ListView

ListView和BaseAdapter 把新聞數據添加到ListViewandroid

//佈局界面有2個xml文件 1 -- activity_main.xml
                       2 -- item_activity.xmljson

//注意添加聯網權限api

一、activity_main.xml文件 佈局ide

代碼工具

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

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

二、item_activity.xml文件 佈局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>
-------------------------

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

相關文章
相關標籤/搜索