gpt4 book ai didi

android - 将 Activity 之间的数据传递给 dialogfragment

转载 作者:行者123 更新时间:2023-11-29 01:18:48 25 4
gpt4 key购买 nike

关于这个案子,我需要你的帮助。我需要一个代码,用于将数据从 Activity 传递到 dialogfragment(从 edittext 输入),然后将值(从 edittext)放在下一个 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) {




}
}

我的第一个 Activity

  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

}



}

我想为下一个 Activity 设置值,类在下面

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)

请帮助我。谢谢。

最佳答案

在这里准备Intent时:

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);

关于android - 将 Activity 之间的数据传递给 dialogfragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38112916/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com