向listview添加项目无法应用字符串

时间:2016-03-25 14:40:03

标签: android arrays listview

所以我正在为Android工作室制作手机应用程序。我目前有2个不同的活动。我的MainActivity,以我的任务列表视图和createTask为特色。我遇到的问题是当我想将CreateTask中填充的数据发送到我的listView时,我不允许添加该项并获得以下错误

无法应用List中的add(java.long.string)

我的代码如下

// Adapter和ArrayList     private ArrayAdapter适配器;     私人清单项目;

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

}

请注意,我留下了一些代码,因为它与问题无关。所以语法可能有点

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

}

1 个答案:

答案 0 :(得分:1)

您正尝试将ListItem添加到items - 列表中。 但其类型为ArrayList<String>();

因此您可以将其更改为ArrayList<ListItem>(); ...