如何将单击的 ListView 项目的文本发送到我的新 activity?

How do I send the text of the ListView item that was clicked to my new activity?

我希望有人单击我的 ListView 中的一个项目,然后我的新 activity 将开始,他们单击的项目的文本应设置为我的新 activity。目前使用以下代码,我的 TextView 结果是: 'com.example.draft.AnimalNames@31571cf'

这是我在 MainActivity.java 中的内容:

list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String clickedName = (list.getItemAtPosition(position).toString());
                Intent intent = new Intent(MainActivity.this, Profile.class);
                intent.putExtra("clickedName", clickedName);
                startActivity(intent);
                System.out.println(clickedName);
            }
        });

在我的新 activity 中:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.profile_layout);
        Intent intent = getIntent();
        String clickedName = intent.getStringExtra("clickedName");
        animalName = findViewById(R.id.textView);
        animalName.setText(clickedName);
    }

在我的 AnimalNames.java 文件中:

public class AnimalNames {
    private String animalName;

    public AnimalNames(String animalName) {
        this.animalName = animalName;
    }

    public String getanimalName() {
        return this.animalName;
    }

}

我认为这与我的问题无关,但这是我的 ListViewAdapter.java 文件:

public class ListViewAdapter extends BaseAdapter {

    // Declare Variables
    Context mContext;
    LayoutInflater inflater;
    private List<AnimalNames> animalNamesList; // Declare a null variable
    private ArrayList<AnimalNames> arraylist; // Declare a null array

    public ListViewAdapter(Context context, List<AnimalNames> animalNamesList) {
        mContext = context;
        this.animalNamesList = animalNamesList;
        inflater = LayoutInflater.from(mContext);
        this.arraylist = new ArrayList<AnimalNames>();
        this.arraylist.addAll(animalNamesList);
    }

    public class ViewHolder {
        TextView name;
    }

    @Override
    public int getCount() {
        return animalNamesList.size();
    }

    @Override
    public AnimalNames getItem(int position) {
        return animalNamesList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public View getView(final int position, View view, ViewGroup parent) {
        final ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = inflater.inflate(R.layout.list_view_items, null); // Locate the TextViews in list_view_items.xml
            holder.name = (TextView) view.findViewById(R.id.name);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        // Set the results into TextViews
        holder.name.setText(animalNamesList.get(position).getanimalName());
        return view;
    }

    // Filter Class
    public void filter(String charText) {
        charText = charText.toLowerCase(Locale.getDefault());
        animalNamesList.clear();
        if (charText.length() == 0) {
            animalNamesList.addAll(arraylist);
        } else {
            for (AnimalNames wp : arraylist) {
                if (wp.getanimalName().toLowerCase(Locale.getDefault()).contains(charText)) {
                    animalNamesList.add(wp);
                }
            }
        }
        notifyDataSetChanged();
    }
}
String clickedName = (list.getItemAtPosition(position).toString());

通过调用toString()方法,它会给你对象引用,这就是你在TextView中得到的。

根据您的代码,如果您希望看到动物名称,您应该这样做(并将方法名称固定为 getAnimalName):

String clickedName = ((AnimalNames)list.getItemAtPosition(position)).getanimalName();

getItemAtPosition() returns 一个对象,所以你需要将它转换为正确的 class.