broadcastlistener无法从片段中的listview适配器获取意图

时间:2015-01-25 01:39:52

标签: android android-intent broadcastreceiver fragment

我有一个在标签结构中设置的片段

其中一个片段,我有一个垂直列表视图

同样在每个垂直列表视图项目中,我都有一个水平列表视图。

问题是..当我在水平适配器中声明一个意图广播器时

并且还在主片段中声明了broadcastlistener但是监听器似乎没有听取意图

请帮助我......我是韩国学生..

这是我的代码简要

[在Horizo​​ntial Adapter中]

...

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

    View retval = convertView;

    if(convertView==null)       // initialize
    {
        retval = LayoutInflater.from(parent.getContext()).inflate(R.layout.part_item, null);

        TextView tv_workout = (TextView)retval.findViewById(R.id.txt_work);
        ImageView iv_workout = (ImageView)retval.findViewById(R.id.pic_work);

        tv_workout.setText(getItem(rowpos).get(position).getPartname());
        iv_workout.setImageResource(getItem(rowpos).get(position).getImagesrc());

        iv_workout.setTag(position);
        iv_workout.setOnTouchListener(new OnTouchListener() 
        {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                if((int)v.getTag() == getCount()-1)
                {   
                    PartContent temp_part = new PartContent("part", R.drawable.plus);
                    PartContent temp_base_part = new PartContent("workout", R.drawable.dumb);


                    totallist.get(rowpos).set(getCount()-1, temp_base_part);
                    totallist.get(rowpos).add(temp_part);

                    Intent intent = new Intent();
                    intent.setClass(getContext(), FragRoutine.class);
                    intent.putExtra("row", rowpos);
                    intent.putExtra("col", (int) v.getTag());
                    getContext().sendBroadcast(intent);


                    //Toast.makeText(getContext(), "one more hour!" , Toast.LENGTH_SHORT).show();
                    getView((int)v.getTag(), (View)v.getParent(), (ViewGroup)v.getParent().getParent());

                    //FragRoutine fr = new FragRoutine();
                    //fr.TotalList.get(rowpos).set(getCount() - 1, temp_base_part);
                    //fr.TotalList.get(rowpos).add(temp_part);

                }
                return true;
            }
        });

    }

    else 
    {
        TextView tv_workout = (TextView)convertView.findViewById(R.id.txt_work);
        ImageView iv_workout = (ImageView)convertView.findViewById(R.id.pic_work);

        tv_workout.setText(getItem(rowpos).get(position).getPartname());
        iv_workout.setImageResource(getItem(rowpos).get(position).getImagesrc());

        iv_workout.setTag(position);
        iv_workout.setOnTouchListener(new OnTouchListener() 
        {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                if((int)v.getTag() == getCount() - 1)
                {   
                    PartContent temp_part = new PartContent("part", R.drawable.plus);
                    PartContent temp_base_part = new PartContent("part", R.drawable.dumb);

                    totallist.get(rowpos).set(getCount()-1, temp_base_part);
                    totallist.get(rowpos).add(temp_part);

                    Intent intent = new Intent();
                    //intent.setClass(getContext(), FragRoutine.class);
                    intent.putExtra("row", rowpos);
                    intent.putExtra("col", (int) v.getTag());
                    getContext().sendBroadcast(intent);


                    Toast.makeText(getContext(), "last clicked" , Toast.LENGTH_SHORT).show();
                    getView((int)v.getTag(), (View)v.getParent(), (ViewGroup)v.getParent().getParent());

                    //FragRoutine fr = new FragRoutine();
                    //fr.TotalList.get(rowpos).set(getCount() - 1, temp_base_part);
                    //fr.TotalList.get(rowpos).add(temp_part);

                }
                return true;
            }
        });
    }

    return retval;
}

[broadcastlistener]

...


public class FragRoutine extends Fragment
{
    private BroadcastReceiver mReceiver;

    @Override
    public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate (R.layout.schedule_routine, container,false);
        return view;
    }

    public void onStart() {
        super.onStart();        
        initializeViewObject();
    }

    private void initializeViewObject(){

            IntentFilter intentFilter = new IntentFilter();
        mReceiver = new BroadcastReceiver(){

            @Override
            public void onReceive(Context context, Intent intent) {
                // TODO Auto-generated method stub
                Toast.makeText(context, "listen!", Toast.LENGTH_LONG).show();
                String msg_me = intent.getStringExtra("row");
            }

        };

        getActivity().registerReceiver(mReceiver, intentFilter);

...

1 个答案:

答案 0 :(得分:0)

您已使用空BroadcastReceiver注册了IntentFilter。它不知道该听什么,因此它永远不会被调用。

您创建了这样的广播Intent

    Intent intent = new Intent();
    //intent.setClass(getContext(), FragRoutine.class);
    intent.putExtra("row", rowpos);
    intent.putExtra("col", (int) v.getTag());
    getContext().sendBroadcast(intent);

Intent也没有ACTION,没有COMPONENT,也没有DATA。当您广播此Intent时,Android不知道如何处理它。

尝试向Intent和`IntentFilter添加唯一的ACTION。像这样:

    Intent intent = new Intent("my.special.broadcast.action");
    //intent.setClass(getContext(), FragRoutine.class);
    intent.putExtra("row", rowpos);
    intent.putExtra("col", (int) v.getTag());
    getContext().sendBroadcast(intent);

现在注册您的听众以收听此Intent行动:

    IntentFilter intentFilter =
            new IntentFilter("my.special.broadcast.action");
    mReceiver = new BroadcastReceiver(){
       ...
    };
    getActivity().registerReceiver(mReceiver, intentFilter);