本文介绍了Android的 - 从删除的GridView的项目里面getView()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种方法来去除GridView的一个项目,但是这必须由getView内内我的自定义适配器来实现()方法。这是我的GridView:

I need a way to remove an Item from the GridView but this must be done from within the getView() method inside my custom Adapter. Here is my GridView:

活动:

    ...
    String[] newList;
    newList[0] = "Item 1";
    newList[1] = "Item 2";
    newList[2] = "Item 3";
    ...
    GridView GV = (GridView) getActivity().findViewById(R.id.gv);
    GV.setAdapter(new Adapter(getActivity(), newList));
    GV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView arg0, 
            View arg1, int position, long arg3) {
            ...
        }
    });

适配器

public class Adapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater mInflator;
    private String mEntries[];

    public static class ViewHolder {
        public TextView myTextView;
    }

    public Adapter (Context context, String[] entries) {
        mContext = context;
        mInflator = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mEntries = entries; 
    }

    @Override
    public int getCount() {
        return mEntries.length;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        if(convertView == null) {
            convertView = mInflator.inflate(R.layout.gvitemlayout, parent, false);
            holder = new ViewHolder();
            holder.myTextView = (TextView) convertView.findViewById(R.list.removeItem);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        String string = mEntries[position];
        String[] data = string.split("\\.");

        if (data.length < 2) {
            TextView itemName = (TextView) convertView.findViewById(R.list.itemName);
            itemName.setText("");
            TextView itemClass = (TextView) convertView.findViewById(R.favlist.itemClass);
            itemClass.setText("");
            holder.myTextView.setText("");
            TextView itemNone = (TextView) convertView.findViewById(R.list.itemNone);
            itemNone.setText("No Items");
        } else {
            TextView itemName = (TextView) convertView.findViewById(R.list.itemName);
            itemName.setText(data[1]);
            TextView itemClass = (TextView) convertView.findViewById(R.list.itemClass);
            itemClass.setText(data[0]);
        }

        final int info = (Integer) getItem(position);
        holder.myTextView.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {               
                SharedPreferences sP = mContext.getSharedPreferences("fav", mContext.MODE_PRIVATE);
                Boolean b = sP.getBoolean(mEntries[info], false);
                if (b == true) {
                    SharedPreferences.Editor editor = sP.edit();
                    editor.remove(mEntries[info]);
                    editor.commit();

                    // REMOVE ITEM CODE NEEDED HERE

                }
            }
        });
        return convertView;
    }
}

希望这使得它更容易明白我需要什么。

Hope this makes it easier to understand what I need.

推荐答案

这是我做的事,没有必要初始化适配器变量。在 getView()简单地做到这一点:

This is what I did, there was no need to initialise the 'adapter' variable. Inside the getView() simply do this:

  1. 从被用作数据源列表中删除项目。

所以。如果你想从列表中删除项目2

So. if you want to remove Item 2 from the list

字符串mEntries [] = ITEM1,ITEM2,ITEM3等

做到这一点:

String newList[] = new String[mEntries.length - 1]; 
int count = 0;
for (int i = 0; i < mEntries.length; i++) {
    if (mEntries.length - 1 > 0) {
        if (mEntries[i] == mEntries[1]) { // mEntries[1] as the range starts from 0, so 1 would be ITEM2
            // SKIP IF MATCHES THE ITEM YO WANT TO REMOVE                       
        } else {
            newList[count] = mEntries[i];
            count++;
        }
    }
}
mEntries = newList; // save the new list into mEntries
notifyDataSetChanged(); // notify the changes and the listview/gridview will update

这篇关于Android的 - 从删除的GridView的项目里面getView()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 13:59