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