You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ListView转RecyclerView后,点击Item如何显示正确内容而非对象地址?

Fixing RecyclerView Item Click Showing Object Memory Address Instead of Content

Hey there! That's a super common issue when working with custom objects in RecyclerViews. The com.example.android.basicrecycle.Word@52809370 you're seeing is the default output of Java's toString() method for your Word class—it just returns the class name plus the object's hash code, not the actual content you want to display. Here are two straightforward fixes:

Option 1: Override the toString() Method in Your Word Class

Add a custom toString() method to your Word class that returns the specific content you want to show. For example, if your Word class has a mWord field storing the item text:

public class Word {
    private String mWord;

    // Your existing constructor, getters, setters, etc.

    @Override
    public String toString() {
        // Return the actual content you want to display
        return mWord;
    }
}

Now when you pass the Word object directly to Toast.makeText(), it will use this custom toString() method and show the correct content instead of the memory address.

Option 2: Access the Specific Field Directly in the Click Callback

If you don't want to modify the Word class, you can directly fetch the relevant field from the Word object in your click listener method. Assuming your adapter's data source is a List<Word> called mWords, here's how to adjust your onListItemClick method in MainActivity:

@Override
public void onListItemClick(int clickedItemIndex) {
    Word selectedWord = mWords.get(clickedItemIndex);
    // Use the getter method for your content field (e.g., getWord())
    Toast.makeText(this, selectedWord.getWord(), Toast.LENGTH_SHORT).show();
}

This will pull the exact content from the Word object and display it in the Toast, skipping the default toString() output entirely.

Either approach will solve your problem—pick the one that fits your use case best! If you need to display Word objects as strings elsewhere in your app, Option 1 is more efficient. If this is just a one-time use for the click listener, Option 2 is quick and targeted.

内容的提问来源于stack exchange,提问作者Vinmister

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 10:19:35