不多说直接贴代码
TestActivity代码
package com.example.activity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class TestActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
initView();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP) {
handler.sendEmptyMessage(0);
return true;
}
return super.onTouchEvent(event);
}
Handler handler=new Handler(){
public void handleMessage(android.os.Message msg) {
Getdata();
};
};
List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
ListView lv_test;
MyAdapter adapter;
private void initView() {
lv_test = (ListView) findViewById(R.id.lv_test);
adapter=new MyAdapter();
lv_test.setAdapter(adapter);
lv_test.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view,int position, long Id) {
Toast.makeText(TestActivity.this,
list.get(position).get("tv0") + "," + list.get(position).get("tv1"),
Toast.LENGTH_SHORT).show();
}
});
}
private void Getdata() {
list.clear();
Random r = new Random();
int size = (int) (r.nextFloat() * 20);
for (int i = 0; i < size; i++) {
int a = (int) (r.nextFloat() * 100);
int b = (int) (r.nextFloat() * 100);
HashMap<String, Object> hashMap = new HashMap<String, Object>();
hashMap.put("tv0", a);
hashMap.put("tv1", b);
list.add(hashMap);
}
adapter.refresh(list);
}
class MyAdapter extends BaseAdapter {
private List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
public void refresh(List<HashMap<String, Object>> data) {
this.data = data;
notifyDataSetChanged();
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View view, ViewGroup group) {
ViewHolder viewHolder ;
if (view == null) {
viewHolder = new ViewHolder();
view = LayoutInflater.from(TestActivity.this).inflate(
R.layout.item_lv_test, null,true);
viewHolder.tv0_item_lv = (TextView) view.findViewById(R.id.tv0_item_lv);
viewHolder.tv1_item_lv = (TextView) view.findViewById(R.id.tv1_item_lv);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
HashMap<String, Object> hashMap = data.get(position);
viewHolder.tv0_item_lv.setText(hashMap.get("tv0").toString());
viewHolder.tv1_item_lv.setText(hashMap.get("tv1").toString());
return view;
}
}
private final class ViewHolder {
public TextView tv0_item_lv, tv1_item_lv;
}
}
activity_test.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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".TestActivity" >
<ListView
android:id="@+id/lv_test"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:baselineAligned="false"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1" >
<TextView
android:id="@+id/tv0_item_lv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1" >
<TextView
android:id="@+id/tv1_item_lv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1" />
</RelativeLayout>
</LinearLayout>
一些关键点