I am trying to create a contact manager function within my android application (created using android studio. At the moment I have the contacts showing up in a list view, as seen in the code snippet:
<ImageView
android:layout_width="75dp"
android:layout_height="75dp"
android:id="@+id/ivContactImage"
android:padding="10dp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Contact Name"
android:id="@+id/contactName"
android:textColor="#040000"
android:textSize="17dp"
android:layout_marginTop="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Phone"
android:id="@+id/phoneNumber"
android:textSize="15dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email"
android:id="@+id/emailAddress"
android:textSize="15dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address"
android:id="@+id/cAddress"
android:textSize="15dp" />
</LinearLayout>
I want to sort these results by alphabetical order if possible? and implement a search bar function, where you can search all contacts on the list. I'm not quite sure where to start.
I have been following this tutorial so far - https://youtu.be/Aqzo_a6Bh4g?list=PL_PaSTBkUwk4PXlhZIe4COpwnWeCRnw2F
My main activity java code:
EditText nameTxt, phoneTxt, emailTxt, addressTxt;
ImageView contactImageimgView;
List<Contact> Contacts = new ArrayList<Contact>();
ListView contactListView;
Uri imageUri = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact_list);
nameTxt = (EditText) findViewById(R.id.txtName);
phoneTxt = (EditText) findViewById(R.id.txtPhone);
emailTxt = (EditText) findViewById(R.id.txtEmail);
addressTxt = (EditText) findViewById(R.id.txtAddress);
contactListView = (ListView) findViewById(R.id.listView);
contactImageimgView = (ImageView) findViewById(R.id.imgViewContactImage);
TabHost tabHost = (TabHost) findViewById(R.id.tabHost);
tabHost.setup();
TabHost.TabSpec tabSpec = tabHost.newTabSpec("Creator");
tabSpec.setContent(R.id.tabCreator);
tabSpec.setIndicator("add contact");
tabHost.addTab(tabSpec);
tabSpec = tabHost.newTabSpec("List");
tabSpec.setContent(R.id.tabContactList);
tabSpec.setIndicator("contact list");
tabHost.addTab(tabSpec);
final Button addBtn = (Button) findViewById(R.id.btnAdd);
addBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Contacts.add(new Contact(nameTxt.getText().toString(), phoneTxt.getText().toString(), emailTxt.getText().toString(), addressTxt.getText().toString(), imageUri));
populateList();
Toast.makeText(getApplicationContext(), nameTxt.getText().toString() + " has been added to your Contacts!", Toast.LENGTH_SHORT).show();
}
});
nameTxt.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
addBtn.setEnabled(!nameTxt.getText().toString().trim().isEmpty());
}
@Override
public void afterTextChanged(Editable s) {
}
});
contactImageimgView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Contact Image"), 1);
}
});
}
public void onActivityResult(int reqCode, int resCode, Intent data) {
if (resCode == RESULT_OK) {
if (reqCode == 1) {
imageUri = data.getData();
contactImageimgView.setImageURI(data.getData());
}
}
}
private void populateList() {
ArrayAdapter<Contact> adapter = new ContactListAdapter();
contactListView.setAdapter(adapter);
}
private class ContactListAdapter extends ArrayAdapter<Contact> {
public ContactListAdapter() {
super (ContactList.this, R.layout.listview_item, Contacts);
}
@Override
public View getView(int position, View view, ViewGroup parent) {
if (view == null)
view = getLayoutInflater().inflate(R.layout.listview_item, parent, false);
Contact currentContact = Contacts.get(position);
TextView name = (TextView) view.findViewById(R.id.contactName);
name.setText(currentContact.getName());
TextView phone = (TextView) view.findViewById(R.id.phoneNumber);
phone.setText(currentContact.getPhone());
TextView email = (TextView) view.findViewById(R.id.emailAddress);
email.setText(currentContact.getEmail());
TextView address = (TextView) view.findViewById(R.id.cAddress);
address.setText(currentContact.getAddress());
ImageView ivContactImage = (ImageView) view.findViewById(R.id.ivContactImage);
ivContactImage.setImageURI(currentContact.get_imageURI());
return view;
}
}
}
Error when implementing java.util.Collections.Sort(Contacts)
Error:(123, 34) error: no suitable method found for sort(List) method Collections.sort(List,Comparator) is not applicable (cannot instantiate from arguments because actual and formal argument lists differ in length) method Collections.sort(List) is not applicable (inferred type does not conform to declared bound(s) inferred: Contact bound(s): Comparable) where T#1,T#2 are type-variables: T#1 extends Object declared in method sort(List,Comparator) T#2 extends Comparable declared in method sort(List)