将数据从视图寻呼机内的片段传递到另一个活动

时间:2014-06-17 10:25:33

标签: android

我希望你们能帮助我。它让我疯狂找到解决方案...... 我已经读过一些与我的问题相似的问题,但没有一个解决了我的问题。

这里是问题

1有2项活动......

first - > 我的活动包含一个包含3个标签片段的视图寻呼机。 在第一个活动中,我扩展了fragmentActivity

这里是代码

    public class A_BonRokok_Add_Main_Paged extends FragmentActivity {
    private static final String[] CONTENT = new String[] { "Header", "Item", "Info"};
    MainActivity            main = new MainActivity();
    FragmentManager         manager;
    FragmentTransaction     transaction;
    Dialog                  alert;

    LayoutInflater li;
    LinearLayout someLayout;
    Button btnSave_dialog;
    Button btnCancel_dialog;

    public EditText search;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.l_bon_rokok_add_main_paged);

        FragmentPagerAdapter adapter = new MyAdapter(getSupportFragmentManager());

        ViewPager pager = (ViewPager)findViewById(R.id.pager);
        pager.setAdapter(adapter);

        TabPageIndicator indicator = (TabPageIndicator)findViewById(R.id.indicator);
        indicator.setViewPager(pager);

        getActionBar().setDisplayHomeAsUpEnabled(true);
        pager.setOffscreenPageLimit(3);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.edit_print) {
            Toast.makeText(this, "print", Toast.LENGTH_SHORT).show();
        } 
        else if (item.getItemId() == R.id.edit_save) {
            Toast.makeText(this, "Save", Toast.LENGTH_SHORT).show();
        }
        else{
            createDialogConfirm();
        }
        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event){
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
        return true;
    }

    Button.OnClickListener dialogYes = new Button.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent(getBaseContext(),A_BonRokok_Main.class);
            startActivity(intent);
            finish();
        }
    };

    Button.OnClickListener dialogNo = new Button.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            alert.cancel();
        }
    };

    public void onBackPressed(){
        createDialogConfirm();
    }

    public void createDialogConfirm(){
        li      = LayoutInflater.from(this);
        someLayout = (LinearLayout) li.inflate(R.layout.d_global_confirm_transaksi, null);
        btnSave_dialog  = (Button) someLayout.findViewById(R.id.d_globalConfirmTrans_btnSave);
        btnCancel_dialog = (Button) someLayout.findViewById(R.id.d_globalConfirmTrans_btnCancel);

        alert = new Dialog(this);
        alert.requestWindowFeature(Window.FEATURE_NO_TITLE);
        alert.setContentView(someLayout);
        alert.getWindow().getAttributes().width = LayoutParams.FILL_PARENT;

        btnSave_dialog.setOnClickListener(dialogYes);
        btnCancel_dialog.setOnClickListener(dialogNo);

        alert.show();
    }

    public class MyAdapter extends FragmentPagerAdapter {
        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public Fragment getItem(int position) {
            Bundle args = new Bundle();
            args.putInt(ChildFragmentPaged.POSITION_KEY, position);
            return ChildFragmentPaged.newInstance(args);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return CONTENT[position % CONTENT.length].toUpperCase();
        }
    }

    public static A_BonRokok_Add_Main_Paged newInstance() {
        return new A_BonRokok_Add_Main_Paged();
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuinflate = new MenuInflater(this);
        menuinflate.inflate(R.menu.save_print, menu);
        return super.onCreateOptionsMenu(menu);
    }   


}

