我是android新手。我正在努力使用listview中的复选框。默认情况下,当从MainActivity单击按钮时,我使listview中的复选框处于INVISIBLE模式。我希望listview中的所有复选框都处于VISIBLE模式,但是当我单击按钮时,复选框在所有其他行中只有一个列表行是可见的。仍然是复选框处于INVISIBLE模式。
FavouriteListAdapter
public class FavouriteListAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater mInflater;
ArrayList<FavouritesArray> mArrFavourites;
ViewHolder viewHolder;
String favId;
//Make checkbox to visible in all rows in listview
public void setcheckbox() {
viewHolder.favCheckbox.setVisibility(View.VISIBLE);
}
public static class ViewHolder {
public TextView favName;
public TextView favUrl;
public TextView favAddress;
public CheckBox favCheckbox;
}
public FavouriteListAdapter(Context context, ArrayList<FavouritesArray> lArrFavourites) {
mContext = context;
mArrFavourites = lArrFavourites;
}
@Override
public int getCount() {
if (mArrFavourites == null) {
return 0;
}
return mArrFavourites.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = mInflater.inflate(R.layout.favouritelistadapter, parent, false);
viewHolder.favName = (TextView) convertView.findViewById(R.id.fav_name);
viewHolder.favUrl = (TextView) convertView.findViewById(R.id.fav_url);
viewHolder.favAddress = (TextView) convertView.findViewById(R.id.fav_address);
viewHolder.favCheckbox = (CheckBox) convertView.findViewById(R.id.fav_checkbox);
viewHolder.favCheckbox .setVisibility(View.INVISIBLE);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
final FavouritesArray chooseService = mArrFavourites.get(position);
viewHolder.favName.setText(chooseService.getFavouritename());
viewHolder.favAddress.setText(chooseService.getFavouritelocation());
viewHolder.favCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
favId = chooseService.getFavouritename();
((MainActivity) mContext).check(favId);
}
}
});
return convertView;
}}
答案 0 :(得分:0)
我认为你需要布尔变量 showCheckBox 并在方法 getView 中检查它的状态。
看看这个样本:
private boolean showCheckBox = false;
public void showChecBox(boolean show) {
showCheckBox = show;
}
public View getView(final int position, View convertView, ViewGroup parent) {
mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = mInflater.inflate(R.layout.favouritelistadapter, parent, false);
viewHolder.favName = (TextView) convertView.findViewById(R.id.fav_name);
viewHolder.favUrl = (TextView) convertView.findViewById(R.id.fav_url);
viewHolder.favAddress = (TextView) convertView.findViewById(R.id.fav_address);
viewHolder.favCheckbox = (CheckBox) convertView.findViewById(R.id.fav_checkbox);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
final FavouritesArray chooseService = mArrFavourites.get(position);
viewHolder.favName.setText(chooseService.getFavouritename());
viewHolder.favAddress.setText(chooseService.getFavouritelocation());
int checkBoxVisibility = showCheckBox : View.VISIBLE ? View.INVISIBLE;
viewHolder.favCheckbox.setVisibility(checkBoxVisibility);
viewHolder.favCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
favId = chooseService.getFavouritename();
((MainActivity) mContext).check(favId);
}
}
});
return convertView;
}}
答案 1 :(得分:0)
在活动中点击按钮时,您需要调用适配器的adapter.showCheckBoxes(true)
。然后在该函数中将字段设置为showCheckBoxes=true;
,并使用notifyDataSetChanged()
刷新列表,这将导致为所有行调用getView
方法。
在getView
中检查showCheckBoxes是否为true,并相应地显示复选框。
public class FavouriteListAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater mInflater;
ArrayList<FavouritesArray> mArrFavourites;
ViewHolder viewHolder;
String favId;
boolean showCheckBoxes = false;
//Make checkbox to visible in all rows in listview
public void setcheckbox() {
viewHolder.favCheckbox.setVisibility(View.VISIBLE);
}
public static class ViewHolder {
public TextView favName;
public TextView favUrl;
public TextView favAddress;
public CheckBox favCheckbox;
}
public FavouriteListAdapter(Context context, ArrayList<FavouritesArray> lArrFavourites) {
mContext = context;
mArrFavourites = lArrFavourites;
}
@Override
public int getCount() {
if (mArrFavourites == null) {
return 0;
}
return mArrFavourites.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = mInflater.inflate(R.layout.favouritelistadapter, parent, false);
viewHolder.favName = (TextView) convertView.findViewById(R.id.fav_name);
viewHolder.favUrl = (TextView) convertView.findViewById(R.id.fav_url);
viewHolder.favAddress = (TextView) convertView.findViewById(R.id.fav_address);
viewHolder.favCheckbox = (CheckBox) convertView.findViewById(R.id.fav_checkbox);
viewHolder.favCheckbox .setVisibility(View.INVISIBLE);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
final FavouritesArray chooseService = mArrFavourites.get(position);
viewHolder.favName.setText(chooseService.getFavouritename());
viewHolder.favAddress.setText(chooseService.getFavouritelocation());
if(showCheckBoxes){
viewHolder.favCheckbox .setVisibility(VISIBLE);
}else{
viewHolder.favCheckbox .setVisibility(INVISIBLE);
}
viewHolder.favCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
favId = chooseService.getFavouritename();
((MainActivity) mContext).check(favId);
}
}
});
return convertView;
}}
public void showCheckBoxes(boolean showCheckBoxes){
this.showCheckBoxes=showCheckBoxes;
notifyDataSetChanged();
}
在按钮点击功能中:
void onClick(View v){
mAdapter.showCheckBoxes(true);
}