How to enable and disable action on switch preference in android - switchpreference

This is not working with switch preference please help .......to write another code..
SwitchPreference uiTestModePref = (SwitchPreference) findPreference("switch");
uiTestModePref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object o) {
boolean check = (preference).isEnabled();
if (check) {
android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.HAPTIC_FEEDBACK_ENABLED, 1);
Toast.makeText(getApplicationContext(), "Yes", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(getApplicationContext(), "No", toast.LENGTH_LONG).show();
}
return false;
}
});

It should be isChecked() instead of isEnabled()

Related

Flutter : How check if List contain passes item?

I have method when icon on pressed ,
onPressed: () {
check(snapshot.data[index]);
},
and method to check if the data found on another list ,I did as below :
void add_cart(item){
for (var x in list){
if (x.id==item.id){
cartCount = 1;
}
else {
cartCount = 0;
}
}
if (cartCount == 1){
list.remove(item);
}
else{
list.add(item);
}
print(cartCount);
notifyListeners();
}
the cartcount work correct but remove method don't work , what is the true way for that ?
you need to override == and hashcode of your cart model like this
class CartModel {
String id;
// other properties
#override
bool operator ==(Object other) => other is CartModel && runtimeType == other.runtimeType && id == other.id;
#override
int get hashCode => id.hashCode;
}
after that list.remove(item); will work as intended

Updating Flutter page after setState()

I have a user follow/unfollow page that searches Firebase for a list of followers and returns a Boolean if their usedID is found...
Future<Null> _friends() async {
fb.child("users/${userid}/Following/${id}").onValue.listen((Event event) {
setState((){
if (event.snapshot.value != null) {
friends == true;
} else if (event.snapshot.value == null) {
friends == false;
}
});
});
}
The bool is then set in a separate function that changes a public bool...
bool friends;
The trouble is that my Widget/Scaffold doesn't update here...
friends == true ? new RaisedButton(onPressed: unfollow()) :
new RaisedButton(onPressed: follow());
Any ideas? I'll update with exact code once later.
the issue is that you've used an == (equality) comparison operator instead of = (assignment). Change your code to the following:
Future<Null> _friends() async {
fb.child("users/${userid}/Following/${id}").onValue.listen((Event event) {
setState((){
// this will return true or false which is what you want based on your code :)
friends = event.snapshot.value == null;
});
});
}

Programe Not Executing in Correct Order in Android Studio

