1

我有一个 GridView,里面有两个 TextView,当填充 GridView 时,会设置一个 OnClickListener,它返回所选项目的位置。我想在选择其中一个 TextView 时触发一个方法。

这可能吗?如果是,我该如何设置?

编辑 3

在填充 GridView 的活动中:我String-Array从 my中检索 a Strings.xml,afor loop检查 Array 中的每个项目并根据 SharedPreferences 中的项目名称搜索条件,这for loop仅用于计算有多少“真实”条件,所以它检索int保存在count. 然后String[]创建一个新的,这需要在添加项目之前给出一个确切的长度,所以我检查count它是否大于 0,它将给出String[]一个长度,count然后另一个for loop将每个添加trueString[]我们刚刚的列表中创建的。如果count为 0(在第一个中没有找到真正的条件for loop),那么只有 1 个项目被添加到String[]并被赋予值“未添加收藏夹”。

然后你就有了 GridView 的 OnItemClickListener()。

        String s[] = getResources().getStringArray(R.array.FullList);
        int count = 0;
        for(int i = 0; i < s.length; i++) {
            SharedPreferences sP = getActivity().getSharedPreferences("fav", MODE_PRIVATE);
            Boolean b = sP.getBoolean(s[i], false);
            if (b == true) {
                count++;
            }
        }
        String[] newList;
        if (count > 0) {
            newList = new String[count];
            count = 0;
            for(int i = 0; i < s.length; i++) {
                SharedPreferences sP = getActivity().getSharedPreferences("fav", MODE_PRIVATE);
                Boolean b = sP.getBoolean(s[i], false);
                if (b == true) {
                    newList[count] = s[i];
                    count++;
                }
            }
        } else {
            newList = new String[1];
            newList[0] = "No favourites added";
        }

        GridView FavGV = (GridView) getActivity().findViewById(R.id.sexp_fav);
        FavGV.setAdapter(new Tab01_FavAdapter(getActivity(), newList));
        FavGV.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView arg0, 
                    View arg1, int position, long arg3) {

                //Intent i = new Intent(getActivity(), PosPreview_Gestures.class);
                //i.putExtra("position", position);
                //startActivity(i);
            }
        });

这就是填充 GridView 的 Activity 中的代码。适配器的原始功能形式:这只是用最喜欢的项目(它们的名称来自 String[])填充 GridView,并添加一个带有“Remove”的 TextView,当按下它时,会显示一个 Toast:“Remove”。

public class Tab01_FavAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater mInflator;

    String mEntries[];     
    public Tab01_FavAdapter (Context c, String[] entries) {
        mContext = c;
        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) {

        if(convertView == null) {
            convertView = mInflator.inflate(R.layout.favitemlayout, parent, false);
        }

        TextView tx = (TextView) convertView.findViewById(R.id.favgridremoveitem);
        OnClickListener oCL = new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext,"Remove",Toast.LENGTH_SHORT).show();
            }
        };
        tx.setOnClickListener(oCL);

        return convertView;
    }
}
4

1 回答 1

0

I am assuming that you are using a custom adapter for populating this GridView, and passing the Context as an argument to the constructor.

In the custom adapter, you should add onClickListeners to the TextViews. Using the context, you can call methods from your activity:

((CallingActivityName)context).methodYouWishToCall(parameters);

This would go inside the onClickListeners.

Edit: Added some code:

public class MyGridAdapter extends BaseAdapter {

    private final List<MyObjectClass> mEntries;
    private final LayoutInflater mInflater;
    private final Context mContext; 

    public static class ViewHolder {
        public TextView tx;
    }

    public MyGridAdapter(CallingActivityName context, List<MyObjectClass> entries) {
        super();
        mEntries = entries;
        mContext = context;
        mInflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

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

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

        final ViewHolder holder;

        if (convertView == null) {

            convertView = mInflator.inflate(R.layout.favitemlayout, parent, false);
            holder = new ViewHolder();

            holder.tx = (TextView) convertView
                .findViewById(R.id.favgridremoveitem);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final MyObjectClass info = mEntries.get(position);

        holder.tx.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        ((CallingActivityName)mContext).favRemove(info);
                        notifyDataSetChanged();
                    }
        });

        return convertView;
    }

}

So, CallingActivityName is the name of the Activity where you initiate the adapter and where the method you need to call resides. info is the object held at position position of the gridview. MyObjectClass is the class name of the objects in the List mEntries.

于 2013-07-17T18:27:07.237 回答