第一个活动使用扩展片段的类管理选项卡片段... 这里是代码

    public class ChildFragmentPaged extends Fragment {
    public static final String POSITION_KEY = "FragmentPositionKey";
    private int position;
    View root;
    static EditText txtDate, txtGudang;
    static RadioGroup btnGroupGudang;
    static RadioButton btnGudang1, btnGudang2;
    static Button btnNewItem, btnNewItem_Cancel;

    private database mySQLiteAdapter;
    private A_BonRokok_Item_View view = new A_BonRokok_Item_View();
    public ListView listContent;
    SimpleCursorAdapter cursorAdapter;
    Cursor cursor;
    AdapterView<?> tempAdt;
    int tempPos;
    public EditText search;

    public ArrayList<bonRokokPagedEntity> list;
    public ListViewAdapter adapter;
    private databasePaged databasePaged;
    public static ChildFragmentPaged newInstance(Bundle args) {
        ChildFragmentPaged fragment = new ChildFragmentPaged();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        position = getArguments().getInt(POSITION_KEY);
        if(position == 0){
            root = inflater.inflate(R.layout.t_bon_rokok_header_paged, container,false);
        }if (position == 1) {
            root = inflater.inflate(R.layout.t_bon_rokok_item_paged, container, false); 
            settingTabItem();
        } else if (position == 2)
            root = inflater.inflate(R.layout.t_bon_rokok_info_paged, container, false);
        return root;
    }

    public void settingTabItem() {
        listContent = (ListView) root.findViewById(R.id.vl_tab_paged);
        btnNewItem = (Button) root.findViewById(R.id.btnNewItem_paged);
        search = (EditText)root.findViewById(R.id.search_paged);
        try{
            databasePaged = new databasePaged(getActivity());
            databasePaged.createDataBase();
        }catch(IOException ioe){
            throw new Error("Unable to craete database");
        }
        try{
            databasePaged.openDataBase();

        }catch(SQLException sqle){
            throw sqle;
        }

        btnNewItem.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(getActivity(),A_BonRokok_Item_New_Paged.class);
                startActivity(intent);
                getActivity().finish();
            }
        });

        list = databasePaged.Getvalue();
        adapter = new ListViewAdapter(getActivity(), list);
        listContent.setAdapter(adapter);
    }

    private void updateList() {
        cursor.requery();
    }

    public void createMenu(){
        final Cursor cursor = (Cursor) tempAdt.getItemAtPosition(tempPos);
        final String header = cursor.getString(cursor.getColumnIndex(database.SKDROKOK));

        LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View contentView = inflater.inflate(R.layout.d_global_edit_delete,null, false);
        ListView lv = (ListView)contentView.findViewById(R.id.d_globalEditDelete_lvEditDelete);
        TextView txtHeader = (TextView)contentView.findViewById(R.id.d_globalEditDelete_lblHeader);
        Button btnCancel = (Button)contentView.findViewById(R.id.d_globalEditDelete_btnCancel);

        txtHeader.setText(header);
        ArrayList<String> tempData = new ArrayList<String>();
        tempData.add("Edit");
        tempData.add("Delete");

        int layoutID = android.R.layout.simple_list_item_1;
        ArrayAdapter tempAdapter = new ArrayAdapter<String>(getActivity(), layoutID, tempData);
        lv.setAdapter(tempAdapter);

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setView(contentView);
        final AlertDialog alert = builder.create();
        alert.show();

        lv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) {
                if (position == 0){ //Edit Data
                    passStringValue();
                    updateList();
                    Intent intent = new Intent(getActivity(),A_BonRokok_Item_View.class);
                    intent.putExtra("status", true);
                    startActivity(intent);
                    getActivity().finish();
                }
                else if (position == 1){ //Delete Data
                    final int item_id = cursor.getInt(cursor.getColumnIndex(database.KEY_ID));
                    mySQLiteAdapter.delete_byID(item_id);
                    updateList();
                    alert.cancel();
                }
            }
        });

        btnCancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                alert.cancel();
            }
        });
    }

    public void passStringValue(){
         final String nama_Rokok = cursor.getString(cursor.getColumnIndex(database.SKDROKOK));
         final String kode_Rokok = "102030";
         final String pita_Cukai = cursor.getString(cursor.getColumnIndex(database.SPITACUKAI));
         final String jumlah = cursor.getString(cursor.getColumnIndex(database.SJUMLAH));
         view.Detail(nama_Rokok, kode_Rokok, pita_Cukai, jumlah);
    }
}

second - &gt; 我的活动包含edittext。

这里是代码

InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                in.hideSoftInputFromWindow(contentView.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                return false;
            }
        });
