猿问

在一个活动中为多个列表定义 onItemClick 行为的正确方法?

我已经看到多个关于在一个活动中显示多个列表视图的问题,但我的问题与两者的 onClick 行为有关。我设置了两个列表视图,并动态填充它们 - 这很好用。问题在于为两个列表设置 onClick 行为时。使用我当前的代码,第二个列表在单击第一个列表中的相同项目时会打开的活动之上打开所需的活动。因此,当用户返回时,它会进入错误的活动,因为返回堆栈是错误的。


我定义 onItemClick 行为的代码在这里:


//make listview items respond to clicks and open relevant activity

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view,

                                int position, long id) {

            // selected item

            String standard = ((TextView) view).getText().toString();


            // Launching new Activity on selecting single List Item

            Intent i = new Intent(SubjectActivity.this, StandardInfoActivity.class);

            // sending data to new activity

            i.putExtra("standard", standard);

            i.putExtra("subject", subjectId);

            i.putExtra("subjectName", subject);

            i.putExtra("level", level);

            startActivity(i);

            finish();

        }

    });


    gradeView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent2, View view,

                                int position, long id2) {

            // Launching new Activity on selecting single List Item

            Intent j = new Intent(SubjectActivity.this, AddGradeActivity.class);

            // sending data to new activity

            j.putExtra("subjectId", subjectId);

            j.putExtra("position", position);

            j.putExtra("level", level);

            j.putExtra("subject", subject);

            startActivity(j);

            finish();


        }

    });

我的猜测是,因为我为两者定义了相同的函数,所以单击第二个列表会调用它两次,一次用于第一个列表,然后再次用于所需的列表。在这种情况下,定义两个列表行为的正确方法是什么?


编辑:用于设置适配器的代码:


ArrayList<String> mArrayList = myDb.getStandards(subjectId);


    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(

            this, R.layout.subject_list_item, mArrayList);

    lv.setAdapter(arrayAdapter);


    ArrayList<String> gradeList = myDb.getGrades(subjectId);



九州编程
浏览 165回答 2
2回答

哔哔one

在您的适配器中创建一个接口。然后您可以覆盖您的活动中的方法。界面:&nbsp; &nbsp;public interface ItemClickAdapterListener {&nbsp; &nbsp; &nbsp; &nbsp; void itemClick(View v, int position);&nbsp; &nbsp; }物品点击:&nbsp;viewHolder.llayout.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View view) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onClickListener.itemClick(view,position);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });活动代码。像这样创建适配器对象YourAdpter adapter= new YourAdpter(new YourAdapter.ItemClickAdapterListener() {&nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void itemClick(View v, int position) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }}):
随时随地看视频慕课网APP

相关分类

Java
我要回答