开发者

apps crashes when listview item is clicked

开发者 https://www.devze.com 2023-02-16 03:55 出处:网络
I\'m working on application which is showing all contacts of in list view, now i want to edit the contacts, i wrote code on setOnItemClickListener event so that i will get contact details. i have writ

I'm working on application which is showing all contacts of in list view, now i want to edit the contacts, i wrote code on setOnItemClickListener event so that i will get contact details. i have written following code.My application is crashing whenever i clicked on listview item please help me.Please guide me what to do to update contact detail.If i am moving in wrong direction please guide me.

code:

super.onCreate(savedInstanceState);
        setContentView(R.layout.main); 
        lv=(ListView)findViewById(R.id.listview);
        TVContactText=(TextView)findViewById(R.id.contactEntryText);
        btnAddContact=(Button)findViewById(R.id.addContactButton);

        ArrayList<String> names = new ArrayList<String>();

        cur=getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        int a=cur.getCount();
        if(cur.moveToFirst())
        {
        do{
            int nameidx=cur.getColumnIndex(PhoneLookup.DISPLAY_NAME);
                int Ididx=cur.getColumnIndex(PhoneLookup.NUMBER);
            String strName=cur.getString(nameidx);
            names.add(strName);                 
           }while(cur.moveToNext());

        cur.close();
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, R.layout.list_view_item_new, R.id.contactEntryText,names);
        lv.setAdapter(adapter); 
        registerForContextMenu(lv);
        } 
       btnAddContact.setOnClickListener(new View.OnClickListener()
       {
           public void onClick(View v)
          {
              launchContactAdder();
            }
       });
          //i want to get i开发者_JAVA技巧nfo regarding contact..
       lv.setOnItemClickListener(new OnItemClickListener() 
       {          
        public void onItemClick(AdapterView<?> parent, View view, int position, long id)
        {           
            ContactAt(position);  

         }   
        }); 

    }

but my application is crashing whenever i clicked on listview item please help me.


Here is an explanation how to use ArrayAdapter:

http://sudarmuthu.com/blog/using-arrayadapter-and-listview-in-android-applications

As you can see, you can use getItem(position) to retrieve the element on which the click event was registered.

0

精彩评论

暂无评论...
验证码 换一张
取 消