【发布时间】:2017-08-09 09:06:28
【问题描述】:
我正在为复选框和文本使用自定义列表视图。数据来自服务器,如果服务器上有任何项目可用,它会在列表视图中显示为选中状态。但是如果我取消选择某些项目(复选框),它会再次得到滚动列表视图后选择。 任何建议都会有所帮助。 这是我的适配器类:
public class SetWorkAdapter extends BaseAdapter {
Context mContext;
public ArrayList<SubStkMarket> mSubStkMarkets;
checkBoxListener checkBoxListener;
ArrayList<MarketNameUID> arrayList;
public SetWorkAdapter(Context mContext, ArrayList<SubStkMarket> mSubStkMarkets,ArrayList<MarketNameUID> arrayList) {
this.mContext = mContext;
this.mSubStkMarkets = mSubStkMarkets;
this.arrayList=arrayList;
}
public interface checkBoxListener {
public void onCheckBoxClick(int position, boolean isChecked, ArrayList<MarketNameUID> mList);
}
public void setCustomButtonListner(checkBoxListener listener) {
this.checkBoxListener = listener;
}
@Override
public int getCount() {
return mSubStkMarkets.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(final int position, View view, ViewGroup viewGroup) {
View convertview;
TextView code, name;
final CheckBox checkBox;
convertview = LayoutInflater.from(mContext).inflate(R.layout.set_work_type_item, null);
code = (TextView) convertview.findViewById(R.id.set_work_type_item_market_code);
name = (TextView) convertview.findViewById(R.id.set_work_type_item_market_name);
checkBox = (CheckBox) convertview.findViewById(R.id.set_work_type_item_market_checkbox);
SubStkMarket subStkMarket = mSubStkMarkets.get(position);
checkBox.setChecked(subStkMarket.isSelected());
code.setText(String.valueOf(subStkMarket.getSubStkUID()));
name.setText(subStkMarket.getMarketName());
if(arrayList!=null)
{
for(int i=0;i<arrayList.size();i++)
{
String marketName = arrayList.get(i).getMarketName();
if(marketName.equalsIgnoreCase(subStkMarket.getMarketName()))
{
checkBox.setChecked(true);
}
}
int cnt=arrayList.size();
}
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if (checkBoxListener != null) {
checkBoxListener.onCheckBoxClick(position,isChecked,arrayList);
}
}
});
return convertview;
}
}
【问题讨论】:
-
onCheckBoxClick的实现是什么?您没有将selected的状态更改为subStkMarket,也不清楚arraylist是什么 -
更好地使用recyclerview
标签: android listview checkbox adapter