Chris Bolton
Chris Bolton

Reputation: 2926

editText.getText().toString() not working?

Here's my current code. Basically it's populating my list with 'com.teslaprime.prirt.Task@44ef98aO' which I assume is the specific instance of that class. any ideas?

package com.teslaprime.prirt;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;

public class TaskList extends Activity {
  List<Task> model = new ArrayList<Task>();
  ArrayAdapter<Task> adapter = null;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button add = (Button) findViewById(R.id.add);
    add.setOnClickListener(onAdd);

    ListView list = (ListView) findViewById(R.id.tasks);

    adapter = new ArrayAdapter<Task>(this,android.R.layout.simple_list_item_1,model);
    list.setAdapter(adapter);
  }

  private View.OnClickListener onAdd = new View.OnClickListener() {
    public void onClick(View v) {
      Task task = new Task();
      EditText name = (EditText) findViewById(R.id.taskEntry);

      task.name = name.getText().toString();

      adapter.add(task);
    }
  };
}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  >
  <LinearLayout android:id="@+id/entryBar"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    >
    <EditText android:id="@+id/taskEntry"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_weight="1"
    />
    <Button android:id="@+id/add"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Add"
    />
  </LinearLayout>
  <ListView android:id="@+id/tasks"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_above="@id/entryBar"
  />
</RelativeLayout>

Upvotes: 0

Views: 1476

Answers (2)

Aleadam
Aleadam

Reputation: 40401

It looks like you're calling task.toString() somewhere else.

Try adapter.add(task.name); or override toString() in your Task class

What you see in the list : com.teslaprime.prirt.Task@44ef98aO is the output of Object.toString(). You need to override it to make it return the string you want:

public class Task {
  // all your code here
  @Override
  public String toString() {
    return this.name;
  }
}

Upvotes: 1

Haphazard
Haphazard

Reputation: 10948

Your EditText is fine, but you are adding it to that Task and adding the Task to the Adapter. Add name.getText().toString(); to the adapter rather than the task.

Upvotes: 1

Related Questions