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

Java 字母排序更改项目在ArrayList中的位置,因此单击侦听器不匹配,java,arraylist,onitemclicklistener,alphabetical-sort,Java,Arraylist,Onitemclicklistener,Alphabetical Sort,我制作了一个自定义比较器,在自定义ArrayList中按字母顺序对对象进行排序。列表中的项有一个侦听器。我的应用程序是多语言的。当我在设置中切换语言时,项目会更改其位置。因此,侦听器失去了与索引中项目位置的匹配 如何链接项目的位置以将单击侦听器保持在正确的位置 这是一项活动: public class ColorsCatalogue extends AppCompatActivity { @Override protected void onCreate(Bundle save

我制作了一个自定义比较器,在自定义ArrayList中按字母顺序对对象进行排序。列表中的项有一个侦听器。我的应用程序是多语言的。当我在设置中切换语言时,项目会更改其位置。因此,侦听器失去了与索引中项目位置的匹配

如何链接项目的位置以将单击侦听器保持在正确的位置

这是一项活动:

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);
                }

            }

        });

    }

}
arrayListCatalogo.add(new setTextViewCatalogue(getResources()
                          .getString(R.string.codice_arancione), 
                           getResources()
                           .getString(R.string.sfumature_di_arancione), 
                           getResources()
                           .getString(R.string.codice_arancione), new ClickHandler(){
                               @Override
                               void onClick(){
                                   //Your action
                               }
                           }));
    //Do this every time you call "add()"
  • 在类
    settExtViewCatalog
    中,定义要在单独函数中执行的操作(例如
    clickHandler

  • onItemClickListener
    中,执行以下操作:

    arrayListCatalogo.get(i).单击处理程序(/*参数以映射到特定操作*/)


  • 您还可以使用接口

    public Interface ClickHandler{
        void onClick();
    }
    
    将SetTextViewCatalog修改为:

    public class setTextViewCatalogue {
    
        private String mColorImg;
        private String mNomeColore;
        private String mCodiceColore;
        ClickHandler callback;
    
        public setTextViewCatalogue(String colorImg, String nomeColore, String codiceColore, ClickHandler callback) {
            mColorImg = colorImg;
            mNomeColore = nomeColore;
            mCodiceColore = codiceColore;
            this.callback = callback;
        }
    
        public void clicked(){ callback.onClick(); }
    
        public String getColorImg() {return mColorImg;}
        public String getNomeColore() {return mNomeColore;}
        public String getCodiceColore() {return mCodiceColore;}
    
    }
    
    在活动中修改构造函数:

    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);
                    }
    
                }
    
            });
    
        }
    
    }
    
    arrayListCatalogo.add(new setTextViewCatalogue(getResources()
                              .getString(R.string.codice_arancione), 
                               getResources()
                               .getString(R.string.sfumature_di_arancione), 
                               getResources()
                               .getString(R.string.codice_arancione), new ClickHandler(){
                                   @Override
                                   void onClick(){
                                       //Your action
                                   }
                               }));
        //Do this every time you call "add()"
    
    然后,在
    onItemClick

    @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
    
                arrayListCatalogo.get(i).clicked();
    
            }
    
    @覆盖
    公共无效onItemClick(AdapterView AdapterView、View视图、int i、long l){
    arrayListCatalogo.get(i).clicked();
    }
    
    括号可能放错地方了,因为我是从内存中键入的。请更正由于括号错误而遇到的任何错误。另外,不要忘记修改构造函数定义。它可以工作,这是我的错。你真的很准确。非常感谢。