0

我有自定义列表视图。有两个文本框和一个复选框。也添加了 XML 文件。

package com.example.phonebook;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements AdapterView.OnItemClickListener{

ListView lst;

CustomPerson p;

private static final int CAMERA_REQUEST = 1888;
ImageView imageView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.setContentView(R.layout.activity_main);

    Button buttonSms, buttonCall, buttonPhoto;


    List<CustomPerson> list = new ArrayList<CustomPerson>();

    list = GetContacts();

    lst = (ListView) findViewById(R.id.lst_person);
    buttonSms= (Button) findViewById(R.id.btnSms);
    buttonCall = (Button) findViewById(R.id.btnCall);
    buttonPhoto = (Button) findViewById(R.id.BtnPhoto);


    lst.setAdapter(new CustomAdapter(getApplicationContext(),list));

    lst.setOnItemClickListener(this);

     public void onItemClick(AdapterView<?> parent, View view, int position,       long id) {

    // I want to get name and number here
     Toast.makeText(this,"Clicked!", Toast.LENGTH_LONG).show();
}

 @SuppressLint({ "NewApi", "NewApi" }) private List<CustomPerson> GetContacts()
{
    Intent intentContact = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); 
    List<CustomPerson> contactList = new ArrayList<CustomPerson>();

    Cursor cursor =  managedQuery(intentContact.getData(), null, null, null, null);      
       while (cursor.moveToNext()) 
       {          
           String Name = "";
           String PhoneNumber = "";
           String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
           Name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)); 

           String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

           if ( hasPhone.equalsIgnoreCase("1"))
               hasPhone = "true";
           else
               hasPhone = "false" ;

           if (Boolean.parseBoolean(hasPhone)) 
           {
            Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
            while (phones.moveToNext()) 
            {
              PhoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            }
            phones.close();
           }

           contactList.add(new CustomPerson(Name, PhoneNumber));

      }          
       cursor.close();
       return contactList;
}

我有 CustomPerson 类和 CustomAdapter。

当我从列表视图中单击一个人时,我想将人的电话和号码获取到我的对象 p。但是这些 setOnItemClickListener 和 OnItemClick 方法不起作用,因为当我运行项目时,我无法单击任何项​​目。

4

1 回答 1

0

This problem I think it's for the clickable checkbox. Try to follow this similar issue with a button on a listview resolved early here:

I can't click the ListView in android?

于 2012-07-16T13:38:12.913 回答