I have been working and messing with my code from last two days.
I have a List View in a Fragment and a Button .
On clicking a button i add the data to List View entered through Dialog
What I am unable to do is refresh the ListView after clicking Ok Button.
Here is my Code
However it loads when i exit the app and resume again
Any Help will be appreciated
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
int title = getArguments().getInt("title");
final EditText input = new EditText(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
return new AlertDialog.Builder(getActivity())
.setView(input)
.setIcon(R.drawable.ic_launcher)
.setTitle(title)
.setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
String Value = input.getText().toString();
String normalizednumber = NormalizePhoneUtilities
.normalizePhoneNumber(Value);
ContactModel contactModel = new ContactModel(
"Hitesh", normalizednumber, String
.valueOf(3));
ContactsDatabaseWorker sm = new ContactsDatabaseWorker(
getActivity());
sm.AddSmartContact(contactModel);
databaseWorker = new ContactsDatabaseWorker(
getActivity());
// LinkedList<Cursor> list = new
// LinkedList<Cursor>();
// list=databaseWorker.getAllData();
run = new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
customAdapter = new CustomCursorAdapter(
getActivity(), databaseWorker
.getAllData());
customAdapter.notifyDataSetChanged();
listview.invalidateViews();
listview.refreshDrawableState();
listview.setAdapter(customAdapter);
}
};
getActivity().runOnUiThread(run);
isModal = false;
}
})
.setNegativeButton(R.string.alert_dialog_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
}
}).create();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (isModal) // AVOID REQUEST FEATURE CRASH
{
View view = inflater.inflate(R.layout.smartalert_fragment,
container, false);
listview = (ListView) view.findViewById(R.id.lvSmartAlert);
return super.onCreateView(inflater, container, savedInstanceState);
}
View view = inflater.inflate(R.layout.smartalert_fragment, container,
false);
Button bAdd = (Button) view.findViewById(R.id.bAddContact);
bAdd.setOnClickListener(this);
listview = (ListView) view.findViewById(R.id.lvSmartAlert);
new Handler().post(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
customAdapter = new CustomCursorAdapter(getActivity(),
databaseWorker.getAllData());
listview.setAdapter(customAdapter);
}
});
return view;
}
Call notifyDataSetChanged (); in adapter.
Your CursorAdapter has a swapCursor() method, which you can use each time you get an updated cursor.
Related
In my app I am trying to activate phonecall on a Item click for a list item click. But this code is not working at all. After running this code whenever I click on phone call the app is crashing . I have attached my crash report here. As I am very new in android developing. I am not sure how to get rid of thi serror.
My code is-
public class AlertDialogFragment extends DialogFragment {
private ListView listView1;
private ListView listView2;
private Button cancelButton1;
private Button cancelButton2;
private String[] companyName;
private String[] actionName;
private ArrayAdapter<String> adapter;
protected FragmentActivity mActivity;
public AlertDialogFragment(){
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setCancelable(true);
setStyle(DialogFragment.STYLE_NORMAL, R.style.DialogStyle);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.first_alertlist_contact, container, false);
//Set Title Dialog
getDialog().setTitle("Contact");
//Button,ListView1 Initialization
listView1=(ListView) rootView.findViewById(R.id.listView1);
cancelButton1=(Button) rootView.findViewById(R.id.cancel_button1);
// Defined Array values to show in ListView
companyName = getResources().getStringArray(R.array.company_name);
//Create and set Adepter TO ListView1
adapter=new ArrayAdapter<String>(getActivity(), R.layout.first_alertlist_textstyle,android.R.id.text1,companyName);
listView1.setAdapter(adapter);
cancelButton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dismiss();
}
});
listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
int itemPosition = position;
if(itemPosition == 0)
{
dismiss();
showDialog2();
}
if(itemPosition == 1)
{
dismiss();
showDialog2();
}
if(itemPosition == 2)
{
dismiss();
showDialog2();
}
.....
}
});
return rootView;
}
private void showDialog2(){
final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this.getActivity(), R.style.DialogStyle);
LayoutInflater inflater = this.getActivity().getLayoutInflater();
View dialogView = inflater.inflate(R.layout.second_alertlist_contact, null);
dialogBuilder.setView(dialogView);
listView2 = (ListView) dialogView.findViewById(R.id.listView2);
cancelButton2=(Button) dialogView.findViewById(R.id.cancel_button2);
// Defined Array values to show in ListView
actionName = getResources().getStringArray(R.array.contact_way);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(),
R.layout.first_alertlist_textstyle, android.R.id.text1, actionName);
listView2.setAdapter(adapter);
listView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
int itemPosition = position;
if(itemPosition == 0)
{
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("+1234667");
//builder.setMessage("Are you sure you want to log out?");
builder.setPositiveButton("Call", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// close the dialog, go to login page
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:1234567"));
startActivity(callIntent);
dialog.dismiss();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}
if(itemPosition == 1)
{
dismiss();
System.out.println("Hello");;
}
if(itemPosition == 2)
{
dismiss();
System.out.println("Hello");;
}
if(itemPosition == 3)
{
dismiss();
System.out.println("Hello");;
}
}
});
dialogBuilder.show();
}
}
07-10 15:40:49.134 12970-12970/com.testgrid E/InputEventReceiver: Exception dispatching input event.
07-10 15:40:49.134 12970-12970/com.testgrid E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
07-10 15:40:49.134 12970-12970/com.testgrid E/MessageQueue-JNI:
java.lang.NullPointerException: Attempt to invoke virtual method
'android.content.res.Resources$Theme android.content.Context.getTheme()' on a null object reference
at
android.support.v7.app.AlertDialog.resolveDialogTheme(AlertDialog.java:113)
at
android.support.v7.app.AlertDialog$Builder.<init>(AlertDialog.java:291)
at
com.testgrid.AlertDialogFragment$3.onItemClick(AlertDialogFragment.java:155)
at
android.widget.AdapterView.performItemClick(AdapterView.java:310)
You need some permissions forr making calls, put this in your manifest
<uses-permission android:name="android.permission.CALL_PHONE" />
maybe this tutorial helps https://www.mkyong.com/android/how-to-make-a-phone-call-in-android/
Use below code to make dialog and call button :
Put context youractivityname.this or if you are in a fragment getActivity().
AlertDialog.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder = new AlertDialog.Builder(context, android.R.style.Theme_Material_Dialog_Alert);
} else {
builder = new AlertDialog.Builder(context);
}
builder.setTitle("Title")
.setMessage("some message")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// call specific number
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:1234567"));
startActivity(callIntent);
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do anything
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
source
I show items from my SQLite database in a Listview. For adding Items I use a AlertDialog. In the ALertDialog I save the item in my database and refresh the List (userLists) with the data from the database. Then I use notifyDataSetChanged but the view does't change.
public class MediUserListsFragment extends Fragment {
private MediDAO mediDAO;
private ListView listView;
private List<UserList> userLists;
private ArrayAdapter<UserList> adapter;
#Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
getActivity().setTitle("Medikamenten Listen");
final View view = inflater.inflate(R.layout.fragment_medi_user_list, container, false);
setHasOptionsMenu(true);
mediDAO = new MediDAO(getContext());
userLists = mediDAO.getAllUserList();
listView = (ListView) view.findViewById(R.id.list_view);
adapter = new ArrayAdapter<>(getContext(),android.R.layout.simple_list_item_1, userLists);
listView.setAdapter(adapter);
Button newLstBtn = (Button) view.findViewById(R.id.new_list_button);
newLstBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View alertLayout = inflater.inflate(R.layout.dialog_medi_list_new, null);
final EditText newLstName = (EditText) alertLayout.findViewById(R.id.newListName);
AlertDialog.Builder alert = new AlertDialog.Builder(getContext());
alert.setTitle("Neue Liste");
alert.setView(alertLayout);
alert.setCancelable(false);
alert.setNegativeButton("Abbrechen", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
alert.setPositiveButton("Erstellen", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
mediDAO.newUserList(newLstName.getText().toString());
userLists = mediDAO.getAllUserList();
adapter.notifyDataSetChanged();
}
});
AlertDialog dialog = alert.create();
dialog.show();
}
});
return view;
}
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.fragment_medi_abc, menu);
}
}
In your setPositiveButton, try the following.
userLists.clear();
userLists.addAll(mediDAO.getAllUserList());
adapter.notifyDataSetChanged();
When you were doing userLists = mediDAO.getAllUserList(); the adapter looses the original data object's reference (which is userLists). With userLists.addAll(), the adapter knows the data object it created with has changed hence, notifyDataSetChanged finds changes to notify.
I have an application with a listview and a button on screen that opens a dialog box. Dialog box has one EditText field where user can input text and click Add. onClickListener for Add button makes a async call and passes text to api. Problem is EditText value is not being saved. I cant even get it to show in the Log. Im sure Im missing something simple. Your help is imprecated. Here is the code.
SignInActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_in);
Log.v("SignInActivity","Activity has began");
final DancerAdapter adapter = new DancerAdapter(this,oneDancerArrayList,1);
Log.v("SignInActivity","DancerAdapter");
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
getGirlList();
nameInput = (EditText) findViewById(R.id.cr_room_name);
loginDancer = (Button) findViewById(R.id.addDancer);
loginDancer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
DialogFragment newFragment = new AddDancerDialog();
newFragment.show(getFragmentManager(), "newDancer");
}
});
static public class AddDancerDialog extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(R.layout.signin_dialog);
LayoutInflater inflater = LayoutInflater.from(getContext());
final View dialogview = inflater.inflate(R.layout.signin_dialog, null);
//final EditText dancerName = (EditText)dialogview.findViewById(R.id.etDancerName);
builder.setMessage("Login New Dancer")
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
final ProgressBar newProgress = (ProgressBar)dialogview.findViewById(R.id.progressBar);
final EditText dancerName = (EditText)dialogview.findViewById(R.id.etDancerName);
String nameSubmit = dancerName.getText().toString();
Log.v("SignInActivity-Dialog",nameSubmit);
newProgress.setVisibility(View.VISIBLE);
AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
params.put("action", "addDancer");
params.put("name",nameSubmit);
Log.v("SignInActivity-Dialog", "Add Dancer Function");
Log.v("SignInActivity-Dialog",dancerName.getText().toString());
client.post("http://peekatu.com/apiweb/girlList.php", params,
new AsyncHttpResponseHandler() {
#Override
public void onSuccess(String response) {
Log.v("response", response);
responseString2 = response;
//parseDancerList(response);
Log.v("SignInActivity",response);
}
#Override
public void onFailure(Throwable error, String content) {
Log.v("response", "response failed network error");
//waitncall(true);
}
});
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
return builder.create();
}
}
}
Problem is here:
Here you set a view to dialog
builder.setView(R.layout.signin_dialog);
Then, you search a EditText in this view:
LayoutInflater inflater = LayoutInflater.from(getContext());
final View dialogview = inflater.inflate(R.layout.signin_dialog, null);
final EditText dancerName = (EditText)dialogview.findViewById(R.id.etDancerName);
That dialogView was inflated but it is not the view that was added to
dialog. They are different..
I think you can fix as follows:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = LayoutInflater.from(getContext());
final View dialogview = inflater.inflate(R.layout.signin_dialog, null);
builder.setView(dialogview);
...
}
on click item is not working inside dialog..
Description: i have one print button when i click that button a dialog will appear which contains listview using adapter.. when i click on listview row it must go to other activity.. But problem is onitemclick is not working in listview dialog..
thank u in advance.
WorkDetailView.java
printbutton = (Button) findViewById(R.id.barcodePrintButton);
printbutton.setOnClickListener(new OnClickListener() {
private Context context = WorkDetailView.this;
/**
*
* Description: barcodePrintButton onClick to ServiceManager.callPrintService
*
*/
#Override
public void onClick(View view) {
dialog = new Dialog(context);
dialog.setContentView(R.layout.dialoglistview);
dialog.setTitle("Print Dialog");
listviewDialog = (ListView) dialog.findViewById(R.id.dialogList);
dataDialogAdapter = new WorkDetailDialogAdapter(context, sampleDetailsArray);
listviewDialog.setAdapter(dataDialogAdapter);
listviewDialog.setClickable(true);
listviewDialog.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
Log.i("selectedAdapter", "selectedAdapter"+ position);
Intent intent = new Intent("com.google.zxing.client.android.ENCODE");
intent.putExtra("ENCODE_DATA", patient.getMrnNumber());
intent.putExtra("ENCODE_FORMAT", "CODE_128");
// intent.putExtra("ENCODE_SHOW_CONTENTS", false);
startActivity(intent);
Toast.makeText(WorkDetailView.this, "Printing Barcode Result", Toast.LENGTH_LONG).show();
}
});
dialog.show();
}
});
Did you try to change the type of onClickListener to Dialog.onClickListener?
android.content.DialogInterface.OnClickListener ocl = new Dialog.OnClickListener() {
#Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
}
};
try this way to build your dialog
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.dialoglistview,
(ViewGroup) findViewById(R.id.root_id_in_dialoglistview_xml_tree));
dialog = AlertDialog.Builder(this).setTitle("Print Dialog").setView(layout);
Place dialog.show() before listviewDialog.setClickable(true)
Button btn=(Button)findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
showdialog();
}});
private void showdialog() {
listDialog = new Dialog(this);
listDialog.setTitle("Select Item");
LayoutInflater li = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = li.inflate(R.layout.list_view, null, false);
listDialog.setContentView(v);
listDialog.setCancelable(true);
final ListView list1 = (ListView) listDialog.findViewById(R.id.listView1);
String array[] = {"1","2","3"};
ArrayAdapter<String> adapt = new ArrayAdapter<String>(this,R.layout.group, array);
list1.setAdapter(adapt);
listDialog.show();
list1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
System.out.println(position);
}
});
}
As you can see, below the bottom list element in my ListView, there is excess space I can't seem to be rid of. I've tried Relative and Linearlayout, both look like this. Here's the code:
public class ChooseDialog extends DialogFragment implements
DialogInterface.OnClickListener {
String URLhome;
String Title;
String type;
/* public static ChooseDialog newInstance() {
ChooseDialog dialog = new ChooseDialog();
Log.v("a", "shit runs");
Bundle bundle = new Bundle();
dialog.setArguments(bundle);
return dialog;
}*/
public ChooseDialog(String type) {
this.type = type;
}
#Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setCancelable(true);
int style = DialogFragment.STYLE_NORMAL, theme = 0;
setStyle(style, theme);
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(type);
builder.setNegativeButton("Cancel", this);
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View dialogLayout = inflater.inflate(R.layout.dialog, null);
builder.setView(dialogLayout);
final String[] items = {"Red", "Green", "Blue" };
builder.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, items),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Log.v("touched: ", items[which].toString());
}}
);
return builder.create();
}
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}
And the code that launches the dialog:
public OnClickListener listener = new OnClickListener() {
public void onClick(View v) {
showNationalityDialog();
}
};
private void showNationalityDialog() {
FragmentManager fm = getSupportFragmentManager();
ChooseDialog nationalityDialog = new ChooseDialog("Nationality");
nationalityDialog.show(fm, "fragment_edit_name");
}
I know this question never drew much attention, but I finally solved the problem.
By using the listview that I created in XML rather than setting the builder's adapter, I managed to get rid of all the excess space.
Here's what the new code looks like:
switch (editText.getId()) {
case (0) :
ListView list = (ListView) dialogLayout.findViewById(R.id.listView1);
list.setAdapter(new ArrayAdapter<String>(activity, R.layout.dialoglist,
activity.getResources().getStringArray(R.array.ageArray)));
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
editText.setText(activity.getResources().getStringArray(R.array.ageArray)[arg2]);
dismiss();
}
});
builder = (Integer.parseInt(android.os.Build.VERSION.SDK) < 11)? new AlertDialog.Builder(activity) :
new AlertDialog.Builder(activity, android.R.style.Theme_Translucent);
builder.setNegativeButton("Cancel", this);
builder.setView(dialogLayout);
return builder.create();
If you are setting a custom view on the alert dialog (via setView()) that ONLY has a ListView then you don't need to use a custom view. The builder will automatically add a ListView into the view if set adapter is called.
The extra space at the end of the list view is probably your custom view with no content.
For example:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final AccountChooserListAdapter adapter = new AccountChooserListAdapter(getActivity(), R.layout.choose_account_list_item,
accountMetadataFactory.getAccountsAsList());
return new AlertDialog.Builder(getActivity())
.setCancelable(true)
.setTitle(getActivity().getString(R.string.title_add_account))
.setAdapter(adapter, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
listener.onAddAccount(which);
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.create();
}