如何在register-parentcategory-childcategory-register中使用startActivityForResult?

时间:2015-11-26 09:23:27

标签: android android-intent android-activity startactivityforresult

我有一个SignUp Activity,其中我有一个字段类别,我在其中调用了一个Parent活动ListView和一个所选复选框的基础我称为子类ListView。两个ListView都是从数据库sqlite填充的。如何使用startActivityForResult?

RegisterActivity.class

spProfession.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        Intent in =new Intent(RegisterActivity.this,Signup_category.class);
                        in.putExtra("typeOfUser", typeOfUser);
                        startActivityForResult(in, 2);
                    }
                });

        @Override  
              protected void onActivityResult(int requestCode, int resultCode, Intent data)  
              {  
                        super.onActivityResult(requestCode, resultCode, data);  
                         // check if the request code is same as what is passed  here it is 2  
                          if(resultCode == RESULT_OK && requestCode == 2)  
                                {  
                                      if (data.hasExtra("parentkey")) {
                                          Toast.makeText(getApplicationContext(), "hello", Toast.LENGTH_LONG).show();
                               /*  Intent in=new Intent(RegisterActivity.this,Child_Category.class);
                                 startActivityForResult(in, requestCode);
        */                      }
                                   /*String message="heloo";   
                                   Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
                */                }  
            }

ParentCategory.class 从注册

点击类别时调用此活动
public class Signup_category extends ActionBarActivity implements
            OnItemClickListener, OnCheckedChangeListener {
        ListView listView1;
        Button btn;
        ArrayList<String> list;
        DatabaseHandler databaseHandler;
        String typeOfUser;
        Context context = null;
        CategoryAdapter<ProfessionEntity> adapter;
        CategoryAdapter<?> categoryAdapter;
        ArrayList<Integer> arr = null;
        ListView lv = null;
        ArrayList<String> selectedStrings = new ArrayList<String>();
        ArrayList<ProfessionEntity> listProfession = new ArrayList<ProfessionEntity>();
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_signup_category);
            listView1 = (ListView) findViewById(R.id.listView1);
            btn = (Button) findViewById(R.id.button1);

            listView1.setOnItemClickListener(this);
            listView1.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
            InitDatabase();
            Intent in = getIntent();
            typeOfUser = in.getStringExtra("typeOfUser");
            loadProfessionData(typeOfUser);
            CheckButtonClick();
            btn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }

        private void loadProfessionData(String pos) {
            listProfession = databaseHandler.getParentCategory(typeOfUser);
            ArrayList<String> list = new ArrayList<String>();
            for (int i = 0; i < listProfession.size(); i++) {
                Log.d("prfoession:", listProfession.get(i).getDecription());
                list.add("" + listProfession.get(i).getDecription());
            }
            adapter = new CategoryAdapter<ProfessionEntity>(Signup_category.this,listProfession);
            listView1.setAdapter(adapter);

        }

        }
        @Override
        public void finish() {

          Intent data = new Intent();
          data.putExtra("parentkey", arr);
          setResult(RESULT_OK, data);
          super.finish();
        } 
        public void CheckButtonClick(){
                     StringBuffer responseText = new StringBuffer();
                     responseText.append("The following were selected...\n");
                     ArrayList<ProfessionEntity> listentity = adapter.getCheckedItems();
                     for(int i=0;i<listentity.size();i++)
                     {
                          ProfessionEntity profs = listentity.get(i);
                           Log.d("parentkey",String.valueOf(profs.getParent_key()));
                          Log.d("selection",String.valueOf(profs.isSelected()));
                         if(profs.isSelected())
                         {
                             responseText.append("\n" + profs.getDecription());
                         }
                       arr.add(profs.getParent_key());
                     }

                }
    }

ChildCategory.class

此类调用parentcategory并根据父级的选中复选框填充列表但是......如何将该复选框项目数组从父级传递给子级

public class Child_Category extends Activity implements OnClickListener {
    Button btn;
    DatabaseHandler databaseHandler;
    CategoryChildAdapter childAdapter;
    ArrayList<ProfessionChildEntity> listChildProfession=new ArrayList<ProfessionChildEntity>();
    ListView lvChild;
    Intent intent;
    ArrayList<Integer> arr;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signup_category);
        btn = (Button) findViewById(R.id.button1);
        btn.setOnClickListener(this);
        lvChild=(ListView)findViewById(R.id.listView1);
        InitDatabase();
        Intent in = getIntent();
        arr=in.getIntegerArrayListExtra("parentkey");

    //  loadChildProfessionData(arr);
    }


    private void loadChildProfessionData(ArrayList<Integer> pos) {
        listChildProfession = databaseHandler.getPChildCategory(pos);
        ArrayList<String> list = new ArrayList<String>();
        for (int i = 0; i < listChildProfession.size(); i++) {
            Log.d("prfoession:", listChildProfession.get(i).getDescription());
            list.add("" + listChildProfession.get(i).getDescription());
        }
        childAdapter = new CategoryChildAdapter(this,listChildProfession);
        lvChild.setAdapter(childAdapter);
    }

     @Override  
     protected void onActivityResult(int requestCode, int resultCode, Intent data)  
     {  
               super.onActivityResult(requestCode, resultCode, data);  
                // check if the request code is same as what is passed  here it is 2  
                 if(requestCode==2)  
                       {  
                          String message=data.getStringExtra("MESSAGE");   
                          Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
                       }  
   }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        /*Intent in = new Intent(Child_Category.this,RegisterActivity.class);
        startActivityForResult(in, 2);*/printData(arr);
    }   
}

1 个答案:

答案 0 :(得分:0)

如果您希望将活动C的结果传递给A:

从活动A开始活动B:

Intent showB = new Intent(ActivityA, ActivityB); 
startActivityForResult(showB, RequestCode);

在活动B中调用C:

Intent showC = new Intent(ActivityC);
showC.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
startActivity(showC); 
finish(); //Close Activity B

在C:

//set the result code and close the activity
Intent result = new Intent();
setResult(resultCode, result);//like RESULT_OK
finish();

在A:

public void onActivityResult(int requestCode, int resultCode, Intent data) {

  ...... handle RequestCode here
}

从B

启动活动C时,您可能已经注意到了意图标记FLAG_ACTIVITY_FORWARD_RESULT
  

public static final int FLAG_ACTIVITY_FORWARD_RESULT从:API Level 1

     

如果设置并且此意图用于从&gt;现有活动启动新活动,则现有活动的回复目标将被&gt;转移到新活动。这样,新活动可以调用&gt; setResult(int)并将该结果发送回&gt;原始活动的回复目标。

这样A应该收回从C

发回的额外内容中发回的任何数据