main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView
android:id="@+id/show_result"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
items.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="6dip"
android:textColor="#cceecc" >
</TextView>
<TextView
android:id="@+id/text2"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:padding="10dip"
android:textColor="#cceecc" >
</TextView>
</LinearLayout>
ListViewAdapter.java
package com.updatelistview.main;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class ListViewAdapter extends BaseAdapter {
private Context mContext;
List mList = new ArrayList();
public ListViewAdapter(Context context, List list) {
mContext = context;
mList = list;
}
public int getCount() {
// TODO Auto-generated method stub
return mList.size();
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(mContext).inflate(R.layout.items,
null);
holder.text1 = (TextView) convertView.findViewById(R.id.text1);
holder.text2 = (TextView) convertView.findViewById(R.id.text2);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text1.setText(((HashMap) mList.get(position)).get("viewspot")
.toString());
holder.text2.setText(((HashMap) mList.get(position)).get("add")
.toString());
return convertView;
}
final class ViewHolder {
public TextView text1;
public TextView text2;
}
}
==============================================
package com.updatelistview.main;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class UpdateListViewActivity extends Activity {
// 控件
private ListView listview;
private Context context;
// 数组
private ListViewAdapter listItemAdapter;
private ArrayList<HashMap<String, Object>> listItem = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = this;
listview = (ListView) findViewById(R.id.show_result);
init();
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
listItem.clear();
for (int i = 0; i < 12; i++) {
HashMap<String, Object> map = new HashMap<String, Object>();
if (i == arg2) {
map.put("viewspot", "ddddd" + i);
map.put("add", "ccc");
} else {
map.put("viewspot", "data more" + i);
map.put("add", "aaaaaa");
}
listItem.add(map);
}
listItemAdapter = new ListViewAdapter(context, listItem);
listview.setAdapter(listItemAdapter);
listItemAdapter.notifyDataSetChanged();
}
});
}
private void init() {
listItem = new ArrayList<HashMap<String, Object>>();
for (int i = 0; i < 12; i++) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("viewspot", "data more" + i);
map.put("add", "aaaaaa");
listItem.add(map);
}
listItemAdapter = new ListViewAdapter(context, listItem);
listview.setAdapter(listItemAdapter);
}
}