I want to check whether the email id entered by user is unique or not so for that initially I have my variable Boolean valid = false;. On clicking a button i am taking the email id entered and checking it for valid email id expression using regular expression and then i am using an asyntask to check its uniqueness. Code in my onclicklistner is
if (emailid.matches(regexp) && emailid.length() > 0) {
new Validate().execute();
Toast.makeText(getApplicationContext(), valid.toString(), Toast.LENGTH_LONG).show();
if (valid) {
data.putString("eid", eid);
data.putString("firstname", firstname);
data.putString("lastname", lastname);
data.putString("emailid", emailid);
Intent i = new Intent(getApplicationContext(), GamesFragment.class);
startActivity(i);
} else {
Toast.makeText(getApplicationContext(), "Email Address Already Exist", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(getApplicationContext(), "Check Your Email Address", Toast.LENGTH_LONG).show();
}
Here what problem i am facing is, for first time when i am entering an email which is unique and clicks the button, the Validate() asynctask checks and sets the valid variable to true, but it doesn't goes to next activity GamesFragment because i have declared valid = false initially. Now when i again click the button, then it goes to next activity as the valid variable is set to true because of previous click.
Now My Validate() asynctask is
private class Validate extends AsyncTask<Void, Void, Void> {
#Override
protected Boolean doInBackground(Void... params) {
ArrayList<NameValuePair> emailId = new ArrayList<NameValuePair>();
emailId.add(new BasicNameValuePair("email", emailid));
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("url/validate.php");
httppost.setEntity(new UrlEncodedFormEntity(emailId));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
iss = entity.getContent();
} catch(Exception e) {
Log.e("pass 1", "Connection Error");
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader
(new InputStreamReader(iss,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null)
sb.append(line + "\n");
iss.close();
result = sb.toString();
} catch(Exception e) {
e.printStackTrace();
}
try {
JSONObject json_data = new JSONObject(result);
code=(json_data.getInt("code"));
if(code == 1)
valid = true;
else
valid = false;
Log.e("pass 3", "valid "+valid);
} catch(Exception e) {
e.printStackTrace();
}
return null;
}
}
Please help i am not getting why this is happening.
Create function to check validation.
private boolean function validate(String emailid){
if (emailid.matches(regexp) && emailid.length() > 0) {
return true;
}
return false;
}
use that function to decide event
if(validate(emailid)){ // if function return true then email is valid and good to go.
new Validate().execute();
}
For second condition you have to check it in your async task onPostExecute() that is Validate();
#Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
if(code == 1){
// check if response is valid than
Intent i = new Intent(getApplicationContext(), GamesFragment.class);
startActivity(i);
}
}

WindowsPhoneDataBoundApplication with web service

I want to merge between my code that contains the Web service and the code of WindowsPhoneDataBound Application.Is it possible? and how?
My Code:
private void button1_Click(object sender, RoutedEventArgs e)
{
Uri url = new Uri("http://www.google.com/ig/api?weather=" + textBoxVille.Text, UriKind.Absolute);
WebClient client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
client.DownloadStringAsync(url);
}
void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
StringReader stream = new StringReader(e.Result);
XmlReader reader = XmlReader.Create(stream);
reader.MoveToContent();
while (reader.Read())
{
switch (reader.Name)
{
case ("day_of_week"):
{
listBox1.Items.Add(new ListBoxItem()
{
Content = reader.GetAttribute("data")
});
} break;
case ("low"):
{
listBox1.Items.Add(new ListBoxItem()
{
Content = reader.GetAttribute("data")
});
} break;
case ("high"):
{
listBox1.Items.Add(new ListBoxItem()
{
Content = reader.GetAttribute("data")
});
} break;
case ("icon"):
{
listBox1.Items.Add(new ListBoxItem()
{
Content = reader.GetAttribute("data")
});
} break;
case ("condition"):
{
listBox1.Items.Add(new ListBoxItem()
{
Content = reader.GetAttribute("data")
});
} break;
case ("weather"):
break;
}
}
reader.Close();
}
}
Of course it is possible with the help of data binding and observable collections. Instead of adding new list box items every time you get a new element, you can use an ObservableCollection. Set the data source of the listbox to the observable collection.
The benefit of using ObservableCollection is that whenever you add or remove an element from your collection, your listbox will automatically reflect that. So no need to manually add or remove listboxItems. If your listboxItem has custom layout, then you can create a dataTemplate and bind data to the elements which needs to be displayed in the listboxItem.

gwt CellList edit cell

CellList<Device> cellList = new CellList<Device>(new ItemCell());
where:
static class ItemCell extends AbstractCell<Device> {
#Override
public void render(Context context, Device device, SafeHtmlBuilder builder) {
if(device == null) {
return;
}
builder.appendHtmlConstant("<div>device.getId()</div>");
builder.appendHtmlConstant("<div>device.getName()</div>");
}
}
And now, I want to make an 'Edit' button, when I'll press on it - I want to see editable selected item. How can I do it? Please answer, who knows.
Use EditTextCell or ActionCell
addColumn(new EditTextCell(), "Name", new GetValue() {
#Override
public String getValue(IData contact) {
return contact.getName();
}
}, new FieldUpdater() {
#Override
public void update(int index, IData object, String value) {
try {
pandingChanges.add(new FirstNameChange(object, value));
} catch (Exception e) {
}
}
});
check this out or >THIS>
I followed the simple old way of doing this.
I used a cell list and a form attached to it. Every time you click on a row, row's data is loaded to the form. You can delete or edit the selected row via this form now.