点击监听器上的ListView [英] Click Listener on ListView
问题描述
我已经修改了这个例子从SDK页面抓住所有的联络小组从手机并显示出来。我不能,但是,弄清楚如何点击其中一个,然后使用Groups._ID做别的事情。有人能教我如何获得一个点击/选择监听这个名单上?
MyListAdapter.java
包com.example.simplelist;
进口android.app.ListActivity;
进口android.database.Cursor;
进口android.os.Bundle;
进口android.provider.Contacts.Groups;
进口android.widget.ListAdapter;
进口android.widget.SimpleCursorAdapter;
公共类MyListAdapter扩展ListActivity {
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.custom_list_activity_view);
光标mCursor = this.getContentResolver()查询(Groups.CONTENT_URI,NULL,NULL,NULL,NULL);
startManagingCursor(mCursor);
ListAdapter适配器=新SimpleCursorAdapter(这一点,android.R.layout.two_line_list_item,mCursor,新的String [] {Groups.NAME,Groups._ID},新的INT [] {android.R.id.text1,android.R.id .text2});
setListAdapter(适配器);
}
}
custom_list_activity_view.xml
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:方向=垂直
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:以下属性来=8DP
机器人:paddingRight =8DP>
< ListView的机器人:ID =@ ID / Android的:清单
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:后台=#00FF00
机器人:layout_weight =1
机器人:drawSelectorOnTop =假的Android版本:choiceMode =singleChoice机器人:可点击=真/>
< TextView的机器人:ID =@ ID /安卓:空
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:后台=#FF0000
机器人:文本=无数据/>
< / LinearLayout中>
two_line_list_item.xml
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:方向=垂直>
< TextView的机器人:ID =@ + ID / text1中
机器人:TEXTSIZE =16SP
机器人:TEXTSTYLE =黑体
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT/>
< TextView的机器人:ID =@ + ID /文本2
机器人:TEXTSIZE =16SP
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT/>
< / LinearLayout中>
AndroidManifest.xml中
< XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=com.example.simplelist
安卓版code =1
机器人:VERSIONNAME =1.0>
<使用-SDK安卓的minSdkVersion =3/>
<应用机器人:图标=@可绘制/图标机器人:标签=@字符串/ APP_NAME>
<活动机器人:名称=。MyListAdapter
机器人:标签=@字符串/ APP_NAME>
<意向滤光器>
<作用机器人:名称=android.intent.action.MAIN/>
<类机器人:名称=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
< /用途>
<使用-权限的Android:名称=android.permission.READ_CONTACTS/>
< /舱单>
这应该允许您选择的项目进行互动,你想要的方式。
MyListAdapter.java
包com.example.simplelist;
进口的java.util.ArrayList;
进口的java.util.HashMap;
进口的java.util.List;
进口的java.util.Map;
进口java.util.regex.Matcher中;
进口java.util.regex.Pattern中;
进口android.app.ListActivity;
进口android.content.ContentResolver;
进口android.content.Context;
进口android.database.Cursor;
进口android.os.Bundle;
进口android.provider.Contacts.Groups;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.BaseAdapter;
进口android.widget.ListView;
进口android.widget.SimpleAdapter;
进口android.widget.TextView;
进口android.widget.Toast;
公共类MyListAdapter扩展ListActivity {
名单< ContactGroup>组=新的ArrayList< ContactGroup>();
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
this.groups = getContacts();
ContactGroupAdapter cAdapter =新ContactGroupAdapter(本);
setListAdapter(cAdapter);
}
私人列表< ContactGroup> getContacts(){
名单< ContactGroup> GRPS =新的ArrayList< ContactGroup>();
ContentResolver的CR = getContentResolver();
光标groupCur = cr.query(Groups.CONTENT_URI,NULL,NULL,NULL,NULL);
如果(groupCur.getCount()大于0){
而(groupCur.moveToNext()){
ContactGroup新闻组=新ContactGroup();
newGroup.Id = groupCur.getString(groupCur.getColumnIndex(Groups._ID));
newGroup.Name = groupCur.getString(groupCur.getColumnIndex(Groups.NAME));
grps.add(新闻组);
}
}
返回GRPS;
}
@覆盖
保护无效onListItemClick(ListView的L,视图V,INT位置,长的id){
ContactGroup selectedGroup =新ContactGroup();
selectedGroup = groups.get(位置);
Toast.makeText(getBaseContext(),selectedGroup.Name +编号#+ selectedGroup.Id,1).show();
}
公共类ContactGroupAdapter扩展了BaseAdapter {
公共ContactGroupAdapter(上下文C){
mContext = C;
}
@覆盖
公众诠释getCount将(){
返回groups.size();
}
@覆盖
公共对象的getItem(INT位置){
返回的位置;
}
@覆盖
众长getItemId(INT位置){
返回的位置;
}
@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
ViewHolder持有人;
如果(convertView == NULL){
LayoutInflater VI = LayoutInflater.from(this.mContext);
convertView = vi.inflate(R.layout.two_line_list_item,NULL);
持有人=新ViewHolder();
convertView.setTag(保持器);
}
其他 {
//获取观点持有者回
支架=(ViewHolder)convertView.getTag();
}
ContactGroup C = groups.get(位置);
如果(C!= NULL){
//名称
holder.toptext =(TextView中)convertView.findViewById(R.id.text1);
holder.toptext.setText(c.Name);
// ID
holder.bottomtext =(TextView中)convertView.findViewById(R.id.text2);
holder.bottomtext.setText(c.Id);
}
返回convertView;
}
私人语境mContext;
}
公共静态类ViewHolder {
TextView中的TopText;
TextView的bottomtext;
}
公共类ContactGroup {
公共字符串ID;
公共字符串名称;
}
}
two_line_list_item.xml
< XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout机器人:ID =@ + ID / LinearLayout01机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT的xmlns:机器人=http://schemas.android.com/apk/res/android>
< TextView的机器人:ID =@ + ID / text1中
机器人:TEXTSIZE =16SP
机器人:TEXTSTYLE =黑体
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT/>
< TextView的机器人:ID =@ + ID /文本2
机器人:TEXTSIZE =16SP
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT/>
< / LinearLayout中>
I have modified this example from the SDK pages to grab all the Contact Groups from the phone and display them. I cannot, however, figure out how to click one of them and then use the Groups._ID to do something else. Can someone teach me how to get a click/select listener on this list?
MyListAdapter.java
package com.example.simplelist;
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts.Groups;
import android.widget.ListAdapter;
import android.widget.SimpleCursorAdapter;
public class MyListAdapter extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_list_activity_view);
Cursor mCursor = this.getContentResolver().query(Groups.CONTENT_URI, null, null, null, null);
startManagingCursor(mCursor);
ListAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.two_line_list_item, mCursor, new String[] {Groups.NAME, Groups._ID}, new int[] {android.R.id.text1, android.R.id.text2});
setListAdapter(adapter);
}
}
custom_list_activity_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp">
<ListView android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00FF00"
android:layout_weight="1"
android:drawSelectorOnTop="false" android:choiceMode="singleChoice" android:clickable="true" />
<TextView android:id="@id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF0000"
android:text="No data"/>
</LinearLayout>
two_line_list_item.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="wrap_content"
android:orientation="vertical">
<TextView android:id="@+id/text1"
android:textSize="16sp"
android:textStyle="bold"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/text2"
android:textSize="16sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.simplelist"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="3" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MyListAdapter"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.READ_CONTACTS" />
</manifest>
This should allow you to interact with selected item the way you want.
MyListAdapter.java
package com.example.simplelist;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.app.ListActivity;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts.Groups;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class MyListAdapter extends ListActivity {
List<ContactGroup> groups = new ArrayList<ContactGroup>();
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.groups = getContacts();
ContactGroupAdapter cAdapter = new ContactGroupAdapter(this);
setListAdapter(cAdapter);
}
private List<ContactGroup> getContacts(){
List<ContactGroup> grps = new ArrayList<ContactGroup>();
ContentResolver cr = getContentResolver();
Cursor groupCur = cr.query(Groups.CONTENT_URI, null, null, null, null);
if (groupCur.getCount() > 0) {
while (groupCur.moveToNext()) {
ContactGroup newGroup = new ContactGroup();
newGroup.Id = groupCur.getString(groupCur.getColumnIndex(Groups._ID));
newGroup.Name = groupCur.getString(groupCur.getColumnIndex(Groups.NAME));
grps.add(newGroup);
}
}
return grps;
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
ContactGroup selectedGroup = new ContactGroup();
selectedGroup = groups.get(position);
Toast.makeText(getBaseContext(), selectedGroup.Name + " ID #" + selectedGroup.Id, 1).show();
}
public class ContactGroupAdapter extends BaseAdapter{
public ContactGroupAdapter(Context c) {
mContext = c;
}
@Override
public int getCount() {
return groups.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
LayoutInflater vi = LayoutInflater.from(this.mContext);
convertView = vi.inflate(R.layout.two_line_list_item, null);
holder = new ViewHolder();
convertView.setTag(holder);
}
else {
//Get view holder back
holder = (ViewHolder) convertView.getTag();
}
ContactGroup c = groups.get(position);
if (c != null) {
//Name
holder.toptext = (TextView) convertView.findViewById(R.id.text1);
holder.toptext.setText(c.Name);
//ID
holder.bottomtext = (TextView) convertView.findViewById(R.id.text2);
holder.bottomtext.setText(c.Id);
}
return convertView;
}
private Context mContext;
}
public static class ViewHolder {
TextView toptext;
TextView bottomtext;
}
public class ContactGroup{
public String Id;
public String Name;
}
}
two_line_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:id="@+id/text1"
android:textSize="16sp"
android:textStyle="bold"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/text2"
android:textSize="16sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
这篇关于点击监听器上的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!