为了账号安全,请及时绑定邮箱和手机立即绑定

按字母顺序排序更改 ArrayList 中项目的位置,因此单击侦听器不匹配

按字母顺序排序更改 ArrayList 中项目的位置,因此单击侦听器不匹配

手掌心 2021-06-25 10:09:33
我制作了一个自定义比较器来按字母顺序对自定义 ArrayList 中的对象进行排序。列表中的项目有一个 OnItemClickListener。我的应用程序是多语言的。当我在设置中切换语言时,项目会改变它们的位置。因此, OnItemClickListener 失去了与索引中项目位置的匹配。如何链接项目的位置以在正确的位置维护点击侦听器?这是活动:public class ColorsCatalogue extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_colors_catalogue);        final ArrayList<setTextViewCatalogue> arrayListCatalogo = new ArrayList<>();        arrayListCatalogo.add(new setTextViewCatalogue(getResources().getString(R.string.codice_arancione), getResources().getString(R.string.sfumature_di_arancione), getResources().getString(R.string.codice_arancione)));        arrayListCatalogo.add(new setTextViewCatalogue(getResources().getString(R.string.codice_bianco), getResources().getString(R.string.sfumature_di_bianco), getResources().getString(R.string.codice_bianco)));        arrayListCatalogo.add(new setTextViewCatalogue(getResources().getString(R.string.codice_blu), getResources().getString(R.string.sfumature_di_blu), getResources().getString(R.string.codice_blu)));        Collections.sort(arrayListCatalogo, new AlphabeticalComparator());        setTextViewCatalogueAdapter adapter = new setTextViewCatalogueAdapter(this,arrayListCatalogo);        ListView listView = findViewById(R.id.catalogueListView);        listView.setAdapter(adapter);        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {            @Override            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {                if (i == 0) {                    Intent goToShadesOfOrange = new Intent(ColorsCatalogue.this, ShadesOfOrange.class);                    startActivity(goToShadesOfOrange);                }            }        });    }}
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 125 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信