我在android studio中有一个带有联系人子项的用户名列表,我只想访问列表中的联系人,但是我不能这样做,因为我的自定义列表适配器将项目和子项都保存为一个值。如何仅从列表中获取没有用户名的联系人?
模型类
public class Contact {
String contactName;
String contactNum;
String id;
public Contact() {
}
public Contact(String contactName,String id,String contactNum) {
this.contactName=contactName;
this.contactNum = contactNum;
this.id = id;
}
public Contact(String contactNum) {
this.contactNum = contactNum;
}
public String getContactName() {
return contactName;
}
public String getContactNum() {
return contactNum;
}
}
主要活动
public class MainActivity extends AppCompatActivity {
EditText editTextName, editTextContact;
Button buttonAdd, prevAc;
ListView listViewContacts;
List<Contact> contactList;
List<Contact> numList;
TextView textViewContact;
DatabaseReference databaseArtists;
ArrayList arrayList = new ArrayList();
Number number = new Number();
String name;
View v;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
databaseArtists = FirebaseDatabase.getInstance().getReference("Trusted Contacts");
editTextName = findViewById(R.id.editTextName);
editTextContact = findViewById(R.id.editTextContact);
prevAc = findViewById(R.id.prevAc);
buttonAdd = findViewById(R.id.buttonAddContact);
listViewContacts = findViewById(R.id.listViewContacts);
contactList = new ArrayList<>();
numList = new ArrayList<>();
v = getLayoutInflater().inflate(R.layout.list_layout, null);
textViewContact = v.findViewById(R.id.value1);
prevAc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Contact contact1 = new Contact();
for(int i=0;i<listViewContacts.getAdapter().getCount();i++){
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel: " + listViewContacts.getAdapter().getItem(0)));
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getApplicationContext(), "permission denied", Toast.LENGTH_LONG).show();
} else {
startActivity(callIntent);
dissConnectCall(MainActivity.this);
// timer1();
}
}
prevAc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// timer2();
}
});
}
});
buttonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addContact();
}
});
}
@Override
protected void onStart() {
super.onStart();
databaseArtists.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
contactList.clear();
for (DataSnapshot artistSnapshot : dataSnapshot.getChildren()) {
Contact contact = artistSnapshot.getValue(Contact.class);
contactList.add(contact);
}
ArtistList adapter = new ArtistList(MainActivity.this, contactList);
listViewContacts.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
private void addContact() {
String name = editTextName.getText().toString();
String contact = editTextContact.getText().toString();
if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(contact)) {
if (contact.length() == 11) {
String id = databaseArtists.push().getKey();
Contact contact1 = new Contact(name,id,contact);
databaseArtists.child(id).setValue(contact1);
Toast.makeText(MainActivity.this, "Contact added", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this, "Contact Number should be of 11 digits", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(MainActivity.this, "Fields cannot be empty", Toast.LENGTH_LONG).show();
}
}
public boolean dissConnectCall(final Context activity) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
try {
TelephonyManager telephonyManager = (TelephonyManager) activity.getSystemService(Context.TELEPHONY_SERVICE);
Class classTelephony = Class.forName(telephonyManager.getClass().getName());
Method methodGetITelephony = classTelephony.getDeclaredMethod("getITelephony");
methodGetITelephony.setAccessible(true);
Object telephonyInterface = methodGetITelephony.invoke(telephonyManager);
Class telephonyInterfaceClass = Class.forName(telephonyInterface.getClass().getName());
Method methodEndCall = telephonyInterfaceClass.getDeclaredMethod("endCall");
methodEndCall.invoke(telephonyInterface);
} catch (Exception ex) { // Many things can go wrong with reflection calls
ex.printStackTrace();
return false;
}
return true;
}
}, 4000);
return false;
}
列表适配器
public class ArtistList extends ArrayAdapter<Contact> {
private Activity context;
private List<Contact> contactList;
public ArtistList(Activity context,List<Contact> contactList) {
super(context, R.layout.list_layout, contactList);
this.context = context;
this.contactList = contactList;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View listViewItem = inflater.inflate(R.layout.list_layout,null,true);
TextView textViewName = listViewItem.findViewById(R.id.title);
TextView textViewContact = listViewItem.findViewById(R.id.value1);
Contact contact = contactList.get(position);
textViewName.setText(contact.getContactName());
textViewContact.setText(contact.getContactNum());
return listViewItem;
}
}