5
回答
SimpleAdapter isEnabled死活不起作用。
华为云实践训练营,热门技术免费实践!>>>   
package asm.mis;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Data;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class Item extends Activity {
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {

	    super.onCreate(savedInstanceState);
	
	    setContentView(R.layout.itemmain);    
	    
	    
	    String[] mFrom = new String[]{"name","phoneNumber","contactId","textCompany","checked"};
	    int[] mTo = new int[]{R.id.name,R.id.phoneNumber,R.id.contactId,R.id.textCompany,R.id.checked};
	    
	    List<Map<String,Object>> mList = new ArrayList<Map<String,Object>>();
	    Map<String,Object> mMap = null;
	    	
		Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
		
		while(cursor.moveToNext())
		{

			String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));		
			String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
			
			Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"="+contactId, null, null);			
			
			String phoneNumber="";
			
			if(phones.getCount()>0)
			{				
				phones.moveToFirst();
				phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));	
			}
			
			phones.close();
			
			Cursor organizations = getContentResolver().query(ContactsContract.Data.CONTENT_URI,new String[] { ContactsContract.Data._ID, ContactsContract.CommonDataKinds.Organization.COMPANY,
					ContactsContract.CommonDataKinds.Organization.TITLE },
					ContactsContract.Data.CONTACT_ID + "=?" + " AND " + Data.MIMETYPE + "='"+ ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE + "'",new String[] { contactId }, 
					null);
			String company="";
			
			if(organizations.getCount()>0)
			{
				organizations.moveToFirst();
				company = organizations.getString(organizations.getColumnIndex(ContactsContract.CommonDataKinds.Organization.COMPANY));	
			}
			
			if(phoneNumber.length()>0)
			{			
				mMap = new HashMap<String, Object>();
				mMap.put("name", name);
		    	mMap.put("phoneNumber", phoneNumber);
		    	mMap.put("contactId", contactId);
		    	mMap.put("checked", false);	
		    	mMap.put("textCompany", company);	
		    	mList.add(mMap);
		    	
		    	Log.i("test", mMap.toString());
		    	
		    	
		    	
			}
			
			organizations.close();			
		}
		
		cursor.close();
		
	    final ListView mListView = (ListView)findViewById(R.id.listView_contact);	    
	    SimpleAdapter mAdapter = new SimpleAdapter(this,mList,R.layout.item,mFrom,mTo)        
        {   
	    	@Override
			public boolean isEnabled(int position) {
				// TODO Auto-generated method stub
				@SuppressWarnings("unchecked")
				final HashMap<String,Object> map = (HashMap<String, Object>) this.getItem(position);
				Log.e("map", map.toString());
				//我预期contactId为12的这行不能操作,但运行后还是可以操作(即失效了),死活找不到原因
				if(map.get("contactId") == "12")
				{
					return false;
				}
				return super.isEnabled(position);
			}

			@Override
        	public View getView(final int position, View convertView, ViewGroup parent) {
        		View view = super.getView(position, convertView, parent);  
                @SuppressWarnings("unchecked")  
                final HashMap<String,Object> map = (HashMap<String, Object>) this.getItem(position);  
             
                CheckBox checkBox = (CheckBox)view.findViewById(R.id.checked);  
                checkBox.setChecked((Boolean) map.get("checked"));  
            
                checkBox.setOnClickListener(new View.OnClickListener() {  
                    @Override  
                    public void onClick(View view) {  
                        map.put("checked", ((CheckBox)view).isChecked());  
                    }  
                });  
                return view;
        	}
        };        
        
	    mListView.setAdapter(mAdapter);	 
	    
	    
	    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				
				@SuppressWarnings(value="unchecked")			  
				HashMap<String,Object> map = (HashMap<String, Object>) parent.getItemAtPosition(position);  
				String contactId = (String)map.get("contactId");
				
				Intent intent = new Intent();
				intent.setAction(Intent.ACTION_VIEW);
				String data = "content://com.android.contacts/contacts/"+contactId;
				Uri uri= Uri.parse(data);
				
				intent.setData(uri);
				startActivity(intent);
		       
			}
		});
	    
	}
}

 

<无标签>
举报
lvking
发帖于5年前 5回/720阅
共有5个答案 最后回答: 5年前

item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"    
    style="@style/Main_Frame">
    <CheckBox
        android:id="@+id/checked"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:checked="false"
        android:focusable="false" android:layout_alignParentLeft="true"/>

    <LinearLayout       
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:paddingLeft="8dp" android:layout_toRightOf="@id/checked" android:gravity="center_vertical">

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#cbcaca"
            android:textSize="22dp"/>
        
        <TextView
            android:id="@+id/textCompany"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:textSize="18dp"/>
        
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/imgphone"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:src="@drawable/phone"/>

            <TextView
                android:id="@+id/phoneNumber"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#cbcaca"
                android:textSize="16dp"/>

            <TextView
                android:id="@+id/contactId"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
               android:visibility="invisible"/>

        </LinearLayout>        
</LinearLayout> 

</RelativeLayout>

 

itemmain.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" > 
    <Button
        android:id="@+id/btnBacktoHome"
        style="@style/normal_title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="left|center"
        android:text="@string/back_readcontactlist" />
         
    <ImageView 
		android:layout_width="match_parent" 
		android:layout_height="wrap_content"
		android:src="@drawable/line"
		android:contentDescription="@string/seperate_line"/>
    
	<ListView
    	android:id="@+id/listView_contact"
        android:layout_width="match_parent"
        android:dividerHeight="1dp" 
        android:layout_height="0dip"
        android:layout_weight="1">
	</ListView>	
	
	<Button
         android:id="@+id/btnNext"
         style="@style/operate"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="@string/forward_next" />
                                                      
</LinearLayout>

 

@Override
   public boolean isEnabled(int position) {
    // TODO Auto-generated method stub
    @SuppressWarnings("unchecked")
    final HashMap<String,Object> map = (HashMap<String, Object>) this.getItem(position);
    Log.e("map", map.toString());
     //我预期contactId为12的这行不能操作,但运行后还是可以操作(即失效了),死活找不到原因
    if(map.get("contactId") == "12")
    {
     return false;
    }
    return super.isEnabled(position);
   }
顶部