//      txtSelop.setOnFocusChangeListener(focusSelopChange);
//      txtBungkus.setOnFocusChangeListener(focusBungkusChange);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.edit_save) {
            Intent intent = new Intent(getBaseContext(),A_BonRokok_Add_Main_Paged.class);
            startActivity(intent);
            finish();

        } else {
            if(txtNamaRokok.getText().length()==0){
                Intent intent = new Intent(getBaseContext(),A_BonRokok_Add_Main_Paged.class);
                startActivity(intent);
                finish();
            }
            else
            createDialogConfirm();
        }
        return false;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.save_print, menu);
        MenuItem item = menu.findItem(R.id.edit_print);
        item.setVisible(false);
        return true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
        return true;
    }

    OnClickListener searchClick = new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            createDialogSearch();
        }
    };

    OnItemClickListener itemClickListener = new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,long id) {
            @SuppressWarnings("unchecked")
            HashMap<String, String> hm = (HashMap<String, String>)arg0.getAdapter().getItem(position);
            autoCompleteBonRokok.setText(hm.get("kdRokok"));
            txtNamaRokok.setText(hm.get("namaRokok"));
        }
    };

    public void onBackPressed(){
        if(txtNamaRokok.getText().length()==0){
            Intent intent = new Intent(getBaseContext(),A_BonRokok_Add_Main.class);
            startActivity(intent);
            finish();
        }
        else
        createDialogConfirm();
    }

    Button.OnClickListener dialogYes = new Button.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent(getBaseContext(),A_BonRokok_Add_Main.class);
            startActivity(intent);
            finish();
        }
    };

    Button.OnClickListener dialogNo = new Button.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            alert.cancel();
        }
    };

    OnFocusChangeListener focusBalChange = new OnFocusChangeListener() {
        @Override
        public void onFocusChange(View arg0, boolean isFocused) {
            if(!isFocused){
                if(txtBal.length()==0)
                    bal = 0;
                else
                bal = Integer.parseInt(txtBal.getText().toString());
                splitValue();
            }
        }
    };

    public void saveToDatabase() {

    }

    private void updateList() {
        cursor.requery();
    }

    public void createDialogSearch(){
        li            = LayoutInflater.from(this);
        someLayout    = (LinearLayout)li.inflate(R.layout.d_bon_rokok_search_new_item, null);

        DialogDummyAutoComplete[] modelItemsDialog;
        final ListView lvDialog = (ListView)someLayout.findViewById(R.id.d_bonRokokSearchNewItem_lvSearch);

        modelItemsDialog     = new DialogDummyAutoComplete[3];
        modelItemsDialog [0] = new DialogDummyAutoComplete("1051200", "Supra need 12");
        modelItemsDialog [1] = new DialogDummyAutoComplete("1051600", "Supra need 16");
        modelItemsDialog [2] = new DialogDummyAutoComplete("1001200", "NY");

        DialogAutoCompleteSearchRokok dialogAdapter = new DialogAutoCompleteSearchRokok(this, modelItemsDialog);
        lvDialog.setAdapter(dialogAdapter);

        alert = new Dialog(this);
        alert.requestWindowFeature(Window.FEATURE_NO_TITLE);
        alert.setContentView(someLayout);

        alert.getWindow().getAttributes().width = LayoutParams.FILL_PARENT;
        alert.getWindow().getAttributes().height = LayoutParams.WRAP_CONTENT;
        alert.show();

        btnCancel            = (Button)someLayout.findViewById(R.id.d_bonRokokSearchNewItem_btnCancel);
        btnCancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                alert.cancel();
            }
        });


        lvDialog.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) {
                String selectedKdRokok = ((TextView)arg1.findViewById(R.id.kdRokok_dialog)).getText().toString();
                String selectedNamaRokok = ((TextView)arg1.findViewById(R.id.namaRokok_dialog)).getText().toString();
                autoCompleteBonRokok.setText(selectedKdRokok);
                txtNamaRokok.setText(selectedNamaRokok);
                alert.cancel();
            }
        });
    }

    public void splitValue(){
        if (txtJumlah.length()==0)
            txtJumlah.setText("0,000");

        separated = txtJumlah.getText().toString().split(","); 
        first = separated[0];
        second = separated[1];

        first = Integer.toString(bal);
        txtJumlah.setText(first + "," + second);
        txtJumlah.clearFocus();
    }



    public void createDialogConfirm(){
        LayoutInflater li       = LayoutInflater.from(this);
        LinearLayout someLayout = (LinearLayout) li.inflate(R.layout.d_global_confirm_transaksi, null);
        Button btnSave_dialog   = (Button) someLayout.findViewById(R.id.d_globalConfirmTrans_btnSave);
        Button btnCancel_dialog = (Button) someLayout.findViewById(R.id.d_globalConfirmTrans_btnCancel);

        btnSave_dialog.setOnClickListener(dialogYes);
        btnCancel_dialog.setOnClickListener(dialogNo);

        alert = new Dialog(this);
        alert.requestWindowFeature(Window.FEATURE_NO_TITLE);
        alert.setContentView(someLayout);
        alert.getWindow().getAttributes().width = LayoutParams.FILL_PARENT;
        alert.show();
    }


}

我的问题是 我如何将第二个活动(包含edittext)中的值传递给视图分页器中的片段,因为每当我尝试使用多种方式插入时,java lang null指针总是成为我的噩梦......

请帮助我......你这么多

1 个答案:

答案 0 :(得分:0)

   Intent.putExtra("YourValueKey", datatobepassed);
  on the other activity
 Bundle extras = getIntent().getExtras();
if ( extras != null ){
    extras.get("YourValueKey")
}