Я пытаюсь написать простой список дел, состоящий из одного титульного экрана (который является основным действием) с кнопкой, которая переключается на фактический список (который является другим действием). Тем не менее, после нажатия кнопки «Добавить» для этого другого действия ничего не происходит - функция onClick даже не вызывается. Как я должен заставить это работать?
Я пытался переписать код в соответствии с этим решением, но ничего не изменилось: Android Studio onClick и OnclickListener не работают после того, как они изменились на другое действие Более того, я попытался реализовать код, используя первый подход, упомянутый здесь, но безрезультатно: https://medium.com/@CodyEngel/4-ways-to-implement-onclicklistener-on-android-9b956cbd2928
Вот класс MainActivity:
package com.example.kandydatpl;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void openChecklist(View view) {
Intent intent = new Intent(this, TaskListActivity.class);
startActivity(intent);
}
}
MainActivitty.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="openChecklist"
android:text="Open Checklist"
tools:layout_editor_absoluteX="136dp"
tools:layout_editor_absoluteY="407dp" />
</android.support.constraint.ConstraintLayout>
, а также класс TaskListActivity:
package com.example.kandydatpl;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import java.io.File;
import java.util.ArrayList;
public class TaskListActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemClickListener {
private EditText itemET;
private Button btn;
private ListView itemsList;
private ArrayList<String> items;
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_task_list);
itemET = findViewById(R.id.item_edit_text);
btn = findViewById(R.id.add_btn);
itemsList = findViewById(R.id.items_list);
items = FileHelper.readData(this);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
itemsList.setAdapter(adapter);
btn.setOnClickListener(this);
itemsList.setOnItemClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.add_btn:
String itemEntered = itemET.getText().toString();
adapter.add(itemEntered);
itemET.setText("");
FileHelper.writeData(items, this);
Toast.makeText(this, "Item Added!", Toast.LENGTH_SHORT).show();
break;
}
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
items.remove(position);
adapter.notifyDataSetChanged();
FileHelper.writeData(items, this);
Toast.makeText(this, "Delete", Toast.LENGTH_SHORT).show();
}
}
и соответствующий код XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".TaskListActivity">
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/item_edit_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:hint="Enter Item" />
<Button
android:id="@+id/add_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClickAdd"
android:text="Add" />
</LinearLayout>
<ListView
android:id="@+id/items_list"
android:layout_width="395dp"
android:layout_height="667dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayout"
tools:layout_editor_absoluteX="8dp" />
</android.support.constraint.ConstraintLayout>