0

So I am working on making a application for my phone in Android studio. I currently have 2 different activities. My MainActivity, featuring my a listview of tasks and a createTask. The problem I encounter is when I want to send the data I filled in CreateTask to my listView, I am not allowed to add the item and get the following error

add(java.long.string) in List cannot be applied

My code is as followed

// Adapter and ArrayList private ArrayAdapter adapter; private List items;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    listView = (ListView) findViewById(R.id.task_list);
    registerForContextMenu(listView);
    TextView emptyView = (TextView) findViewById(R.id.main_list_empty);
    listView.setEmptyView(emptyView);

    //Initialize the views
    listView = (ListView) findViewById(R.id.task_list);

    //Create the List of items
    items = new ArrayList<String>();

    //Create the Array Adapter, give it a layout and a list of values
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
    adapter.notifyDataSetChanged();

    //Set the newly created adapter as the adapter for the listview
    listView.setAdapter(adapter);

    btn = (Button) findViewById(R.id.taskView);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(view.getContext(), CreateTask.class);
            startActivityForResult(intent, 1234);
        }
    });


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    //Check if the result code is the right one
    if (resultCode == Activity.RESULT_OK) {
        //Check if the request code is correct
        if (requestCode == 1234) {
            //Everything's fine, get the values;
            String title = data.getStringExtra("title");
            String description = data.getStringExtra("description");

            //Create a list item from the values
            ListItem item = new ListItem(title, description);

            //Add the new item to the adapter;
            items.add(item);

            //Have the adapter update
            adapter.notifyDataSetChanged();
        }
    }
    super.onActivityResult(requestCode, resultCode, data);
}

}

NOTE that I left some code out since it is irrelevant to the problem. so the syntax might be off a bit

  btn = (Button) findViewById(R.id.createButton);
    taskName = (EditText)this.findViewById(R.id.taskName);
    taskDescription = (EditText)this.findViewById(R.id.taskDescription);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                //Create a new intent with the entered data
                Intent data = new Intent();
                data.putExtra("title", taskName.getText().toString());
                data.putExtra("description",taskDescription.getText().toString());
                //Send the result back to the activity
                setResult(Activity.RESULT_OK, data);

                //Finish this activity
                finish();
        }

    });


  public class ListItem {
    private String title;
    private String description;

//Constructor
public ListItem(String title, String description) {
    this.title = title;
    this.description = description;
}

//Getters
public String getTitle() {
    return title;
}

public String getDescription() {
    return description;
}

//setters
public void setDescription(String description) {
    this.description = description;
}

public void setTitle(String title) {
    this.title = title;
}

}

3
  • Check the size of your items array. Is it contains any data ? Commented Mar 25, 2016 at 14:56
  • No there is no current data in there Commented Mar 25, 2016 at 14:59
  • Please post your ListItem code. Is it contains get set methods ? Commented Mar 25, 2016 at 15:00

1 Answer 1

1

You are trying to add a ListItem to the items-List. But its type is ArrayList<String>();

So you could change it to ArrayList<ListItem>();...

Sign up to request clarification or add additional context in comments.

3 Comments

Changing this gives me the a error at the arrayadapter saying its types are incomperable
you probably need to change the type of adapter = new ArrayAdapter<String>(this, ...) to new ArrayAdapter<ListItem>(...) too
then you have to write your own Adapter... like here link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.