0

我想隐藏图像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任何解决方案的图像

4

2 回答 2

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;
}
于 2013-01-04T05:50:57.833 回答
1

您可以使用标志来管理可见性并检查适配器中 getView 中的标志值。在这两种情况下 if(convertView==null) 和 else 部分也是。

并单击编辑按钮将标志值设置为 false 和ListView.invalidate()notifyDataSetChanged()

于 2013-01-04T06:20:16.457 回答