Android Studio中RecyclerView仅显示姓氏问题求助
Hey there! Let's troubleshoot why your RecyclerView items are only displaying the lastname instead of the full firstname + lastname you're trying to show. Based on the code you shared, here are the most likely issues and fixes:
1. Check Your Contact Class Getter Methods
This is the most common culprit here. In your Adapter, you're calling contact.getFirstname() but if this method isn't correctly returning the firstname field, it'll show an empty string (making it look like only lastname is there).
Example of a Correct Contact Class:
public class Contact { // Make sure these field names match exactly what you're assigning in MainActivity public String firstname; public String lastname; public String phone; // Double-check these getters return the right fields! public String getFirstname() { return firstname; // Don't accidentally return lastname here! } public String getLastname() { return lastname; } public String getPhone() { return phone; } }
Common mistake: Accidentally returning lastname in getFirstname(), or misspelling the field name (e.g., using firstName with a capital N but assigning to firstname in your parser).
2. Verify JSON Parsing is Fetching firstname Correctly
Let's confirm your GET request is actually pulling the firstname value from the JSON. Add a debug log inside your parsing loop in MainActivity:
for (int i = 0; i < response.length(); i++) { JSONObject employee = response.getJSONObject(i); Contact e=new Contact(); e.firstname = employee.getString("firstname"); e.lastname = employee.getString("lastname"); e.phone = employee.getString("phone"); // Add this log to check parsed values Log.d("MainActivity", "Parsed: " + e.firstname + " " + e.lastname); items.add(e); }
Run your app, open Logcat, and look for the MainActivity logs. If firstname is empty here, you might have a typo in the JSON key (but your JSON shows "firstname" which matches your code, so this is less likely).
3. Check Your Filter Logic (Since You're Using Search Filter)
If your contactListFiltered (the list your Adapter uses) is missing the firstname values, that'll cause the issue too. Make sure your Filter implementation isn't accidentally dropping the firstname field when creating filtered items.
Correct Filter Implementation Example:
@Override public Filter getFilter() { return new Filter() { @Override protected FilterResults performFiltering(CharSequence charSequence) { String searchQuery = charSequence.toString().toLowerCase(); List<Contact> filteredList = new ArrayList<>(); if (searchQuery.isEmpty()) { filteredList = contactList; } else { for (Contact contact : contactList) { // Include both firstname and lastname in search checks if (contact.getFirstname().toLowerCase().contains(searchQuery) || contact.getLastname().toLowerCase().contains(searchQuery)) { filteredList.add(contact); } } } FilterResults results = new FilterResults(); results.values = filteredList; return results; } @Override protected void publishResults(CharSequence charSequence, FilterResults filterResults) { contactListFiltered = (List<Contact>) filterResults.values; notifyDataSetChanged(); } }; }
Avoid this mistake: Creating new Contact objects in the filter loop but forgetting to set the firstname field.
4. Quick Test to Isolate the Issue
As a quick check, modify your Adapter's onBindViewHolder to use the public field directly instead of the getter:
holder.name.setText(contact.firstname + " " + contact.lastname);
If this shows the full name, you know the problem is definitely with your getFirstname() method.
内容的提问来源于stack exchange,提问作者loucha

