android > ListView -- SimpleAdapter

layout/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="fill_parent"
    android:orientation="vertical" >
	
    <LinearLayout 
	    android:layout_width="fill_parent"
	    android:layout_height="fill_parent"
	    android:orientation="vertical" >
        
    	<ListView 
	        android:id="@id/android:list"
	        android:layout_width="fill_parent"
	        android:layout_height="wrap_content"
	        android:drawSelectorOnTop="false"
	        android:scrollbars="vertical"
	        >        
        
   		 </ListView>
          
    </LinearLayout>

    
   

</LinearLayout>

layout / list.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" 
    android:padding="10dip"
    >

    <TextView 
        android:id="@+id/list_1__1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        >   
    </TextView>
    
    <TextView 
        android:id="@+id/list_1__2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        >   
    </TextView>    
   

</RelativeLayout>

 Wiget2Activity.java

package wiget2.com;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class Wiget2Activity extends ListActivity {
    /** 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>>();
        HashMap<String, String> map1 = new HashMap<String, String>();
        HashMap<String, String> map2 = new HashMap<String, String>();
        HashMap<String, String> map3 = new HashMap<String, String>();
        
        map1.put("user_name", "zhangshan");
        map1.put("user_ip", "192.168.0.1");
        map2.put("user_name", "lishi");
        map2.put("user_ip", "192.168.0.2");
        map3.put("user_name", "wangwu");
        map3.put("user_ip", "192.168.0.3");
        
        list.add(map1);
        list.add(map2);
        list.add(map3);        
        
        SimpleAdapter listAdapter = new SimpleAdapter(this,
        		list,R.layout.list,
        		new String[]{"user_name","user_ip"},
        		new int[]{R.id.list_1__1,R.id.list_1__2}
        		);
        setListAdapter(listAdapter);
    }
    
    
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
    	// TODO Auto-generated method stub
    	super.onListItemClick(l, v, position, id);
    	
    	System.out.println(" position=" + position + "|| id=" + id);
    	
    }

	//长按事件
        listView.setOnItemLongClickListener(new OnItemLongClickListener() {
        	public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
        			int arg2, long arg3) {
        		// TODO Auto-generated method stub
        		Builder builder;  
        		builder = new Builder(LoginIndexActivity.this);  
                // builder.setIcon(R.drawable.ic_launcher);  
                 builder.setMessage("想要删除该条信息?");  
                 builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {     
                        public void onClick(DialogInterface dialog, int id) {    
                              
                            //BroadActivity.this.onCreate(null) ;  //相当于把当前activity 重新刷新  
                          
                          
                        }     
                  }); 
                 builder.setNegativeButton("取消", null);  
                 builder.create().show();  
                 
                 return false;
                 //\\
        	}
		});
		//\\
    
    
    
    
    
}

获取 listview 列表每行数据

ArrayList<String> listArray = new ArrayList<String>();// listView data

java.util.Iterator it1 = listItem.iterator();			
			final String[] getValue = new String[20];	// 返回配置值 数组 0 -9			
			int f = 0;
			while (it1.hasNext()) {
				Object value = it1.next();
				String value_str = value.toString();
				Pattern pattern;
				Matcher matcher;
				pattern = Pattern.compile("ItemValue=(.*?),");
				matcher = pattern.matcher(value_str);
				matcher.find();
				getValue[f] = matcher.group(1); 
				f++;				
			}

////

ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String,Object>>();

相关推荐