将TextView子项目添加到ListView

时间:2018-09-17 14:43:38

标签: android listview subitem

我正在尝试为Android开发一个待办事项列表应用程序,我有三个EditText视图,用户可以在其中编写任务标题和任务内容。我想从我拥有的ListView中的EditText视图中发送这两个消息。

我的onCreate方法:

ArrayAdapter<String> mAdapter;
ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_menu);
FloatingActionButton faButton = (FloatingActionButton) findViewById(R.id.fab);  //Find Floatin Action Button
    // Waits for you to click the button
    faButton.setOnClickListener(new View.OnClickListener() {                        //Set onClickListener on fab
        @Override
        public void onClick(View view) {                                            //onClick method
            addNotification();                                                      //adds notification
            listView.setAdapter(mAdapter);                                          //sets the adapter
            addToDoElement(null);                                              //adds task to taskList
        }
    });
    loadTaskList();
    dbHelper = new DBHelper(this);
    listView = (ListView)findViewById(R.id.lstTask);
    listView = (ListView) findViewById(R.id.lstTask);
    taskList = new ArrayList<>();
    mAdapter = new ArrayAdapter<String>(this, R.layout.row, R.id.task_title, taskList);
    listView.setAdapter(mAdapter);
}

将EditText文本传输到ListView的类:

 ArrayList<String> taskList;
public void addToDoElement(View view) {

    //Gets TITLE text from EditText and sets it as title
    EditText mEdit   = (EditText)findViewById(R.id.title);
    Log.v("EditText", mEdit.getText().toString());

    //Gets CONTENT text from EditText and sets it as content
    EditText mEdit2   = (EditText)findViewById(R.id.content);
    Log.v("EditText", mEdit2.getText().toString());

    taskList.add(mEdit.getText().toString());
    mEdit.setText("");
    mAdapter.notifyDataSetChanged();

}

0 个答案:

没有答案