English 中文(简体)
名单见文
原标题:Android Contacts on listview

在我的申请中,我想在一份清单中增加选定的接触。 我正在按照守则选用守则,但没有在名单表上显示某些接触。 请帮助我,我是错做什么,什么是错做的。

public class Contacts extends Activity implements View.OnClickListener {
    private Button AddNewContact;
private Button AddFrmContacts;
private ListView EmergencyContactList;
private final int PICK = 1001;
private final int NEW = 1002;
ArrayList<ContactDetails> ContactList = new ArrayList<ContactDetails>();
ContactDetails CD = new ContactDetails(null, null);
ContactsAdapter contactsAdapter = null;
String[] ContactsArray = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add_contacts);
    EmergencyContactList = (ListView) findViewById(R.id.emergency_contact_list);
    ContactsArray = new String[ContactList.size()]; 
    contactsAdapter = new ContactsAdapter(Contacts.this, R.layout.contacts_listitem,ContactsArray);
    EmergencyContactList.setAdapter(contactsAdapter);
    AddNewContact = (Button) findViewById(R.id.add_new);
    AddFrmContacts = (Button) findViewById(R.id.from_contacts);

    EmergencyContactList.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // TODO Auto-generated method stub
            Log.v("Item position :",
                    EmergencyContactList.getSelectedItemPosition() + "");
        }
    });
    AddFrmContacts.setOnClickListener(this);
    AddNewContact.setOnClickListener(this);
}

@Override
public void onResume() {
    super.onResume();
    Log.v("contactlist on resume :", ContactList.isEmpty() + " "
            + ContactList.size());
    if (!ContactList.isEmpty()) {
        Log.v("name on resume :", ContactList.get(0).Name);
        Log.v("name on resume :", ContactList.get(0).Number);
        ContactsArray = getContactsArray(ContactList);
        contactsAdapter = new ContactsAdapter(Contacts.this, R.layout.contacts_listitem,ContactsArray);
        contactsAdapter.notifyDataSetChanged();
        EmergencyContactList.setAdapter(contactsAdapter);
    }
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.add_new:
        Intent NewContactIntent = new Intent(Contacts.this,
                SOSAddNewContact.class);
        startActivityForResult(NewContactIntent, NEW);
        break;
    case R.id.from_contacts:
        Intent intent = new Intent(Intent.ACTION_PICK,
                ContactsContract.Contacts.CONTENT_URI);
        // calling OnActivityResult with intenet And Some conatct for
        // Identifie
        startActivityForResult(intent, PICK);
        break;
    }
}

@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
    super.onActivityResult(reqCode, resultCode, data);
    String name = null;
    String PhoneNumber = "";
    switch (reqCode) {
    case (PICK):
        Uri contactData = data.getData();
        Cursor c = managedQuery(contactData, null, null, null, null);
        if (c.moveToFirst()) {
            name = c.getString(c
                    .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            Log.v("_ID", name);
            int HasNumber = c
                    .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
            if (HasNumber > 0) {
                Uri result = data.getData();
                Log.v("TAG", "Got a contact result: " + result.toString());
                String id = result.getLastPathSegment();
                try {
                    c = getContentResolver().query(Phone.CONTENT_URI, null,
                            Phone.CONTACT_ID + "=?", new String[] { id },
                            null);
                    int contactnumberidx = c.getColumnIndex(Phone.DATA);
                    if (c.moveToFirst()) {
                        PhoneNumber = c.getString(contactnumberidx);
                        Log.v("TAG", "Got number: " + PhoneNumber);
                        CD = new ContactDetails(name, PhoneNumber);
                        ContactList.add(CD);
                    } else {
                        Log.w("TAG", "No results");
                    }
                } catch (Exception e) {
                    Log.e("TAG", "Failed to get phone data", e);
                } finally {
                    if (c != null) {
                        c.close();
                    }
                }
            }
        }
        break;
    case NEW :
        if(resultCode == Activity.RESULT_OK)
            Log.v("on activity result", "OK");
        else
            Log.v("result", "not OK");
        Bundle extras = data.getExtras();
        if(extras != null){
        name = extras.getString("Name");
        PhoneNumber = extras.getString("Number");
        CD = new ContactDetails(name, PhoneNumber);
        ContactList.add(CD);
        for(int i = 0;i<ContactList.size();i++)
            Log.v("ContactList", i+" "+ContactList.get(i).Name);
        }else{
            Log.v("extras ", "NULL");
        }
        break;
    }
}

