我有自定义Listview。 有两个文本框和一个复选框。 也添加了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方法不起作用,因为当我运行项目时,我无法点击任何项目。