我的 ListView 的意图没有改变我的活动

我正在创建一个闹钟应用程序,它在 MainActivity 上有一个闹钟列表,很像默认的 Android 时钟应用程序。为此,我实现了一个显示良好的 ListView。但是,我一直在尝试在 ListView 上设置一个更新按钮,以允许我通过我的 CreateAlarmActivity 更新警报的详细信息,并以此为指导:ListView 中的 Intent。我已经看过几次了,但是当我在测试过程中按下这个按钮时,没有任何反应。活动只是保持原样。


我敢肯定这可能是一件简单而愚蠢的事情(几乎总是如此),但我似乎找不到我的错误。也许我需要 AlarmAdapter 的 onClick 方法以及 MainActivity 的 onItemClick 中的某些内容?


代码:


主活动.java


public class MainActivity extends AppCompatActivity {


    ListView alarmListView;

    ArrayList<Alarm> alarmList = new ArrayList<Alarm>();

    AlarmAdapter alarmAdapter;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        alarmListView = findViewById(R.id.alarmListView);


        // Credit to Mitch Woodbright for helping with this section, and other try/catch blocks.

        try

        {

            ArrayList<Alarm> alarm = (ArrayList<Alarm>) getIntent().getSerializableExtra("alarmList");

            for(Alarm elements : alarm){

                alarmList.add(elements);

            }

        }

        catch (NullPointerException e){

            //error handling code

        }


        alarmAdapter = new AlarmAdapter(MainActivity.this, alarmList);

        alarmListView.setAdapter(alarmAdapter);

        alarmAdapter.notifyDataSetChanged();


        alarmListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override

            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent updateAlarmIntent = new Intent(MainActivity.this, CreateAlarmActivity.class);

                updateAlarmIntent.putExtra("alarmList", alarmList);

                updateAlarmIntent.putExtra("position", position);

                startActivity(updateAlarmIntent);



            }

        });

    }


慕村225694
浏览 124回答 2
2回答

牧羊人nacy

请alarmListView.setOnItemClickListener按照@Parul 的说法删除并在适配器中updateButton整体替换view。&nbsp;view.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View view) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent updateAlarmIntent = new Intent(MainActivity.this, CreateAlarmActivity.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; updateAlarmIntent.putExtra("alarmList", alarmList);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; updateAlarmIntent.putExtra("position", position);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startActivity(updateAlarmIntent);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });

阿晨1998

如果您只想在更新按钮上执行操作,请执行此操作。在适配器的 getView() 方法中:updateButton.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View view) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Logic goes here.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;onUpdateClick(position, alarmList); // public method in adapter&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });然后在适配器中创建公共方法:public void onUpdateClick(int position, ArrayList<alarm> alarmList){}现在在初始化适配器时在活动中覆盖此方法:alarmAdapter = new AlarmAdapter(MainActivity.this, alarmList){&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onUpdateClick(int position, ArrayList<alarm> alarmList) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super.onUpdateClick(position, alarmList);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent updateAlarmIntent = new Intent(MainActivity.this,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CreateAlarmActivity.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; updateAlarmIntent.putExtra("alarmList", alarmList);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; updateAlarmIntent.putExtra("position", position);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startActivity(updateAlarmIntent);&nbsp; &nbsp; &nbsp; }};
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java