У меня есть макет для списка, состоящий из одного флажка в строке. Когда действие запущено, оно отображает флажки, некоторые из которых отмечены по умолчанию. Вначале я правильно установил флажки. Но когда я хочу снять флажок и проверить других, все они будут сняты после того, как я получу сообщение «У вас не может быть более 5 вариантов!» от пользовательского адаптера. Я хочу, чтобы иметь возможность проверять новые флажки или снимите флажки с некоторыми из тех, которые уже были проверены с самого начала. Общее количество проверяемых коробки должно быть меньше 5.listview, состоящий из флажков
расположение: single_row_item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkBox"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/cinteretidtxt"
android:visibility="gone"/>
</LinearLayout>
Пользовательский адаптер: ModCinteretsAdapter
public class ModCinteretsAdapter extends ArrayAdapter<ModCinteret>{
private ArrayList<String> selectedStrings = new ArrayList<String>();
private ArrayList<String> selectedids = new ArrayList<String>();
public ModCinteretsAdapter(Context context, ArrayList<ModCinteret> cinterets) {
super(context, 0, cinterets);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
ModCinteret cinteret = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.single_row_item, parent, false);
}
// Lookup view for data population
final TextView cinteretidtxt = (TextView) convertView.findViewById(R.id.cinteretidtxt);
cinteretidtxt.setText(cinteret.getId());
final CheckBox cinterettxt = (CheckBox) convertView.findViewById(R.id.checkBox);
cinterettxt.setText(cinteret.getText());
if((cinteret.getFlag()).equals("1")) {
cinterettxt.setChecked(true);
selectedStrings.add(cinterettxt.getText().toString());
selectedids.add(cinteretidtxt.getText().toString());
}else {
cinterettxt.setChecked(false);
selectedStrings.remove(cinterettxt.getText().toString());
selectedids.remove(cinteretidtxt.getText().toString());
}
cinterettxt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
if(selectedStrings.size()<5) {
selectedStrings.add(cinterettxt.getText().toString());
selectedids.add(cinteretidtxt.getText().toString());
}else{
cinterettxt.setChecked(false);
Toast.makeText(getContext(),"You cannot have more than 5 choices!",Toast.LENGTH_LONG).show();
}
}else{
selectedStrings.remove(cinterettxt.getText().toString());
selectedids.remove(cinteretidtxt.getText().toString());
}
}
});
return convertView;
}
public ArrayList<String> getSelectedString(){
return selectedStrings;
}
public ArrayList<String> getSelectedId(){
return selectedids;
}
}
Привет, вы не должны добавлять слушателя в getView. Взгляды перерабатываются, и все будет замешано. –
привет Джавад. Я думаю, что это нормально, если вы хотите прослушивать клики внутри getView. Я прочитал несколько примеров из сети –
Возможно, я не знал. Когда вы обновляете флаги cinteret? –