将活动之间的数据传递给dialogfragment

时间:2016-06-30 02:17:51

标签: android android-fragments

我需要你对这个案子的帮助。我需要一个代码将数据从活动传递到dialogfragment(从edittext输入),然后将值(来自edittext)放在下一个活动上。

DialogFragment.class

package com.titikkosong;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.EditText;

public class InputNameDialogFragment extends DialogFragment implements OnClickListener  {
    EditText txtEstate,txtBlock,txtHa;
    String estate,block,ha;
    Button btnDone;
    static String DialogboxTitle;
    private Intent i;


     Context context; 

    public interface InputNameDialogListener {
        void onFinishInputDialog(String inputText);
    }

    //---empty constructor required
    public InputNameDialogFragment() {

        }
    //---set the title of the dialog window
    public void setDialogTitle(String title) {
    DialogboxTitle = title;
    }

    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle saveInstanceState){

        View view = inflater.inflate(R.layout.input_dialog, container,false);
                context = view.getContext(); 
                //---get the EditText and Button views
                txtEstate = (EditText) view.findViewById(R.id.txtEstate);
                txtBlock = (EditText) view.findViewById(R.id.txtBlock);
                txtHa = (EditText) view.findViewById(R.id.txtHa);

                 estate = txtEstate.getText().toString().trim(); 
                 block =txtBlock.getText().toString().trim(); 
                 ha = txtHa.getText().toString().trim(); 


                btnDone = (Button) view.findViewById(R.id.btnStart);

                //---event handler for the button
                btnDone.setOnClickListener(new View.OnClickListener(){
                    public void onClick(View view) {


                        //---gets the calling activity
                        //InputNameDialogListener activity = (InputNameDialogListener) getActivity();
                        //activity.onFinishInputDialog(txtEstate.getText().toString());
                        //activity.onFinishInputDialog(txtBlock.getText().toString());
                        //activity.onFinishInputDialog(txtHa.getText().toString());

                Intent intent = new Intent(context, MainActivity.class); 
                intent.putExtra(estate, "estate");
                intent.putExtra(block, "block");
                intent.putExtra(ha, "ha");
                startActivity(intent);

                        //---dismiss the alert
                        dismiss();
                    }
                });

                //---show the keyboard automatically
                txtEstate.requestFocus();
                getDialog().getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);

                //---set the title for the dialog
                getDialog().setTitle(DialogboxTitle);



        return view;    
    }
    @Override
    public void onClick(View v) {




    }
}

我的第一个活动

  package com.titikkosong;

    import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;
import com.titikkosong.InputNameDialogFragment;
import com.titikkosong.R;

import com.titikkosong.InputNameDialogFragment.InputNameDialogListener;


    public class FormMain extends FragmentActivity implements InputNameDialogListener {

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

        //-- InputName Dialog box
        public void btnShowDialog(View view) {
            showInputNameDialog();
        }

        private void showInputNameDialog() {
            FragmentManager fragmentManager = getSupportFragmentManager();
            InputNameDialogFragment inputNameDialog = new InputNameDialogFragment();
            inputNameDialog.setCancelable(true);
            inputNameDialog.setDialogTitle("Enter Census Identity");
            inputNameDialog.show(fragmentManager, "Input Dialog");
        }

        @Override
        public void onFinishInputDialog(String inputText) {
            // TODO Auto-generated method stub

        }



    }

我想为下一个活动设置值,该类在

之下
public class MainActivity extends Activity implements InputNameDialogListener {

