Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 处理ListView中的Click事件_Android_Android Listview - Fatal编程技术网

Android 处理ListView中的Click事件

Android 处理ListView中的Click事件,android,android-listview,Android,Android Listview,我有一个列表视图,其中有20个项目。10项是我手机的联系人,其余10项是我默认浏览器的bookmars url。我想要的是: 1) 当我点击联系人(前10个)时,它会将我带到联系人应用程序中的部分联系人。 2) 当我点击书签url(其余10个)时,它应该会在浏览器中打开该url 我用以下代码实现了我的第一个要求: ArrayList<HashMap<String, Object>> listitem = new ArrayList<HashMap<Strin

我有一个列表视图,其中有20个项目。10项是我手机的联系人,其余10项是我默认浏览器的bookmars url。我想要的是: 1) 当我点击联系人(前10个)时,它会将我带到联系人应用程序中的部分联系人。 2) 当我点击书签url(其余10个)时,它应该会在浏览器中打开该url

我用以下代码实现了我的第一个要求:

 ArrayList<HashMap<String, Object>> listitem = new ArrayList<HashMap<String, Object>>();
        mSharedPrefs = PreferenceManager
                .getDefaultSharedPreferences(getApplicationContext());
        Boolean contact_check = mSharedPrefs.getBoolean("contact_check", true);
        Boolean bookmark_check = mSharedPrefs
                .getBoolean("bookmark_check", true);
        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
                null, null, null);
        if (contact_check) {
            if (cur.getCount() > 0) {
                while (cur.moveToNext()) {
                    String id = cur.getString(cur
                            .getColumnIndex(ContactsContract.Contacts._ID));
                    String name = cur
                            .getString(cur
                                    .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                    if (Integer
                            .parseInt(cur.getString(cur
                                    .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                        Cursor pCur = cr
                                .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                        null,
                                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                                + " = ?", new String[] { id },
                                        null);
                        while (pCur.moveToNext()) {
                            String phoneNo = pCur
                                    .getString(pCur
                                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                            HashMap<String, Object> map = new HashMap<String, Object>();
                            map.put("name", name);
                            map.put("phone no", phoneNo);
                            // map.put("Bookmarks", faves.getString(titleIdx));
                            listitem.add(map);

                        }
                        pCur.close();
                    }

                }

            }
        }

SimpleAdapter listitemAdapter = new SimpleAdapter(this, listitem,
                R.layout.list_style, new String[] { "name", "phone no" },
                new int[] { R.id.topTextView, R.id.bottomTextView });
        lv.setAdapter(listitemAdapter);



        lv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {

                RelativeLayout lr = (RelativeLayout) arg1;
                TextView mText = (TextView) lr.getChildAt(1);

                Intent i = new Intent(Intent.ACTION_VIEW);
                i.setAction(ContactsContract.Intents.SHOW_OR_CREATE_CONTACT);
                i.setData(Uri
                        .fromParts("tel", mText.getText().toString(), null));
                startActivity(i);

            }
        });
ArrayList listitem=new ArrayList();
mSharedPrefs=首选项管理器
.GetDefaultSharedReferences(getApplicationContext());
Boolean contact\u check=mSharedPrefs.getBoolean(“contact\u check”,true);
Boolean bookmark\u check=mSharedPrefs
.getBoolean(“书签检查”,true);
ContentResolver cr=getContentResolver();
Cursor cur=cr.query(Contacts contract.Contacts.CONTENT\u URI,null,
空,空,空);
如果(联系检查){
如果(cur.getCount()>0){
while(cur.moveToNext()){
字符串id=cur.getString(cur
.getColumnIndex(Contacts contract.Contacts._ID));
字符串名称=cur
.getString(cur)
.getColumnIndex(Contacts contract.Contacts.DISPLAY_NAME));
if(整数
.parseInt(cur.getString(cur
.getColumnIndex(Contacts contract.Contacts.HAS_PHONE_NUMBER))>0){
光标pCur=cr
.query(contacts contract.commondatatypes.Phone.CONTENT\u URI、,
无效的
Contacts contract.CommonDataTypes.Phone.CONTACT\u ID
+“=?”,新字符串[]{id},
无效);
while(pCur.moveToNext()){
字符串phoneNo=pCur
.getString(pCur)
.getColumnIndex(ContactsContract.CommonDataTypes.Phone.NUMBER));
HashMap=newHashMap();
地图。放置(“名称”,名称);
地图放置(“电话号码”,电话号码);
//map.put(“Bookmarks”,faves.getString(titleIdx));
添加(地图);
}
pCur.close();
}
}
}
}
SimpleAdapter listitemAdapter=新SimpleAdapter(此,listitem,
R.layout.list_样式,新字符串[]{“name”,“phone no”},
新的int[]{R.id.topTextView,R.id.bottomTextView});
lv.setAdapter(listitemAdapter);
lv.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
公共链接(AdapterView arg0、视图arg1、内部arg2、,
长arg3){
RelativeLayout lr=(RelativeLayout)arg1;
TextView mText=(TextView)lr.getChildAt(1);
意向i=新意向(意向.行动\视图);
i、 设置动作(contacts contract.Intents.SHOW\u或\u CREATE\u CONTACT);
i、 设置数据(Uri)
.fromParts(“tel”,mText.getText().toString(),null));
星触觉(i);
}
});
上面的代码是在我的listview中添加联系人来处理click事件

