如何在寄存器parentcategory子类别寄存器中使用startActivityForResult



我有一个SignUp活动,其中我有一种字段类别,其中我称一个Parent活动为ListView,一个是所选复选框的基础,我称之为child类别为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);
    }   
}

如果您想从传递给A:的活动C中获得结果

从活动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 1级

如果设置了,并且此意图用于从>现有活动启动新活动,则现有活动的回复目标将>转移到新活动。通过这种方式,新活动可以调用>setResult(int),并将结果发送回>原始活动的回复目标。

通过这种方式,A应该拾取从C 发送回的附加数据中发送回的任何数据

相关内容

  • 没有找到相关文章

最新更新