    public ImageButton normal,abnormal,titik;
    public Button list,export,exit;
    public String longitude,latitude,estate,block,ha;
    public SimpleDateFormat dateFormat;
    MySQLiteHelper db;
    private String mark="",id,remarks;
    private Intent i;
    Tikos tikos= new Tikos();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.detail);
        db = new MySQLiteHelper(this);

        //pref = getApplicationContext().getSharedPreferences("data1", MODE_PRIVATE);

        i=getIntent();
        mark=i.getStringExtra("mark");

        abnormal = (ImageButton)findViewById(R.id.imageButtonAbnormal);
        titik = (ImageButton)findViewById(R.id.imageButtonVacant);
        normal = (ImageButton)findViewById(R.id.imageButtonNormal);

        list =(Button)findViewById(R.id.buttonlist);
        export=(Button)findViewById(R.id.buttonexport);
        exit = (Button)findViewById(R.id.buttonexit);

       estate =i.getStringExtra("estate");
       block =i.getStringExtra("block");
       ha =i.getStringExtra("ha");


        titik.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {


                    String type="0";
                    String date=(String) DateFormat.format("dd/MM/yy", new java.util.Date());

                     Context context=MainActivity.this; 
                     if(type.equals("") || date.equals("")||longitude.equals("")||latitude.equals("")) 
                     { String e = "Please to TURN ON your GPS Setting first!"; 

                     new AlertDialog.Builder(context) 
                     .setTitle("GPS Setting") 
                     .setMessage(e) 
                     .setNeutralButton("OK", new DialogInterface.OnClickListener() 
                     { 
                         public void onClick(DialogInterface dialog, int which) {

                             } 
                             }).show(); 
                     }else{
                        //If OK, then send the data to save 

                         tikos.setestate(estate);
                         tikos.setblock(block);
                         tikos.setha(ha);
                         tikos.settype(type);
                         tikos.setdate(date);

                         new AlertDialog.Builder(context) 
                         .setTitle("Information") 
                         .setMessage("Are you sure want to save this position ?") 
                         .setPositiveButton("OK", new DialogInterface.OnClickListener(){ 
                             public void onClick(DialogInterface dialog, int which) {
                                 // TODO Auto-generated method stub 
                                 db.addTikos(tikos);
                                 db.close();
                             Toast.makeText(getBaseContext(), " Your currently position have been saved successfully..", Toast.LENGTH_SHORT).show();
                        }
                         })
                         .setNegativeButton("No",new DialogInterface.OnClickListener() {
                         public void onClick(DialogInterface dialog,int id) {
                         dialog.cancel();

                         }
                         }).show();

                          }
                     } } ); 

和日志猫

> 06-30 09:19:02.450: E/SQLiteDatabase(20974): Error inserting
> block=null longitude=101.4314945 latitude=0.5083304 date=30/06/16
> type=2 ha=null estate=null 06-30 09:19:02.450:
> E/SQLiteDatabase(20974):
> android.database.sqlite.SQLiteConstraintException: tikos.estate may
> not be NULL (code 19) 06-30 09:19:02.450: E/SQLiteDatabase(20974):    at
> android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native
> Method) 06-30 09:19:02.450: E/SQLiteDatabase(20974):  at
> android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:973)
> 06-30 09:19:02.450: E/SQLiteDatabase(20974):  at
> android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
> 06-30 09:19:02.450: E/SQLiteDatabase(20974):  at
> android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
> 06-30 09:19:02.450: E/SQLiteDatabase(20974):  at
> android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1591)
> 06-30 09:19:02.450: E/SQLiteDatabase(20974):  at
> android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1461)
> 06-30 09:19:02.450: E/SQLiteDatabase(20974):  at
> com.titikkosong.MySQLiteHelper.addTikos(MySQLiteHelper.java:98) 06-30
> 09:19:02.450: E/SQLiteDatabase(20974):    at
> com.titikkosong.MainActivity$4$2.onClick(MainActivity.java:284) 06-30
> 09:19:02.450: E/SQLiteDatabase(20974):    at
> com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:185)
> 06-30 09:19:02.450: E/SQLiteDatabase(20974):  at
> android.os.Handler.dispatchMessage(Handler.java:99) 06-30
> 09:19:02.450: E/SQLiteDatabase(20974):    at
> android.os.Looper.loop(Looper.java:137) 06-30 09:19:02.450:
> E/SQLiteDatabase(20974):  at
> android.app.ActivityThread.main(ActivityThread.java:5419) 06-30
> 09:19:02.450: E/SQLiteDatabase(20974):    at
> java.lang.reflect.Method.invokeNative(Native Method) 06-30
> 09:19:02.450: E/SQLiteDatabase(20974):    at
> java.lang.reflect.Method.invoke(Method.java:525) 06-30 09:19:02.450:
> E/SQLiteDatabase(20974):  at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
> 06-30 09:19:02.450: E/SQLiteDatabase(20974):  at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025) 06-30
> 09:19:02.450: E/SQLiteDatabase(20974):    at
> dalvik.system.NativeStart.main(Native Method)

请帮帮我吧。 三江源。

2 个答案:

答案 0 :(得分:1)

在这里准备意图:

intent.putExtra(estate, "estate");
intent.putExtra(block, "block");
intent.putExtra(ha, "ha");

使用键作为值和值作为键导致问题,并在MainActivity中获取所有值null

将其更改为:

intent.putExtra("estate",estate);
intent.putExtra("block",block);
intent.putExtra("ha",ha);

答案 1 :(得分:0)

对话框片段中的

添加此参数

 public InputNameDialogFragment(String abc) {
//Toast abc

        }

活动传递参数

InputNameDialogFragment inputNameDialog = new InputNameDialogFragment(abcvalue);
            inputNameDialog.setCancelable(true);
            inputNameDialog.setDialogTitle("Enter Census Identity");
            inputNameDialog.show(fragmentManager, "Input Dialog");