对于第二个要求,我使用书签扩展了listview,代码如下:

字符串[]requestedColumns={Browser.BookmarkColumns.TITLE, Browser.bookmark columns.URL}

@SuppressWarnings("deprecation")
final Cursor faves = managedQuery(Browser.BOOKMARKS_URI, requestedColumns,
        Browser.BookmarkColumns.BOOKMARK + "=1", null, null);
Log.d("Bookmarks", "Bookmarks count: " + faves.getCount());
faves.moveToFirst();
int titleIdx = faves.getColumnIndex(Browser.BookmarkColumns.TITLE);
final int urlIdx = faves.getColumnIndex(Browser.BookmarkColumns.URL);

if (bookmark_check) {
    while (!faves.isAfterLast()) {
        Log.d("SimpleBookmarks", faves.getString(titleIdx));
        Log.d("SimpleBookmarks url", " " + faves.getString(urlIdx));
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("name", faves.getString(titleIdx));
        map.put("phone no", faves.getString(urlIdx));
        listitem.add(map);
        faves.moveToNext();
    }
@SuppressWarnings(“弃用”)
最终光标faves=managedQuery(Browser.BOOKMARKS\u URI,requestedColumns,
Browser.BookmarkColumns.BOOKMARK+“=1”,null,null);
Log.d(“Bookmarks”,“Bookmarks count:+faves.getCount());
faves.moveToFirst();
int titleIdx=faves.getColumnIndex(Browser.BookmarkColumns.TITLE);
final int urlIdx=faves.getColumnIndex(Browser.BookmarkColumns.URL);
如果(书签检查){
而(!faves.isAfterLast()){
Log.d(“SimpleBookmarks”,faves.getString(titleIdx));
Log.d(“SimpleBookMarksURL”,“faves.getString(urlIdx));
HashMap=newHashMap();
map.put(“name”,faves.getString(titleIdx));
map.put(“电话号码”,faves.getString(urlIdx));
添加(地图);
faves.moveToNext();
}

正在填充该url,但我无法处理其单击事件。我希望在用户单击该url时在浏览器中打开该url。

对您的方法进行了一些调整:

    lv.setOnItemClickListener(new OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
        {
            if (arg2 < 10)
            {
                RelativeLayout lr = (RelativeLayout) arg1;
                TextView mText = (TextView) lr.getChildAt(1);

                Intent i = new Intent(Intent.ACTION_VIEW);
                i.setAction(ContactsContract.Intents.SHOW_OR_CREATE_CONTACT);
                i.setData(Uri.fromParts("tel", mText.getText().toString(), null));
                startActivity(i);
            }
            else
            {
                Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(getCurrentUri));
                //getCurrentUri is the uri at position arg2-10
                startActivity(browserIntent);
            }

        }
    });
lv.setOnItemClickListener(新的OnItemClickListener()
{
@凌驾
公共视图单击(AdapterView arg0、视图arg1、整型arg2、长型arg3)
{
如果(arg2<10)
{
RelativeLayout lr=(RelativeLayout)arg1;
TextView mText=(TextView)lr.getChildAt(1);
意向i=新意向(意向.行动\视图);
i、 设置动作(contacts contract.Intents.SHOW\u或\u CREATE\u CONTACT);
i、 setData(Uri.fromParts(“tel”,mText.getText().toString(),null));
星触觉(i);
}
其他的
{
Intent browserIntent=新的Intent(Intent.ACTION_视图,Uri.parse(getCurrentUri));
//getCurrentUri是位于arg2-10位置的uri
startActivity(浏览器内容);
}
}
});

对您的方法进行一些调整:

    lv.setOnItemClickListener(new OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
        {
            if (arg2 < 10)
            {
                RelativeLayout lr = (RelativeLayout) arg1;
                TextView mText = (TextView) lr.getChildAt(1);

                Intent i = new Intent(Intent.ACTION_VIEW);
                i.setAction(ContactsContract.Intents.SHOW_OR_CREATE_CONTACT);
                i.setData(Uri.fromParts("tel", mText.getText().toString(), null));
                startActivity(i);
            }
            else
            {
                Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(getCurrentUri));
                //getCurrentUri is the uri at position arg2-10
                startActivity(browserIntent);
            }

        }
    });
lv.setOnItemClickListener(新的OnItemClickListener()
{
@凌驾
公共视图单击(AdapterView arg0、视图arg1、整型arg2、长型arg3)
{
如果(arg2<10)
{
相对论