代码如下:
<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=".MainActivity" >
<!--
<EditText
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffff0000"
android:textSize="16sp"
android:text="hello"/>
<EditText
android:id="@+id/sex"
android:layout_below="@id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#0000ffff"
android:textSize="20sp"
android:background="#aaaa0000"
android:text="hello"/>-->
<TextView
android:id="@+id/showname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#aaaaaaaa"
android:text="hello"/>
<TextView
android:id="@+id/showsex"
android:layout_below="@id/showname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#99999999"
android:text="hello"/>
<Button
android:id="@+id/button"
android:layout_below="@id/showsex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#33333333"
android:text="hello"/></RelativeLayout>package com.example.adatpertest;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;import android.os.Bundle;
import android.app.ListActivity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
//import android.widget.EditText;
import android.widget.SimpleAdapter;
//import android.widget.TextView;
import android.view.View.OnClickListener;public class MainActivity extends ListActivity { private Button button;
// private EditText name;
// private EditText sex;
// private TextView showname;
// private TextView showsex;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
// name = (EditText)findViewById(R.id.name);
// sex = (EditText)findViewById(R.id.sex);
// showname = (TextView)findViewById(R.id.showname);
// showsex = (TextView)findViewById(R.id.showsex);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
Map<String, Object> map = new HashMap<String, Object>();
// map.put("showname", name.getText());
// map.put("showsex", sex.getText());
map.put("showname", "hello");
map.put("showsex", "zyq");
list.add(map);
SimpleAdapter adapter = new SimpleAdapter(MainActivity.this,list,R.layout.activity_main,new String[]{"showname","showsex"},new int[]{R.id.showname,R.id.showsex});
setListAdapter(adapter);
}
});
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}}
<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=".MainActivity" >
<!--
<EditText
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffff0000"
android:textSize="16sp"
android:text="hello"/>
<EditText
android:id="@+id/sex"
android:layout_below="@id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#0000ffff"
android:textSize="20sp"
android:background="#aaaa0000"
android:text="hello"/>-->
<TextView
android:id="@+id/showname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#aaaaaaaa"
android:text="hello"/>
<TextView
android:id="@+id/showsex"
android:layout_below="@id/showname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#99999999"
android:text="hello"/>
<Button
android:id="@+id/button"
android:layout_below="@id/showsex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#33333333"
android:text="hello"/></RelativeLayout>package com.example.adatpertest;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;import android.os.Bundle;
import android.app.ListActivity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
//import android.widget.EditText;
import android.widget.SimpleAdapter;
//import android.widget.TextView;
import android.view.View.OnClickListener;public class MainActivity extends ListActivity { private Button button;
// private EditText name;
// private EditText sex;
// private TextView showname;
// private TextView showsex;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
// name = (EditText)findViewById(R.id.name);
// sex = (EditText)findViewById(R.id.sex);
// showname = (TextView)findViewById(R.id.showname);
// showsex = (TextView)findViewById(R.id.showsex);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
Map<String, Object> map = new HashMap<String, Object>();
// map.put("showname", name.getText());
// map.put("showsex", sex.getText());
map.put("showname", "hello");
map.put("showsex", "zyq");
list.add(map);
SimpleAdapter adapter = new SimpleAdapter(MainActivity.this,list,R.layout.activity_main,new String[]{"showname","showsex"},new int[]{R.id.showname,R.id.showsex});
setListAdapter(adapter);
}
});
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}}
<TextView
android:id="@+id/showname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#aaaaaaaa"
android:text="hello"/>
<TextView
android:id="@+id/showsex"
android:layout_below="@id/showname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#99999999"
android:text="hello"/>
请重新给这两个控件写个layout
加载的SimpleAdapter adapter = new SimpleAdapter(MainActivity.this,list,R.layout.activity_main,new String[]{"showname","showsex"},new int[]{R.id.showname,R.id.showsex});
这里引用这个layout