简体   繁体   中英

Alphabetical sort changes position of items in ArrayList so click listeners mismatch

I have made a custom comparator to alphabetically sort my object in a custom ArrayList. The items inside the list have a OnItemClickListener. My app is multilanguage. When I switch language in the settings the items change their position. Consequently the OnItemClickListener loses his match with the position of the item in the index.

How can I link the position of the item in order to mantain the click listeners in the right place?

This is the activity:

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

            }

        });

    }

}

This is the custom comparator:

public class AlphabeticalComparator implements Comparator<setTextViewCatalogue> {
    @Override
    public int compare(setTextViewCatalogue s1, setTextViewCatalogue s2) {
        return s1.getNomeColore().compareTo(s2.getNomeColore());
    }
}

This is the custom object inside the ArrayList:

public class setTextViewCatalogue {

    private String mColorImg;
    private String mNomeColore;
    private String mCodiceColore;

    public setTextViewCatalogue(String colorImg, String nomeColore, String codiceColore) {
        mColorImg = colorImg;
        mNomeColore = nomeColore;
        mCodiceColore = codiceColore;
    }

    public String getColorImg() {return mColorImg;}
    public String getNomeColore() {return mNomeColore;}
    public String getCodiceColore() {return mCodiceColore;}

}
  1. In the class setTextViewCatalogue , define the action you want to perform in a separate function (say clickHandler ).

  2. In the onItemClickListener , do this:

    arrayListCatalogo.get(i).clickHandler(/*params to map to particular action*/)


You could also use an interface

public Interface ClickHandler{
    void onClick();
}

Modify setTextViewCatalogue as:

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

}

Modify the constructor in the activity:

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()"

Then, in onItemClick

@Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            arrayListCatalogo.get(i).clicked();

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM