我想隐藏ListView
中的图片,我在BaseAdapter
使用了自定义列表视图
请看下面的图片,此处点击Edit btn
图片1 应该是可见的,
ListView图片如下
我点击了Activity
button
完成了此代码
++btnClick;
if (btnClick % 2 == 0)
{
textView.setText("Edit");
baseAdapter.holder.imgPhoto.setVisibility(View.INVISIBLE);
} else {
textView.setText("Done");
Log.e("call", "Done");
baseAdapter.holder.imgPhoto.setVisibility(View.VISIBLE);
};
其中baseAdapter
是BaseAdapter
的对象,这里仅点击按钮最后一个按钮不可见会发生什么,因为它是最后一个参考,我不想实现再次BaseAdapter
。
BaseAdapterFavotites.java
public class BaseAdapterFavotites extends BaseAdapter {
private ArrayList<SearchResults> searchArrayList;
public ArrayList<String> getSchoolId, getSchoolName;
private LayoutInflater mInflater;
public ViewHolder holder;
Context context;
public String s;
HashMap<String, String> mapSchoolToLink = new HashMap<String, String>();;
public BaseAdapterFavotites(Context context,
ArrayList<SearchResults> results, ArrayList<String> arrayId,
ArrayList<String> arraySchoolName) {
searchArrayList = results;
mInflater = LayoutInflater.from(context);
getSchoolId = arrayId;
this.context = context;
getSchoolName = arraySchoolName;
}
public int getCount() {
return searchArrayList.size();
}
public Object getItem(int position) {
return searchArrayList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.custom_row_for_favorites,
null);
holder = new ViewHolder();
holder.txtSchoolNameList = (TextView) convertView
.findViewById(R.id.schoolNameFav);
holder.imgPhoto = (ImageView) convertView.findViewById(R.id.delete);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtSchoolNameList.setText(searchArrayList.get(position)
.getschoolNameFromList());
Log.e("holder", searchArrayList.get(position).getschoolNameFromList());
holder.imgPhoto.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
mapSchoolToLink.clear();
searchArrayList.remove(position);
getSchoolId.remove(position);
getSchoolName.remove(position);
notifyDataSetChanged();
Log.e("inside base", searchArrayList.toString());
Log.e("inside getSchoolId", getSchoolId.toString());
for (int i = 0; i < getSchoolId.size(); i++) {
mapSchoolToLink.put(getSchoolName.get(i),
getSchoolId.get(i));
}
SharedPreferences.Editor editor = context
.getSharedPreferences("mytest", 0).edit().clear();
for (Entry<String, String> entry : mapSchoolToLink.entrySet()) {
editor.putString(entry.getKey(), entry.getValue());
}
editor.commit();
}
});
return convertView;
}
public class ViewHolder {
TextView txtSchoolNameList;
public ImageView imgPhoto;
}
}
我必须将图片从Activity
隐藏到BaseAdapter
此
答案 0 :(得分:2)
您可以在适配器中设置字段变量,不您的活动,控制图像是否可见,将其命名为inEditMode
。在getView()
中使用您的:
if(inEditMode) {
holder.imgPhoto.setVisibility(View.GONE);
}
else {
holder.imgPhoto.setVisibility(View.VISIBLE);
}
每当您切换inEditMode
的状态时,您必须调用notifyDatasetChanged()
来更新整个ListView。
添加
public View getView(final int position, View convertView, ViewGroup parent) {
// Recycling and View Holder code...
holder.txtSchoolNameList.setText(searchArrayList.get(position)
.getschoolNameFromList());
Log.e("holder", searchArrayList.get(position).getschoolNameFromList());
if(inEditMode) {
holder.imgPhoto.setVisibility(View.GONE);
}
else {
holder.imgPhoto.setVisibility(View.VISIBLE);
}
// Setting an OnClickListener that should happen in (convertView == null) { ... }
return convertView;
}
答案 1 :(得分:1)
您可以使用标志来管理可见性,并在适配器中的getView中检查标志值。在这两种情况下if(convertView == null)和else part。
点击编辑按钮将标志值设置为false并ListView.invalidate()
或notifyDataSetChanged()