I am looking for a way to display some data that are retrieved with a loader from an SQLite DB in an AlertDialog. Clicking a name in a ListView will transfer the DB entry ID to the DialogFragment, and based on this, additional data should be loaded.
The loading as such does work but it seems to happen after the AlertDialog is displayed because the message box only shows Null as a value. Does anybody know if it is possible at all to transfer data into a AlertDialog like this? Thanks for your help!
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ContentUris;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
#SuppressLint("SimpleDateFormat")
public class BirthdayDetailsMessage extends DialogFragment implements
LoaderCallbacks<Cursor> {
static String DATE_FORMAT;
static final String CALENDAR = "calendar";
private long mRowId;
private String mName;
private String mDate;
private boolean mYearKnown;
private String mAge;
private Calendar mCalendar;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mCalendar = Calendar.getInstance();
DATE_FORMAT = PrefDateFormat.getPrefDateFormat(getActivity());
Bundle arguments = getArguments();
mRowId = arguments.getLong(ReminderProvider.COLUMN_ROWID);
getLoaderManager().initLoader(0, null, this);
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState){
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Birthday: "+ mDate +"\n\n"+"Age: "+ mAge)
.setTitle(mName)
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
return builder.create();
}
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return new CursorLoader(getActivity(), ContentUris.withAppendedId(
ReminderProvider.CONTENT_URI, mRowId), null, null, null, null);
}
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor reminder) {
if (reminder.getCount() == 0) {
new Handler().post(new Runnable() {
#Override
public void run() {
((OnFinishEditor) getActivity()).finishEditor();
}
});
return;
}
mName = reminder.getString(reminder
.getColumnIndexOrThrow(ReminderProvider.COLUMN_NAME));
mYearKnown = (reminder.getInt(reminder
.getColumnIndexOrThrow(ReminderProvider.COLUMN_YEAR))==0? false:true);
Long dateInMillis = reminder.getLong(reminder
.getColumnIndexOrThrow(ReminderProvider.COLUMN_DATE));
Date date = new Date(dateInMillis);
mCalendar.setTime(date);
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
mDate = dateFormat.format(mCalendar.getTime());
if (mYearKnown == true){
mAge = String.valueOf(GetDiffYears.getDiffYears(mCalendar));
}else{
mAge = "age not known";
}
}
#Override
public void onLoaderReset(Loader<Cursor> arg0) {
// nothing to reset for this fragment
}
}
EDIT:
I removed the onCreateDialog method and used the following View instead
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.dialog_fragment, container,
false);
getDialog().setTitle("Name");
Date = (TextView) v.findViewById(R.id.date);
Age = (TextView) v.findViewById(R.id.age);
getLoaderManager().initLoader(0, null, this);
return v;
}
private void updateValues() {
getDialog().setTitle(mName);
Date.setText("Geburtstag: "+ mDate);
Age.setText("Alter: " + mAge);
}
In included updateValues() in onLoadFinished, and created an layout XML for the message body...this way it seems to work properly.
What you will want to do is use a call back method from your Loader to create the dialog. I would not put the Loader in the dialog class, but either in the calling class or as a separate class. Then call/create the dialog in onLoadFinished. The implementation would be similar with AsyncTask.
Alternately, you could create the dialog with some default text, and retain a reference to it in your calling class. Then in your onLoadFinished update the values of the dialog. I'm pretty sure that one would work, but I haven't tested it.
Related
I am working on a project which populating SQLite database table for list view and using simple array adapter.
I'm using Asyntask for that purpose and I have problem when:
I want to call another activity and
pass some values which I get from the setOnItemClickListener
I need to archive this two things in onPostExecute setOnItemClickListener method. This is my code for that.
package com.me.doctor.doctor_me;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
public class BackgroundTask extends AsyncTask<String,Doctor,String> {
Context ctx;
DoctorAdapter doctorAdapter;
Activity activity;
ListView listView;
Doctor doctor;
DisplayDoctor displayDoctor;
BackgroundTask(Context ctx){
this.ctx = ctx;
activity = (Activity) ctx;
doctor = new Doctor();
displayDoctor = new DisplayDoctor();
}
#Override
protected String doInBackground(String... strings) {
String method = strings[0];
DatabaseOperation databaseOperation = new DatabaseOperation(ctx);
if(method.equals("get_info")){
listView = activity.findViewById(R.id.display_list_view);
SQLiteDatabase db = databaseOperation.getReadableDatabase();
Cursor cursor = databaseOperation.getInformation(db);
doctorAdapter = new DoctorAdapter(ctx,R.layout.display_doctor_row);
String name, category, hospital;
int id;
while(cursor.moveToNext()){
id = cursor.getInt(cursor.getColumnIndex("d_id"));
name = cursor.getString(cursor.getColumnIndex("d_name"));
category = cursor.getString(cursor.getColumnIndex("d_category"));
hospital = cursor.getString(cursor.getColumnIndex("d_hospital"));
Doctor doctor = new Doctor(id,name,category,hospital);
publishProgress(doctor);
}
return "get_info";
}
return null;
}
#Override
protected void onProgressUpdate(Doctor... values) {
// add each of doctor class object add method inside the adapter class
doctorAdapter.add(values[0]);
}
#Override
protected void onPostExecute(String s) {
if(s.equals("get_info")){
listView.setAdapter(doctorAdapter);
listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
final int position, long id) {
Doctor doctor = (Doctor)parent.getItemAtPosition(position);
String ID = Integer.toString(doctor.getId());
Toast.makeText(ctx,ID,Toast.LENGTH_LONG).show();
// I need fire another activity and pass some values which i getting here
}
});
}else{
Toast.makeText(ctx,s,Toast.LENGTH_LONG).show();
}
}
}
And this is the class which call to the AsyncTask Class
package com.me.doctor.doctor_me;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class DisplayDoctor extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display_doctor_layout);
BackgroundTask backgroundTask = new BackgroundTask(this);
backgroundTask.execute("get_info");
}
}
I had investigate closed question on Stack Overflow, but I did not found a solution.
Short answer:
you already have context in Background task
Context ctx;
use this this to call next activiry
ctx.startActivity(nextActivityIntent)
u can add values to the intent like this
Intent nextActivityIntent = new Intent(ctx,NextActivity.class);
nextActivityIntent.putExtra("data", "some data");
with async task i guess you are trying to query data base on another thread
You can use loaders for the same
Loaders run on separate thread
here is a simple example of cursor loader
example taken from github
public class ForecastFragment extends Fragment implements LoaderManager.LoaderCallbacks {
public static final int LOADER_ID = 0;
private ArrayAdapter<String> forecastAdapter;
private ForecastAdapter mForecastAdapter;
public ForecastFragment() { }
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
getLoaderManager().initLoader(LOADER_ID, null, this);
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
//some database query
}
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
//some action
}
#Override
public void onLoaderReset(Loader<Cursor> loader) {
}
}
As a part of The Big Nerd Ranch's Android Guide example Criminal Intent,
I am updating a button with date/time entered in the
Date/TimePicker. Button and Date/TimePicker reside in different
fragments. I am transferring data between them using an Intent. It
works fine on Pre-Lollipop devices but doesn't seem to update button
text on Android 5.0 or Lollipop devices. What am I missing?
Full code can be seen at Github.
CrimeFragment.java
package com.sudhirkhanger.android.criminalintent;
import java.util.Date;
import java.util.UUID;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
public class CrimeFragment extends Fragment {
private Crime mCrime;
private EditText mTitleField;
private Button mDateButton;
private Button mTimeButton;
private CheckBox mSolvedCheckBox;
private static final String TAG = "CriminalFragment";
public static final String EXTRA_CRIME_ID = "com.sudhirkhanger.android.criminalintent.crime_id";
private static final String DIALOG_DATE = "date";
private static final String DIALOG_TIME = "time";
private static final int REQUEST_DATE = 0;
private static final int REQUEST_TIME = 1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
UUID crimeId = (UUID) getArguments().getSerializable(EXTRA_CRIME_ID);
mCrime = CrimeLab.get(getActivity()).getCrime(crimeId);
}
private void updateDateAndTime() {
Date d = mCrime.getDate();
CharSequence c = DateFormat.format("EEEE, MMM dd, yyyy", d);
CharSequence t = DateFormat.format("h:mm a", d);
mDateButton.setText(c);
mTimeButton.setText(t);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_crime, parent, false);
mTitleField = (EditText) v.findViewById(R.id.crime_title);
mTitleField.setText(mCrime.getTitle());
mTitleField.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence c, int start, int before,
int count) {
mCrime.setTitle(c.toString());
}
public void beforeTextChanged(CharSequence c, int start, int count,
int after) {
// This space intentionally left blank
}
public void afterTextChanged(Editable c) {
// This one too
}
});
mDateButton = (Button) v.findViewById(R.id.crime_date);
mDateButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
FragmentManager fm = getActivity().getSupportFragmentManager();
DatePickerFragment dialog = DatePickerFragment
.newInstance(mCrime.getDate());
dialog.setTargetFragment(CrimeFragment.this, REQUEST_DATE);
dialog.show(fm, DIALOG_DATE);
}
});
mTimeButton = (Button) v.findViewById(R.id.crime_time);
mTimeButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
FragmentManager fm = getActivity().getSupportFragmentManager();
TimePickerFragment dialog = TimePickerFragment
.newInstance(mCrime.getDate());
dialog.setTargetFragment(CrimeFragment.this, REQUEST_TIME);
dialog.show(fm, DIALOG_TIME);
}
});
updateDateAndTime();
mSolvedCheckBox = (CheckBox) v.findViewById(R.id.crime_solved);
mSolvedCheckBox.setChecked(mCrime.isSolved());
mSolvedCheckBox
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// Set the crime's solved property
mCrime.setSolved(isChecked);
}
});
return v;
}
public static CrimeFragment newInstance(UUID crimeId) {
Bundle args = new Bundle();
args.putSerializable(EXTRA_CRIME_ID, crimeId);
CrimeFragment fragment = new CrimeFragment();
fragment.setArguments(args);
return fragment;
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != Activity.RESULT_OK)
return;
if (requestCode == REQUEST_DATE) {
Date date = (Date) data
.getSerializableExtra(DatePickerFragment.EXTRA_DATE);
mCrime.setDate(date);
updateDateAndTime();
} else if (requestCode == REQUEST_TIME) {
Date date = (Date) data
.getSerializableExtra(TimePickerFragment.EXTRA_TIME);
mCrime.setDate(date);
updateDateAndTime();
}
}
}
DatePickerFragment.java
package com.sudhirkhanger.android.criminalintent;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.View;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
public class DatePickerFragment extends DialogFragment {
public static final String EXTRA_DATE = "com.sudhirkhanger.android.criminalintent.date";
private Date mDate;
public static DatePickerFragment newInstance(Date date) {
Bundle args = new Bundle();
args.putSerializable(EXTRA_DATE, date);
DatePickerFragment fragment = new DatePickerFragment();
fragment.setArguments(args);
return fragment;
}
private void sendResult(int resultCode) {
if (getTargetFragment() == null)
return;
Intent i = new Intent();
i.putExtra(EXTRA_DATE, mDate);
getTargetFragment().onActivityResult(getTargetRequestCode(),
resultCode, i);
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
mDate = (Date) getArguments().getSerializable(EXTRA_DATE);
// Create a Calendar to get the year, month, and day
final Calendar calendar = Calendar.getInstance();
// calendar.setTime(mDate);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
View v = getActivity().getLayoutInflater().inflate(
R.layout.dialog_date, null);
DatePicker datePicker = (DatePicker) v
.findViewById(R.id.dialog_date_datePicker);
datePicker.init(year, month, day, new OnDateChangedListener() {
public void onDateChanged(DatePicker view, int year, int month,
int day) {
// Translate year, month, day into a Date object using a
// calendar
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
mDate = new GregorianCalendar(year, month, day, hour, minute)
.getTime();
// Update argument to preserve selected value on rotation
getArguments().putSerializable(EXTRA_DATE, mDate);
}
});
return new AlertDialog.Builder(getActivity())
.setView(v)
.setTitle(R.string.date_picker_title)
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
sendResult(Activity.RESULT_OK);
}
}).create();
}
}
The problem isn't with the communication between the Fragments, it's that for some reason the default CalendarView DatePicker in Lollipop never calls the OnDateChanged() method, so mDate in DatePickerFragment.java never gets changed; DatePickerFragment sends back to CrimeFragment the same value it received from CrimeFragment, so it only looks like the communication between the two Fragments isn't working.
The Lollipop DatePicker does call OnDateChanged() when it's in SpinnerView mode, so CrimeFragment's date will then get properly updated. Unfortunately, Lollipop ignores the android:calendarViewShown="false" directive in the DatePicker's tag in the dialog_date.xml file. The only way to force the DatePicker to display as spinners is to include a android:datePickerMode="spinner" directive in the DatePicker's xml tag. You will get a warning that this directive is supported in API 21 only, but it will be ignored by devices running pre-5.0 Android.You have to keep the calendarViewShown="false" directive for pre-5.0 devices.
DatePickerDialog appears to have been fixed in Android 5.0, so you could also use that resource in DatePickerFragment to make things work.
I have 1 Activity showing 1 DialogFragment and I use setAlwaysFinish app to force the Activity to get killed after onPause.
After pausing & restoring the Activity, I sometimes got 2 or 3 of the same DialogFragment shown. Does anybody know how to prevent this?
Note: I left the Activity with the Dialog still showing. The bug's revealed after several trying with setAlwaysFinish.
Code: Activity
package com.example.testdialog;
import java.util.Calendar;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.View;
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void showDialog(View v) {
Calendar today = Calendar.getInstance();
DialogFragment d = DateDialog.newInstance(today.get(Calendar.DAY_OF_MONTH), today.get(Calendar.MONTH), today.get(Calendar.YEAR));
d.show(getSupportFragmentManager(), "mydialog");
}
}
Code: DialogFragment
package com.example.testdialog;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.widget.DatePicker;
public class DateDialog extends DialogFragment implements OnDateSetListener {
public static final String TAG = "datepicker";
public static final int SEARCH_FLIGHT = 0;
public static final int PASSENGER = 1;
static DateDialog newInstance(int day, int month, int year) {
DateDialog d = new DateDialog();
Bundle bun = new Bundle();
bun.putInt("day", day);
bun.putInt("month", month);
bun.putInt("year", year);
d.setArguments(bun);
return d;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Bundle bun = getArguments();
int year = bun.getInt("year");
int month = bun.getInt("month");
int day = bun.getInt("day");
DatePickerDialog d = new DatePickerDialog(getActivity(), this, year, month, day);
d.setButton(DialogInterface.BUTTON_POSITIVE, "OK", d);
d.setTitle("my dialog");
return d;
}
#Override
public void onDestroyView() {
Dialog d = getDialog();
if (d != null && getRetainInstance())
d.setDismissMessage(null);
super.onDestroyView();
}
}
You can prefent it by using this in your dialogfragment:
#Override
public void show(FragmentManager manager, String tag) {
if (tag != null && tag.equals("FRAGMENT_TAG_MAX_ONE_INSTANCE")) {
// we do not show it twice
if (manager.findFragmentByTag(tag) == null) {
super.show(manager, tag);
}
} else {
super.show(manager, tag);
}
}
and changing:
d.show(getSupportFragmentManager(), "FRAGMENT_TAG_MAX_ONE_INSTANCE");
I have same issue and dont know real way to solve it but this way can help you
private void showLostPassDialog() {
if (!dialogShown) {
dialogShown = true;
DialogFragment newFragment = new LostPassDialogFragment();
newFragment.setCancelable(false);
newFragment.show(getSupportFragmentManager(), "dialog");
}
}
and when destroy dialog set dialogShown=false as i did :
public static class LostPassDialogFragment extends SherlockDialogFragment {
....
#Override
public void onDestroy() {
// TODO Auto-generated method stub
dialogShown = false;
super.onDestroy();
}
hope to b e useful
Whenever you close the dialog, close it properly by calling
dialog.dismiss();
dialog.cancel();
For me this same problem occurred when I usedgetDialog().setOnDismissListener() inside DialogFragment.
After going through DialogFragment's documentation I found this:
Note: DialogFragment own the Dialog.setOnCancelListener and
Dialog.setOnDismissListener callbacks. You must not set them yourself.
To find out about these events, override onCancel(DialogInterface) and
onDismiss(DialogInterface) Source: here
Removing getDialog().setOnDismissListener() and implementing onDismiss(DialogInterface) fixed this.
I want to update listview in fragment from main activity after selecting item from dialog.
I am using FragmentPagerAdapter in this Activity
My Dialog method
private void showDialog(String routeName) {
MyTTCDB db = new MyTTCDB(this);
List<String> directions = db.getDirectionByRoute(routeName);
AlertDialog.Builder builderSingle = new AlertDialog.Builder(this);
builderSingle.setTitle("Direction");
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this, android.R.layout.select_dialog_singlechoice);
for (String string : directions) {
arrayAdapter.add(string);
}
builderSingle.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
builderSingle.setAdapter(arrayAdapter,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
String side = arrayAdapter.getItem(which);
FragmentStops fragment = (FragmentStops) mSectionsPagerAdapter
.getItem(1);
fragment.refresh(side);
}
});
builderSingle.show();
}
My Fragment
import java.util.List;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import com.anilvasani.myttc.StopsActivity.Interface_FragmentStops;
import com.anilvasani.myttc.adapter.AdapterStops;
import com.anilvasani.myttc.data.MyTTCDB;
import com.anilvasani.myttc.models.Stop;
public class FragmentStops extends Fragment {
View view;
AdapterStops myAdapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragement_stops, container, false);
return view;
}
public void refresh(String side) {
// TODO Auto-generated method stub
String routeName = getActivity().getIntent()
.getStringExtra("routeName").toString();
ListView listStops = (ListView) view.findViewById(R.id.dialog_listView);
// EditText txtSearch=(EditText)
// view.findViewById(R.id.dialog_txtSearch);
MyTTCDB db = new MyTTCDB(getActivity());
List<Stop> allStops = db.getAllStopsByRouteAndSide(routeName, side);
myAdapter = new AdapterStops(getActivity(), allStops);
listStops.setAdapter(myAdapter);
}
}
I am getting errors like...
12-14 23:43:02.696: E/MessageQueue-JNI(7577): java.lang.NullPointerException
12-14 23:43:02.696: E/MessageQueue-JNI(7577): at com.anilvasani.myttc.FragmentStops.refresh(FragmentStops.java:34)
12-14 23:43:02.696: E/MessageQueue-JNI(7577): at com.anilvasani.myttc.StopsActivity$3.onClick(StopsActivity.java:120)
12-14 23:43:02.696: E/MessageQueue-JNI(7577): at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:941)
So please help me, How to call method of Fragment from Activity based on user's input.
It seems your error is coming from the line:
String routeName = getActivity().getIntent()
.getStringExtra("routeName").toString();
Make sure of the following things:
That when you started the activity that you calling refresh method (the activity that the dialog is attached to) that you start the activity in an intent since getIntent() is trying to return that intent.
Next make sure that there is a String named "routeName" and that that String is not null.
I hope this helps
I made an Activity for searching people that also shows history of recent research.
If I long click on an item of the history it asks me if I want to delete it. If I press "Yes" it deletes the list item.
So, I write something and click to "Search" button. This brings me in another Activity with results. Here I click on result so it stores the person info and brings me in the person page.
When I come back I don't see the new person in the history.
So I overwritten onResume() but it still not work and now I cannot delete items from the history list.
Here the code:
package com.lpsmt.proffinder;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import com.lpsmt.R;
public class HomeActivity extends Activity
{
protected Db db = null;
protected List<ProfBean> historyProfs = null;
protected ProfListItemAdapter listAdapter = null;
protected ListView listView = null;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.db = new Db(this);
this.setContentView(R.layout.prof_finder_home);
this.historyProfs = this.db.getHistory(-1); // -1 means with no limits
this.listAdapter = new ProfListItemAdapter(HomeActivity.this, R.id.prof_finder_history_list_view, this.historyProfs);
this.listView = (ListView) this.findViewById(R.id.prof_finder_history_list_view);
listView.setAdapter(this.listAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(HomeActivity.this, ProfPageActivity.class);
Bundle bundle = new Bundle();
bundle.putString("profId", HomeActivity.this.historyProfs.get(position).getProfId());
intent.putExtras(bundle);
HomeActivity.this.startActivity(intent);
}
});
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id)
{
Resources resources = HomeActivity.this.getResources();
String title = resources.getString(R.string.prof_finder_history_delete_title);
String message = resources.getString(R.string.prof_finder_history_delete_message);
AlertDialog.Builder adb=new AlertDialog.Builder(HomeActivity.this);
adb.setTitle(title);
adb.setMessage(message);
final int positionToRemove = position;
String positive = resources.getString(R.string.prof_finder_history_delete_positive);
String negative = resources.getString(R.string.prof_finder_history_delete_negative);
adb.setNegativeButton(negative, null);
adb.setPositiveButton(positive, new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
ProfBean prof = HomeActivity.this.historyProfs.get(positionToRemove);
HomeActivity.this.db.deleteProf(prof.getProfId());
HomeActivity.this.historyProfs.remove(positionToRemove);
HomeActivity.this.runOnUiThread(new Runnable() {
public void run() {
HomeActivity.this.listAdapter.notifyDataSetChanged();
}
});
}});
adb.show();
return true;
}
});
}
public void searchProf(View view) throws Exception
{
EditText queryEditText = (EditText) this.findViewById(R.id.prof_finder_search_query);
String query = queryEditText.getText().toString().trim();
queryEditText.setText(query);
if (query.length() < 3) {
String message = this.getResources().getString(R.string.prof_finder_query_too_short);
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
return;
}
Intent intent = new Intent(HomeActivity.this, SearchResultActivity.class);
Bundle bundle = new Bundle();
bundle.putString("query", query);
intent.putExtras(bundle);
this.startActivity(intent);
}
public void onResume()
{
super.onResume();
this.historyProfs = this.db.getHistory(-1);
this.listAdapter.notifyDataSetChanged();
}
}
You haven't set any new data to list view. Thats why your new contact isn't added to the list after notifyDataSetChanged(). You need to add some method into adapter like
setData(List<ProfBean> data)
{
this.currentAdaptersList= data;
}
and then call notifyDataSetChanged(). So the final onResume will be :
public void onResume()
{
super.onResume();
this.historyProfs = this.db.getHistory(-1);
this.listAdapter.setData(this.historyProfs);
this.listAdapter.notifyDataSetChanged();
}
Enjoy.
And using onResume() for this task is bad idea. Is better to use onActivityResult.
notifyDataSetChanged() didn't work for me either. I was able to solve this a little bit differently:
I use OnStart() (in a derived class from Fragment)
I use setNotifyOnChange() of the ArrayAdapter:
ListView listView = (ListView) findViewById(R.id.logListView);
listView.setAdapter(logAdapter);
logAdapter.setNotifyOnChange(true);
I create the adapter once:
logAdapter = new ArrayAdapter(activity, android.R.layout.simple_list_item_1, activity.logMessages);
in onViewCreated().