zoukankan      html  css  js  c++  java
  • listview动态加载数据,并更新数据列表

    package com.com.zgt.testlist;
     
    import java.util.ArrayList;
    import java.util.HashMap;
     
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    import android.widget.ListView;
    import android.widget.SimpleAdapter;
    import android.widget.TextView;
     
    public class MainActivity extends Activity {
     
    ListView mListView;
    MyAdapter adapter;
    ArrayList<String> list;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mListView = (ListView) findViewById(R.id.listView1);
            
            LayoutInflater inflater = getLayoutInflater();
            
            View v = inflater.inflate(R.layout.list_footer, null);
            
            final TextView mTextView =(TextView) v.findViewById(R.id.footer_more);
            final LinearLayout mLayout = (LinearLayout)v.findViewById(R.id.footer_loading);
            
            list = new ArrayList<String>();
            
            for(int i = 0;i<5;i++){
            list.add("zgt"+i);
            }
            
            mListView.addFooterView(v);
            
            adapter = new MyAdapter(list);
            
            mListView.setAdapter(adapter);
            
            mTextView.setOnClickListener(new OnClickListener() {
     
    @Override
    public void onClick(View v) {
     
     
     
     
    new AsyncTask<String, String, String>() {
     
     
     
    @Override
    protected void onPreExecute() {
    super.onPreExecute();
    mTextView.setVisibility(View.GONE);
    mLayout.setVisibility(View.VISIBLE);
    }
     
    @Override
    protected String doInBackground(String... params) {
    try {
    Thread.sleep(1000);
    list.add("zzzzzzzzzzzzz");
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return null;
    }
     
    @Override
    protected void onPostExecute(String result) {
    super.onPostExecute(result);
    mTextView.setVisibility(View.VISIBLE);
    mLayout.setVisibility(View.GONE);
    adapter.notifyDataSetChanged();
     
    }
     
     
    }.execute();
     
     
     
     
     
    }
    });
            
           
        }
        
        
        private class MyAdapter extends BaseAdapter{
        
        private ArrayList<String> list;
        
        public MyAdapter(ArrayList<String> list) {
    this.list = list;
    }
     
    @Override
    public int getCount() {
     
    return list.size();
    }
     
    @Override
    public Object getItem(int position) {
    // TODO Auto-generated method stub
    return list.get(position);
    }
     
    @Override
    public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
    }
     
    @Override
    public View getView(int position, View view, ViewGroup parent) {
    view = getLayoutInflater().inflate(R.layout.item, null);
    if(list.size()>0){
    ImageView img = (ImageView) view.findViewById(R.id.imageView1);
    TextView mTextView = (TextView) view.findViewById(R.id.textView1);
    mTextView.setText(list.get(position));
    }
     
     
    return view;
    }
        
        }
     
       
    }
  • 相关阅读:
    Android升级ADT22后会报ClassNotFoundException的原因分析
    修改Android解锁界面
    Android中dip, dp, px,pt, sp之间的区别:
    移动开发:Android官方提供的支持不同屏幕大小的全部方法
    常用正则表达式
    Android多语言与国际化
    Android中的资源与国际化
    Android开发:使用Fragment改造TabActivity
    Android开发–Intent-filter属性详解
    Fragment、Activity比较——Android碎片介绍
  • 原文地址:https://www.cnblogs.com/zhangguangtao/p/3010372.html
Copyright © 2011-2022 走看看