public String[] getContactsArray(ArrayList<ContactDetails> List){
    if(!List.isEmpty()){
        for(int i=0;i<=List.size()-1;i++){
            ContactsArray = new String[List.size()];
            ContactsArray[i] = List.get(i).Name +"
"+ List.get(i).Number;
        }
    }
    return ContactsArray;
}
}

<<>LOGCAT

01-16 17:23:50.954: I/InputReader(61): Device reconfigured: id=0x0, name=qwerty2, display size is now 320x480
01-16 17:23:50.954: I/InputManager-Callbacks(61): No virtual keys found for device qwerty2.
01-16 17:23:51.379: I/ARMAssembler(61): generated scanline__00000077:03515104_00001004_00000000 [ 65 ipp] (85 ins) at [0x4437d520:0x4437d674] in 908313 ns
01-16 17:23:51.399: I/ARMAssembler(61): generated scanline__00000177:03515104_00001001_00000000 [ 91 ipp] (114 ins) at [0x4437d678:0x4437d840] in 707349 ns
01-16 17:23:52.950: I/ActivityManager(61): Starting: Intent { cmp=com.sos.emergency/.SOSAddContacts } from pid 386
01-16 17:23:53.080: V/contactlist on resume :(386): true 0
01-16 17:23:53.389: I/ActivityManager(61): Displayed com.sos.emergency/.Contacts: +415ms
01-16 17:23:54.040: I/ActivityManager(61): Starting: Intent { cmp=com.sos.emergency/.SOSAddNewContact } from pid 386
01-16 17:23:54.429: I/ActivityManager(61): Displayed com.sos.emergency/.SOSAddNewContact: +374ms
01-16 17:24:05.449: W/KeyCharacterMap(386): No keyboard for id 0
01-16 17:24:05.449: W/KeyCharacterMap(386): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
01-16 17:24:13.729: D/dalvikvm(61): GREF has increased to 401
01-16 17:24:24.400: V/intent data(386): abc 5555555555
01-16 17:24:24.450: V/on activity result(386): OK
01-16 17:24:24.450: V/ContactList(386): 0 abc
01-16 17:24:24.450: V/contactlist on resume :(386): false 1
01-16 17:24:24.459: V/name on resume :(386): abc
01-16 17:24:24.459: V/name on resume :(386): 5555555555
01-16 17:24:26.850: I/ActivityManager(61): Starting: Intent { act=android.intent.action.PICK dat=content://com.android.contacts/contacts cmp=com.android.contacts/.ContactsListActivity } from pid 386
01-16 17:24:27.230: D/dalvikvm(180): GC_CONCURRENT freed 482K, 49% free 3050K/5959K, external 716K/1038K, paused 4ms+5ms
01-16 17:24:27.400: I/ContactsListActivity(180): Called with action: android.intent.action.PICK
01-16 17:24:28.299: I/ActivityManager(61): Displayed com.android.contacts/.ContactsListActivity: +1s431ms
01-16 17:24:30.419: V/_ID(386): Jordan
01-16 17:24:30.419: V/TAG(386): Got a contact result: content://com.android.contacts/contacts/lookup/0r4-3F494F332D47/4
01-16 17:24:30.480: V/TAG(386): Got number: 845-435-3555
01-16 17:24:30.489: V/contactlist on resume :(386): false 2
01-16 17:24:30.489: V/name on resume :(386): abc
01-16 17:24:30.489: V/name on resume :(386): 5555555555
01-16 17:24:30.550: W/InputManagerService(61): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@4050be88 (uid=10000 pid=180)
01-16 17:24:39.319: I/ActivityManager(61): Starting: Intent { act=android.intent.action.PICK dat=content://com.android.contacts/contacts cmp=com.android.contacts/.ContactsListActivity } from pid 386
01-16 17:24:39.360: I/ContactsListActivity(180): Called with action: android.intent.action.PICK
01-16 17:24:39.689: D/dalvikvm(180): GC_CONCURRENT freed 333K, 47% free 3238K/6023K, external 914K/1038K, paused 5ms+6ms
01-16 17:24:39.979: I/ActivityManager(61): Displayed com.android.contacts/.ContactsListActivity: +643ms
01-16 17:24:41.269: V/_ID(386): Akriti
01-16 17:24:41.269: V/TAG(386): Got a contact result: content://com.android.contacts/contacts/lookup/0r3-2D414F3D533D/3
01-16 17:24:41.309: V/TAG(386): Got number: 854-741-1111
01-16 17:24:41.319: V/contactlist on resume :(386): false 3
01-16 17:24:41.330: V/name on resume :(386): abc
01-16 17:24:41.330: V/name on resume :(386): 5555555555
01-16 17:24:41.389: W/InputManagerService(61): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@4050be88 (uid=10000 pid=180)
01-16 17:25:00.189: D/dalvikvm(130): GC_CONCURRENT freed 246K, 48% free 2970K/5703K, external 956K/1038K, paused 5ms+6ms
最佳回答

3. 改变您目前的适应类别:

public class ContactsAdapter extends BaseAdapter {

    ArrayList<ContactDetails> contactDetails = new ArrayList<ContactDetails>();
    Context c;    
    LayoutInflater mInflater;

    public ContactsAdapter(Context context,ArrayList<ContactDetails> ContactDetails) {

        contactDetails = ContactDetails;        
        c=context;
        mInflater = LayoutInflater.from(context);  
        Log.v("constructor", contactDetails.size()+"");
    }

    @Override
    public int getCount() {

        Log.v("getCount", "");
        return contactDetails.size();
    }
    @Override
    public Object getItem(int position) {

        Log.v("getItem", position+" ");
        return contactDetails.get(position);
    }
    @Override
    public long getItemId(int position) {

        Log.v("getItemId", position+"");
        return position;
    }
    @Override
    public View getView(int position, View convertView,ViewGroup parent) {

        final ViewHolder holder;
        Log.v("in :","get View");        

        if (convertView == null)
        {            
            convertView = mInflater.inflate(R.layout.contacts_listitem, null);
            holder = new ViewHolder();

            holder.text=(TextView)convertView.findViewById(R.id.text);
            convertView.setTag(holder);

            Log.v("TAG", "convert view not null");
        } 
        else       
            holder=(ViewHolder)convertView.getTag();

        textview.setText(contactDetails.get(position).Name);

        return convertView;
    }
    static class ViewHolder
    {
        TextView text;            
    }
}

and also include layout file named contacts_listitem.xml as shown below in my answer.

现在,你也需要在你的主要活动中改变这些思路:

...
contactsAdapter = new ContactsAdapter(Contacts.this,ContactList);
EmergencyContactList.setAdapter(contactsAdapter);
...

Here,also you have not set adapter to your listview so it is not showing you anything there.you missed the line EmergencyContactList.setAdapter(contactsAdapter);. please do as above and let me know the result.

www.un.org/Depts/DGACM/index_russian.htm

您也可以尝试使用传统“ArrayAdapter”类:

ContactsAdapter.class:

public class ContactsAdapter extends ArrayAdapter<String> {

    String[] array;
    LayoutInflater mInflater;

    public ContactsAdapter(Context context, int textViewResourceId,
            String[] objects)
    {
        super(context, textViewResourceId, objects);
        array=objects;
        mInflater = LayoutInflater.from(context);               
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {       
        final ViewHolder holder;

        if(convertView==null)
        {
                 convertView = mInflater.inflate(R.layout.contacts_listitem, null);
                 holder = new ViewHolder();

                 holder.text=(TextView)convertView.findViewById(R.id.text);
                 convertView.setTag(holder);
        }
        else
                holder=(ViewHolder)convertView.getTag();

        holder.text.setText(array[position]);

        return convertView;
    }

    static class ViewHolder
    {
        TextView text;            
    }
}

创建排版文件contacts_list.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:id="@+id/text"
        android:textSize="18dip"
        android:padding="5dip"      
        android:textColor="#90000000"
        android:layout_weight="0.9"
        />
</LinearLayout>

现在,你们需要在你的主要活动中使用接触器的物体,例如:

...
ContactsAdapter aa=new ContactsAdapter(context,R.layout.contacts_listitem, new String[]{"contact1","contact2","contact3","contacts4"});
listView.setAdapter(aa);
...

在这方面,你可以相应地在积极恢复的基础上建立你的阵容。 (千美元) 我拿到你在适应班子中所做的那样做,以显示接触细节,而只是从适应器的强硬阵列中 put。 你们必须按你们的需要行事。

http://www.un.org。

of

public String[] getContactsArray(ArrayList<ContactDetails> List)
{    
    ContactsArray = new String[List.size()];
    for(int i=0;i<=List.size()-1;i++)
    {           
        ContactsArray[i] = List.get(i).Name +"
"+ List.get(i).Number;
    }   
    return ContactsArray;
}

在这里,如果名单是空的,就不需要检查,因为你在要求这项职能之前已经这样做了。

问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签