我有一个列表视图,我听这个。我检查了一个选项,但另一个选择被自动选中,因此总共选择了两个项目。我选择了一个,另一个自动选中。我希望我点击的项目仅被选中。 我该如何解决?
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = mContext.getLayoutInflater().inflate(
R.layout.brandslayout, parent, false);
TextView UrunAdi = (TextView) convertView
.findViewById(R.id.BrandAdi);
final TextView UrunId = (TextView) convertView
.findViewById(R.id.BrandId);
final CheckBox chkSelect = (CheckBox) convertView
.findViewById(R.id.checkBox1);
final EditText edtText = (EditText) convertView
.findViewById(R.id.edtOran);
Button btnSelect = (Button) convertView
.findViewById(R.id.btnSeciliBrand);
final TextView tvOran = (TextView) convertView
.findViewById(R.id.tvRateIcon);
UrunAdi.setText(((Brands)Markalar.get(position)).BrandName);
UrunId.setText(((Brands)Markalar.get(position)).BrandId);
chkSelect.setVisibility(((Brands)Markalar.get(position)).deger1);
edtText.setVisibility(((Brands)Markalar.get(position)).deger2);
btnSelect.setVisibility(((Brands)Markalar.get(position)).deger3);
tvOran.setVisibility(((Brands)Markalar.get(position)).deger4);
convertView.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Toast.makeText(YeniSahaActivity2.this, "Ürünler Aranıyor...", Toast.LENGTH_SHORT).show();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Spinner spnProductGroup = (Spinner)findViewById(R.id.spnBrandProduct);
if(chkSelect.isChecked())
{
ProductGroupId=UrunId.getText().toString();
RackPercentage= edtText.getText().toString();
ExistsOnRack = "1";
AddProducts(Integer.parseInt( spnProductGroup.getSelectedItem().toString().split("#")[1]),Integer.parseInt(UrunId.getText().toString()));
//Button btnSave = (Button) findViewById(R.id.btnMarkaGir);
//btnSave.setText("");
}
else
{
ExistsOnRack = "0";
//Toast.makeText(YeniSahaActivity2.this, "Lütfen ürün grubu seçiniz...", Toast.LENGTH_SHORT).show();
Extras e = new Extras();
e.ViewAlert("Lütfen ürün grubu seçiniz...", YeniSahaActivity2.this);
}
}
});
return convertView;
}
}
答案 0 :(得分:1)
您必须小心,因为滚动时列表视图重复使用元素。您需要在getview方法中相应地设置/取消选中它们。否则,当您滚动时,您选中的一个将重新显示,而不是要检查的项目。
我假设您的列表默认开始,因为没有检查?因为我看不到设置任何复选框的代码。
在创建/回收视图时,您需要一个基本上设置/取消设置的部分。 (setVisibility
部分旁边。)
if(<Item should be checked>)
chkSelect.setChecked(true);
else
chkSelect.setChecked(false);
或简称:
chkSelect.setChecked(<Item should be checked>);
你需要一种方法,知道哪些项目已被检查过,我认为你的AddProducts
方法正在做什么?因此,请检查相关项目是否在产品列表中。
答案 1 :(得分:0)
使用适配器中的视图持有者进行击球手表现,它也会解决一些此类问题。见this。
答案 2 :(得分:0)
我修改了你的代码。 clickListener
首次创建时需要注册。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null){
convertView = mContext.getLayoutInflater().inflate(
R.layout.brandslayout, parent, false);
convertView.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Toast.makeText(YeniSahaActivity2.this, "Ürünler Araniyor...", Toast.LENGTH_SHORT).show();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Spinner spnProductGroup = (Spinner)findViewById(R.id.spnBrandProduct);
if(chkSelect.isChecked())
{
ProductGroupId=UrunId.getText().toString();
RackPercentage= edtText.getText().toString();
ExistsOnRack = "1";
AddProducts(Integer.parseInt( spnProductGroup.getSelectedItem().toString().split("#")[1]),Integer.parseInt(UrunId.getText().toString()));
//Button btnSave = (Button) findViewById(R.id.btnMarkaGir);
//btnSave.setText("");
}
else
{
ExistsOnRack = "0";
//Toast.makeText(YeniSahaActivity2.this, "Lütfen ürün grubu seçiniz...", Toast.LENGTH_SHORT).show();
Extras e = new Extras();
e.ViewAlert("Lütfen ürün grubu seçiniz...", YeniSahaActivity2.this);
}
}
});
}
TextView UrunAdi = (TextView) convertView
.findViewById(R.id.BrandAdi);
final TextView UrunId = (TextView) convertView
.findViewById(R.id.BrandId);
final CheckBox chkSelect = (CheckBox) convertView
.findViewById(R.id.checkBox1);
final EditText edtText = (EditText) convertView
.findViewById(R.id.edtOran);
Button btnSelect = (Button) convertView
.findViewById(R.id.btnSeciliBrand);
final TextView tvOran = (TextView) convertView
.findViewById(R.id.tvRateIcon);
UrunAdi.setText(((Brands)Markalar.get(position)).BrandName);
UrunId.setText(((Brands)Markalar.get(position)).BrandId);
chkSelect.setVisibility(((Brands)Markalar.get(position)).deger1);
edtText.setVisibility(((Brands)Markalar.get(position)).deger2);
btnSelect.setVisibility(((Brands)Markalar.get(position)).deger3);
tvOran.setVisibility(((Brands)Markalar.get(position)).deger4);
return convertView;
}
}