直奔主题~!

结构如图:

main.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<ListView android:id="@id/android:list" android:layout_height="wrap_content"
		android:layout_width="fill_parent" android:drawSelectorOnTop="false" ></ListView>
</LinearLayout>

user.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
    
    <TextView android:id="@+id/user_name"   
              android:layout_width="180dip"   
              android:layout_height="wrap_content"   
              android:singleLine="true"   
             android:textSize="10pt"  
             android:paddingTop="2dip"  
            android:paddingLeft="2dip"  
              />  
             
   <TextView android:id="@+id/user_ip"  
             android:layout_width="180dip"  
              android:layout_height="wrap_content"  
              android:textSize="10pt"  
              android:singleLine="true"   
              android:paddingTop="2dip"  
             android:paddingRight="2dip"  
              />  
</LinearLayout>

Control_ListViewActivity.java代码:

public class Control_ListViewActivity extends ListActivity {
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		// super.onListItemClick(l, v, position, id);
		super.onListItemClick(l, v, position, id);
	     TextView tv=  (TextView) v.findViewById(R.id.user_name);
	     String username=tv.getText().toString();
		Toast.makeText(Control_ListViewActivity.this,
				"id = " + id + "position = " + position+"username="+username, Toast.LENGTH_LONG)
				.show();

	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
		for (int i = 0; i < 20; i++) {
			HashMap<String, String> hm = new HashMap<String, String>();
			hm.put("username", "jwc" + String.valueOf(i));
			hm.put("user_id", String.valueOf(i));
			list.add(hm);
		}

		SimpleAdapter sa = new SimpleAdapter(Control_ListViewActivity.this,
				list, R.layout.user, new String[] { "username", "user_id" },
				new int[] { R.id.user_name, R.id.user_ip });

		setListAdapter(sa);

	}
}

 

posted on 2011-09-14 16:41  Jwc  阅读(126)  评论(0编辑  收藏  举报