Reputation: 95
i need your help about this case. i need a code for passing data from activity to dialogfragment (input from edittext) then the values (from edittext) are put on the next activity.
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) {
}
}
My firstActivity
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
}
}
And i want to put values for the next activity, the class is below
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();
}
} } );
and the log cat
> 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)
Please Help me on it. Thankyou.
Upvotes: 3
Views: 2009
Reputation: 2088
in dialog fragment add this parameter
public InputNameDialogFragment(String abc) {
//Toast abc
}
in activity pass parameters
InputNameDialogFragment inputNameDialog = new InputNameDialogFragment(abcvalue);
inputNameDialog.setCancelable(true);
inputNameDialog.setDialogTitle("Enter Census Identity");
inputNameDialog.show(fragmentManager, "Input Dialog");
Upvotes: 0
Reputation: 132992
When preparing Intent here:
intent.putExtra(estate, "estate");
intent.putExtra(block, "block");
intent.putExtra(ha, "ha");
using keys as values and values as keys causing issue and in MainActivity getting all values null
.
Change it as:
intent.putExtra("estate",estate);
intent.putExtra("block",block);
intent.putExtra("ha",ha);
Upvotes: 1