按字母顺序排序会更改项目在 ArrayList 中的位置,因此点击侦听器不匹配
Alphabetical sort changes position of items in ArrayList so click listeners mismatch
我制作了一个自定义比较器,可以在自定义 ArrayList 中按字母顺序对我的对象进行排序。列表中的项目有一个 OnItemClickListener。我的应用程序是多语言的。当我在设置中切换语言时,项目会改变它们的位置。因此 OnItemClickListener 失去了与项目在索引中的位置的匹配。
如何 link 项目的位置以便在正确的位置维护点击侦听器?
这是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);
}
}
});
}
}
这是自定义比较器:
public class AlphabeticalComparator implements Comparator<setTextViewCatalogue> {
@Override
public int compare(setTextViewCatalogue s1, setTextViewCatalogue s2) {
return s1.getNomeColore().compareTo(s2.getNomeColore());
}
}
这是 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;}
}
在classsetTextViewCatalogue
中定义你想在一个单独的函数中执行的动作(比如说clickHandler
)。
在onItemClickListener
中,这样做:
arrayListCatalogo.get(i).clickHandler(/*params to map to particular action*/)
你也可以使用接口
public Interface ClickHandler{
void onClick();
}
将 setTextViewCatalogue 修改为:
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;}
}
修改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()"
然后,在onItemClick
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
arrayListCatalogo.get(i).clicked();
}
我制作了一个自定义比较器,可以在自定义 ArrayList 中按字母顺序对我的对象进行排序。列表中的项目有一个 OnItemClickListener。我的应用程序是多语言的。当我在设置中切换语言时,项目会改变它们的位置。因此 OnItemClickListener 失去了与项目在索引中的位置的匹配。
如何 link 项目的位置以便在正确的位置维护点击侦听器?
这是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);
}
}
});
}
}
这是自定义比较器:
public class AlphabeticalComparator implements Comparator<setTextViewCatalogue> {
@Override
public int compare(setTextViewCatalogue s1, setTextViewCatalogue s2) {
return s1.getNomeColore().compareTo(s2.getNomeColore());
}
}
这是 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;}
}
在class
setTextViewCatalogue
中定义你想在一个单独的函数中执行的动作(比如说clickHandler
)。在
onItemClickListener
中,这样做:arrayListCatalogo.get(i).clickHandler(/*params to map to particular action*/)
你也可以使用接口
public Interface ClickHandler{
void onClick();
}
将 setTextViewCatalogue 修改为:
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;}
}
修改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()"
然后,在onItemClick
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
arrayListCatalogo.get(i).clicked();
}