单击列表视图项目时应用程序崩溃
我正在开发在列表视图中显示所有联系人的应用程序,现在我想编辑联系人,我在 setOnItemClickListener 事件上编写了代码,以便获得联系人详细信息。我编写了以下代码。每当我单击列表视图项目时,我的应用程序就会崩溃,请帮助我。请指导我如何更新联系方式。如果我朝错误的方向移动,请指导我。
代码:
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 info regarding contact..
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
ContactAt(position);
}
});
}
但是每当我单击列表视图项目时,我的应用程序就会崩溃,请帮助我。
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 info 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下是如何使用 ArrayAdapter 的说明:
http://sudarmuthu。 com/blog/using-arrayadapter-and-listview-in-android-applications
如您所见,您可以使用 getItem(position) 来检索注册了单击事件的元素。
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.