Search code examples
javaandroidandroid-listview

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


I would like someone to click on an item in my ListView and then my new activity would start and the text of the item they clicked on should be set as the text of the TextView in my new activity. Currently with the following code, my TextView result is: 'com.example.draft.AnimalNames@31571cf'

Here is what I have in 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);
            }
        });

And in my new 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);
    }

And in my AnimalNames.java file:

public class AnimalNames {
    private String animalName;

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

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

}

I don't think it's relevant to my question but here is my ListViewAdapter.java file:

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();
    }
}

Solution

  • String clickedName = (list.getItemAtPosition(position).toString());
    

    By calling toString() method, it will give you the object reference, which is what you get in the TextView.

    According to your code, if you wish to see the Animal name, you should do (and fix the method name to getAnimalName):

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

    getItemAtPosition() returns an Object, so you need to cast it to the correct class.