我正在尝试制作一个拨号应用,我希望在列表视图中显示所有联系人(姓名和电话号码)。问题是每当我在列表视图上显示值时,它显示该人的电话号码不正确。例如,如果人A具有电话号码123而人B具有电话号码456,则其显示人A的电话号码456和人B的电话号码123。 以下是我的java课程:
MainActivity.java
public class MainActivity extends AppCompatActivity {
RelativeLayout rl1;
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,bhash,bStar,del,call;
EditText editText;
Intent phoneIntent,callIntent;
ListView lv;
int c=0;
List<String> name1 = new ArrayList<String>();
List<String> phno1 = new ArrayList<String>();
List<String> temp = new ArrayList<String>();
MyAdapter myAdapter;
DBHelper dbhelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//setSupportActionBar(toolbar);
dbhelper = new DBHelper(this);
// dbhelper.onCreate();
rl1=(RelativeLayout) findViewById(R.id.relativeLayout);
rl1.setVisibility(View.GONE);
editText = (EditText)findViewById(R.id.textView);
editText.setShowSoftInputOnFocus(false);
b1 = (Button)findViewById(R.id.button);
b2 = (Button)findViewById(R.id.button2);
b3 = (Button)findViewById(R.id.button3);
b4 = (Button)findViewById(R.id.button4);
b5 = (Button)findViewById(R.id.button5);
b6 = (Button)findViewById(R.id.button6);
b7 = (Button)findViewById(R.id.button7);
b8 = (Button)findViewById(R.id.button8);
b9 = (Button)findViewById(R.id.button9);
b0 = (Button)findViewById(R.id.button0);
bhash = (Button)findViewById(R.id.buttonHash);
bStar = (Button)findViewById(R.id.buttonStar);
call = (Button) findViewById(R.id.buttonCall);
del = (Button)findViewById(R.id.buttonDel);
lv= (ListView) findViewById(R.id.lv);
getAllContacts(this.getContentResolver());
myAdapter = new MyAdapter();
lv.setAdapter(myAdapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
String NamePhone = name1.get(position);
String tempName = temp.get(position);
String tempPhone = NamePhone.replace(tempName,"");
String phone = tempPhone.trim();
editText.setText(phone);
}
});
final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG).setAction("Action", null).show();
rl1.setVisibility(rl1.isShown()?View.GONE:View.VISIBLE);
c++;
if (c%2==0)
{
fab.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), android.R.drawable.ic_dialog_dialer));
}
else {
fab.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.collaspe));
}
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = editText.getSelectionStart();
String str = editText.getText().toString();
String str1 = (String) str.substring(0, pos);
String str2 = (String) str.substring(pos);
editText.setText(str1+"1" +str2);
int pos1 = pos+1;
editText.setSelection(pos1);
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = editText.getSelectionStart();
String str = editText.getText().toString();
String str1 = (String) str.substring(0, pos);
String str2 = (String) str.substring(pos);
editText.setText(str1+"2"+str2);
int pos1 = pos+1;
editText.setSelection(pos1);
}
});
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = editText.getSelectionStart();
String str = editText.getText().toString();
String str1 = (String) str.substring(0, pos);
String str2 = (String) str.substring(pos);
editText.setText(str1+"3" +str2);
int pos1 = pos+1;
editText.setSelection(pos1);
}
});
b4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = editText.getSelectionStart();
String str = editText.getText().toString();
String str1 = (String) str.substring(0, pos);
String str2 = (String) str.substring(pos);
editText.setText(str1+"4" +str2);
int pos1 = pos+1;
editText.setSelection(pos1);
}
});
b5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = editText.getSelectionStart();
String str = editText.getText().toString();
String str1 = (String) str.substring(0, pos);
String str2 = (String) str.substring(pos);
editText.setText(str1+"5" +str2);
int pos1 = pos+1;
editText.setSelection(pos1);
}
});
b6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = editText.getSelectionStart();
String str = editText.getText().toString();
String str1 = (String) str.substring(0, pos);
String str2 = (String) str.substring(pos);
editText.setText(str1+"6" +str2);
int pos1 = pos+1;
editText.setSelection(pos1);
}
});
b7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = editText.getSelectionStart();
String str = editText.getText().toString();
String str1 = (String) str.substring(0, pos);
String str2 = (String) str.substring(pos);
editText.setText(str1+"7" +str2);
int pos1 = pos+1;
editText.setSelection(pos1);
}
});
b8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = editText.getSelectionStart();
String str = editText.getText().toString();
String str1 = (String) str.substring(0, pos);
String str2 = (String) str.substring(pos);
editText.setText(str1+"8" +str2);
int pos1 = pos+1;
editText.setSelection(pos1);
}
});
b9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = editText.getSelectionStart();
String str = editText.getText().toString();
String str1 = (String) str.substring(0, pos);
String str2 = (String) str.substring(pos);
editText.setText(str1+"9" +str2);
int pos1 = pos+1;
editText.setSelection(pos1);
}
});
b0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = editText.getSelectionStart();
String str = editText.getText().toString();
String str1 = (String) str.substring(0, pos);
String str2 = (String) str.substring(pos);
editText.setText(str1+"0" +str2);
int pos1 = pos+1;
editText.setSelection(pos1);
}
});
bhash.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = editText.getSelectionStart();
String str = editText.getText().toString();
String str1 = (String) str.substring(0, pos);
String str2 = (String) str.substring(pos);
editText.setText(str1+"#" +str2);
int pos1 = pos+1;
editText.setSelection(pos1);
}
});
bStar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = editText.getSelectionStart();
String str = editText.getText().toString();
String str1 = (String) str.substring(0, pos);
String str2 = (String) str.substring(pos);
editText.setText(str1+"*" +str2);
int pos1 = pos+1;
editText.setSelection(pos1);
}
});
call.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String number = editText.getText().toString();
if (number.charAt(0)!='*') {
if (number.length()<=9)
{
Toast.makeText(MainActivity.this, "Please enter a valid number", Toast.LENGTH_SHORT).show();
}
else {
phoneIntent.setData(Uri.parse("tel:" + number));
startActivity(phoneIntent);
}
}
else if (number.subSequence(number.length()-1,number.length()).equals("#"))
{
number = number.substring(0, number.length() - 1);
String callInfo = "tel:" + number + Uri.encode("#");
callIntent.setData(Uri.parse(callInfo));
startActivity(callIntent);
}
}
});
del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int posElse = editText.getSelectionStart();
String text = editText.getText().toString();
if(text.length()!=0 && posElse >= 1) {
int pos = editText.getSelectionStart();
String str1 = (String) text.substring(0, pos-1);
if(str1.equals(null))
{
}
String str2 = (String) text.substring(pos);
String latest = str1+str2;
editText.setText(latest);
int pos1 = pos-1;
editText.setSelection(pos1);
}
else if ( posElse < 1)
{
int pos = editText.getSelectionStart();
String str2 = (String) text.substring(pos);
String latest = str2;
editText.setText(latest);
int pos1 = pos;
editText.setSelection(pos1);
}
}
});
del.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (del.equals(v))
{
editText.setText("");
}
return false;
}
});
}
});
}
//Get the Contacts
public void getAllContacts(ContentResolver cr) {
Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
// Cursor phones = cr.query(ContactsContract.Contacts.CONTENT_URI, null,null,null, null);
while (phones.moveToNext())
{
String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
// String NameAndPhone = name +"\n"+ phoneNumber;
//name1.add(NameAndPhone);
name1.add(name);
phno1.add(phoneNumber);
//temp.add(name);
}
phones.close();
}
public class MyAdapter extends BaseAdapter
{
private SparseBooleanArray mCheckStates;
LayoutInflater mInflater;
TextView tv1,tv;
MyAdapter()
{
mCheckStates = new SparseBooleanArray(name1.size());
mInflater = (LayoutInflater)MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return name1.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi=convertView;
if(convertView==null)
vi = mInflater.inflate(R.layout.row, null);
TextView tv = (TextView) vi.findViewById(R.id.textView1);
tv1= (TextView) vi.findViewById(R.id.textView2);
//Collections.sort(name1);
// Collections.sort(temp);
String name2 = name1.get(position);
String phone2 = phno1.get(position);
dbhelper.save(name2,phone2);
List<String> cName = dbhelper.getName();
List<String> cPh = dbhelper.getPhn();
tv.setText(cName.get(position));
tv1.setText(cPh.get(position));
/*String tempname = temp.get(position);
String name2Trimmed = name2.trim();
String tempTrimmed = tempname.trim();
SpannableString res = new SpannableString(name2Trimmed);
res.setSpan(new RelativeSizeSpan(2f), 0, tempTrimmed.length(), 0);
tv.setText(res);*/
return vi;
}
}
}
DBHelper.java
public class DBHelper extends SQLiteOpenHelper{
public static final String DATABASE = "contactsdb";
public static final int VERSION = 1;
SQLiteDatabase db;
public DBHelper(Context context) {
super(context, DATABASE, null, VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE contactsdb(name VARCHAR,contacts VARCHAR);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
public void save(String name,String phn)
{
getWritableDatabase().execSQL("INSERT INTO contactsdb"+ " VALUES('"+name+"','"+phn+"');");
}
public List<String> getName()
{
Cursor c = getReadableDatabase().rawQuery("SELECT * FROM contactsdb",null);
List<String> nameResults = new ArrayList<String>();
List<String> contactsResults = new ArrayList<String>();
int nameIndex = c.getColumnIndex("name");
int phoIndex = c.getColumnIndex("contacts");
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
nameResults.add(c.getString(nameIndex));
// contactsResults.add(c.getString(phoIndex));
}
return nameResults;
}
public List<String> getPhn()
{
Cursor c = getReadableDatabase().rawQuery("SELECT * FROM contactsdb",null);
List<String> nameResults = new ArrayList<String>();
List<String> contactsResults = new ArrayList<String>();
// int nameIndex = c.getColumnIndex("name");
int phoIndex = c.getColumnIndex("contacts");
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
//nameResults.add(c.getString(nameIndex));
contactsResults.add(c.getString(phoIndex));
}
return contactsResults;
}
}
答案 0 :(得分:0)
我认为错误在getAllContacts()
函数
用这个替换你的while循环: -
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] projection = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER};
Cursor people = getContentResolver().query(uri, projection, null, null, null);
int indexName = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
if(people.moveToFirst()) {
do {
String name = people.getString(indexName);
String number = people.getString(indexNumber);
// Do work...
} while (people.moveToNext());
}
答案 1 :(得分:0)
这是ArrayList数据Order的问题,因为您使用两个不同的arrayList来存储名称和电话号码。您应该创建如下的模型类:
public class Contact{
public String name;
public String phone;
}
然后为此创建阵列列表:
List<Contact> contactList = new ArrayList<>();
修改方法:
public void getAllContacts(ContentResolver cr) {
Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
// Cursor phones = cr.query(ContactsContract.Contacts.CONTENT_URI, null,null,null, null);
if(phones != null && phones.getCount() > 0)
{
phones.moveToFirst();
do {
String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Contact c = new Contact();
c.name = name;
c.phone = phoneNumber;
contactList.add(c);
}while (phones.moveToNext());
}
if(phones != null){
phones.close();
}
}
在适配器类中使用contactList并设置值int getview。
希望它能帮到你!