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