Кнопка не работает при изменении активности ⇐ Android
-
Anonymous
Кнопка не работает при изменении активности
I am trying to write a simple to-do list, consisting of one title screen (which is a main activity) with a button that switches to the actual list (which is an another activity). However, upon clicking the 'Add' button on that another activity, nothing happens - the onClick function is not even called. How am I supposed to make this work?
I have tried to rewrite the code according to this solution, but nothing has changed: Android Studio onClick and OnclickListener not working once changed to another Activity Moreover, I tried to implement the code using the first approach mentioned here, to no avail: https://medium.com/@CodyEngel/4-ways-to ... 956cbd2928
Here's the MainActivity class:
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
as well as TaskListActivity class:
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 items; private ArrayAdapter 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(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(); } } and the correspoding XML:
Источник: https://stackoverflow.com/questions/584 ... is-changed
I am trying to write a simple to-do list, consisting of one title screen (which is a main activity) with a button that switches to the actual list (which is an another activity). However, upon clicking the 'Add' button on that another activity, nothing happens - the onClick function is not even called. How am I supposed to make this work?
I have tried to rewrite the code according to this solution, but nothing has changed: Android Studio onClick and OnclickListener not working once changed to another Activity Moreover, I tried to implement the code using the first approach mentioned here, to no avail: https://medium.com/@CodyEngel/4-ways-to ... 956cbd2928
Here's the MainActivity class:
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
as well as TaskListActivity class:
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 items; private ArrayAdapter 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(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(); } } and the correspoding XML:
Источник: https://stackoverflow.com/questions/584 ... is-changed
Мобильная версия