The operator - under defined for the arguement type(s) EditText, int - eclipse

Im trying to create an activity which adds name, age, and maxhr to sqlite and will be displayed in a listview in Eclipse. name and age is an input text. Maxhr is age(input text) minus 220. But im getting "The operator - under defined for the arguement type(s) EditText, int" error. I can't seem to find the answer for this error. Does anyone know how to fix it?
package com.heartrate.monitoring.activities;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.heartratemonitoringapp.R;
import com.heartrate.monitoring.dao.UserDAO;
import com.heartrate.monitoring.model.User;
public class AddUserActivity extends Activity implements OnClickListener {
public static final String TAG = "AddUserActivity";
private EditText mTxtName;
private EditText mTxtAge;
private Button mBtnAdd;
private UserDAO mUserDao;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_user);
getActionBar().setDisplayHomeAsUpEnabled(true);
initViews();
this.mUserDao = new UserDAO(this);
}
private void initViews() {
this.mTxtName = (EditText) findViewById(R.id.txt_name);
this.mTxtAge = (EditText) findViewById(R.id.txt_age);
this.mBtnAdd = (Button) findViewById(R.id.btn_add);
this.mBtnAdd.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_add:
Editable name = mTxtName.getText();
Editable age = mTxtAge.getText();
if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(age)){
// add the user to database
double mhr;
double maxhr;
mhr = Double.parseDouble(mTxtAge.getText().toString());;
maxhr = mhr - 220;
User createdUser = mUserDao.createUser(
name.toString(), age.toString(),
maxhr.toString());
Log.d(TAG, "added user : "+ createdUser.getName());
Intent intent = new Intent();
intent.putExtra(ListUserActivity.EXTRA_ADDED_USER, createdUser);
setResult(RESULT_OK, intent);
Toast.makeText(this, R.string.user_created_successfully, Toast.LENGTH_LONG).show();
finish();
}
else {
Toast.makeText(this, R.string.empty_fields_message, Toast.LENGTH_LONG).show();
}
break;
default:
break;
}
}
#Override
protected void onDestroy() {
super.onDestroy();
mUserDao.close();
}
}

Remove the extra ; on this line: mhr = Double.parseDouble(mTxtAge.getText().toString());;

Related

Android Studio says "Expression expected"

Android Studio tells there is an error in Java file, in string summonButton2(); Android Studio says "Expression expected".
I want the method summonButton2 to be launched automatically. I undesrtand that I'm doing it wrong. What exactly and is there any other way to start a method, except adding it to onCreate method? Thanks in advance.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
int numberOfLinesLeft = 3;
Button secondaryActivityAddButton;
LinearLayout llForSecondaryButton;
LinearLayout llForSecondaryEditText;
EditText et;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
summonButton2();
}
public void summonButton2(View view){
llForSecondaryButton = findViewById(R.id.secondaryButton);
secondaryActivityAddButton = new Button(this);
secondaryActivityAddButton.setText("" + numberOfLinesLeft);
llForSecondaryButton.addView(secondaryActivityAddButton);
secondaryActivityAddButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
plusTextField();
if(numberOfLinesLeft == 0) {
view.setVisibility(View.GONE);
}
}
});
}
public void plusTextField() {
llForSecondaryEditText = findViewById(R.id.linearLayout1);
// add edittext
et = new EditText(this);
et.setText("text" + numberOfLinesLeft );
llForSecondaryEditText.addView(et);
numberOfLinesLeft--;
secondaryActivityAddButton.setText("" + numberOfLinesLeft);
}
}
summonButton2(new View(this));
Helped in my case.

Error in creating list

I am new to android and i have a Error creating list....... What is the Error? some one help me out........ I am using api18.........
package com.example.personalinformationmanagement;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends ListActivity {
String list[] = {"PersonalTask", "PersonaFileInformation","WeaklyReports", "Remainder"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setListAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1));
}
/* (non-Javadoc)
* #see android.app.ListActivity#onListItemClick(android.widget.ListView, android.view.View, int, long)
*/
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String choice = list[position];
try{
Class ourClass = Class.forName("com.example.personalinformationmanagement." +choice);
Intent i = new Intent(MainActivity.this,ourClass);
startActivity(i);
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
}
First of all ensure that listview id in your activity_main.xml equals to "android:id="#android:id/list".
In code change line
setListAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1,list));
If you want to manipulate with listView use:
ListView myListView = getListview();

Search in ListView android

I'm using the following class to so search in listView of countries
package com.androidhive.androidlistviewwithsearch;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.inputmethod.InputMethodManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class MainActivity extends Activity {
private ListView lv;
SimpleCursorAdapter cursorAdapter;
ArrayAdapter<String> adapter;
EditText inputSearch;
private SQLiteAdapter mySQLiteAdapter;
ArrayList<HashMap<String, String>> productList;
public static final String COUNTRY_NAME = "COUNTRY_NAME";
String[] countryName;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToWrite();
int isEmpty = mySQLiteAdapter.isEmpty();
if (isEmpty == 0) {
mySQLiteAdapter.insert("Afghanistan", "102", "119", "119");
mySQLiteAdapter.insert("Albania", "127", "128", "129");
mySQLiteAdapter.insert("Algeria", "14", "14", "17");
mySQLiteAdapter.insert("American Samoa", "911", "911", "911");
mySQLiteAdapter.insert("Andorra", "118", "118", "110");
mySQLiteAdapter.insert("Angola", "118", "118", "110");
mySQLiteAdapter.insert("Panama", "911", "911", "911");
mySQLiteAdapter.insert("Papua New Guinea /Port Moresby", "", "110", "000");
mySQLiteAdapter.insert("Yemen", "191", "191", "194");
mySQLiteAdapter.insert("Zambia", "991/112", "993/112", "999/112");
mySQLiteAdapter.insert("Zimbabwe", "994/999", "993/999", "995/999");
}
mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToRead();
Cursor cursor = mySQLiteAdapter.queueAll();
startManagingCursor(cursor);
List<String> list = new ArrayList<String>();
if (cursor.moveToFirst()) {
while (cursor.isAfterLast() == false) {
String countries = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.COUNTRIES_CONTENT)).toString().trim();
System.out.println("countries: " + countries);
list.add(countries);
cursor.moveToNext();
}
}
countryName = new String[list.size()];
countryName = list.toArray(countryName);
mySQLiteAdapter.close();
lv = (ListView) findViewById(R.id.list_view);
inputSearch = (EditText) findViewById(R.id.inputSearch);
adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, countryName);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parentView, View childView, int position, long id) {
Intent intent ;
Bundle b = new Bundle();
b.putString(COUNTRY_NAME, countryName[position]);
intent = new Intent(getApplicationContext(), CountryNumbers.class);
intent.putExtras(b);
startActivity(intent);
finish();
}
});
inputSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
MainActivity.this.adapter.getFilter().filter(cs);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
}
}
this code works well and do a search and it also do the setOnItemClickListener that opens the Activity that contains info about the selected country.
But when I do search and I wrote "E" for example, I found the list changed and gets countries that starts with "E" but When I press for example the second country that gets from search, it opens the country with second index in "countryName" array.
How can I solve this issue and get the info of the selected country from the search?
Hope anyone got my mean.
Thanks in advance.
I solved it by replacing this line
b.putString(COUNTRY_NAME, countryName[position]);
by this line
b.putString(COUNTRY_NAME, adapter.getItem(position));

Why isn't the TextView working?

I'm coding an android program that arranges three words in alphabetical order using two simple activities.But in the second(output) activity,either the TextView or the Intents aren't working.The output isn't getting displayed.The java code for first(input) activity is as follows:
`package com.example.names;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Input extends Activity {
EditText t1,t2,t3;
final Context context=this;
String n[]=new String[100];
String t[]=new String[100];
String a1,a2,a3;
Button alpha;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.input);
t1=(EditText)findViewById(R.id.editText1);
t2=(EditText)findViewById(R.id.editText2);
t3=(EditText)findViewById(R.id.editText3);
n[0]=t1.getText().toString();
n[1]=t2.getText().toString();
n[2]=t3.getText().toString();
for(int j=0;j<2;j++)
{
for(int k=j+1;k<3;k++)
{
if(n[j].compareToIgnoreCase(n[k])>0)
{
String temp=n[j];
n[j]=n[k];
n[k]=temp;
}
}
}
a1=n[0];
a2=n[1];
a3=n[2];
alpha=(Button)findViewById(R.id.button1);
alpha.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(Input.this,Output.class);
i.putExtra("x", a1+a2+a3);
Toast.makeText(Input.this, "Method OnClick has been invoked",Toast.LENGTH_SHORT).show();
startActivity(i);
}
});}}`
And the java code for second activity is as follows:
package com.example.names;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class Output extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView p1=new TextView(this);
Bundle extras=getIntent().getExtras();
if (extras!=null){
String ans=extras.getString("x");
p1.setText(ans);
p1.setTextSize(50);
Toast.makeText(Output.this, "All lines have been executed",Toast.LENGTH_LONG).show();
setContentView(p1);
}};}
Why isn't the output getting displayed then???
The application is not crashing also.
Change your code with this below code.
Get edittext value when button is clicked.
Your Input Activity looks like this.
public class Input extends Activity {
EditText t1,t2,t3;
final Context context=this;
String n[]=new String[100];
String t[]=new String[100];
String a1,a2,a3;
Button alpha;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.input);
t1=(EditText)findViewById(R.id.editText1);
t2=(EditText)findViewById(R.id.editText2);
t3=(EditText)findViewById(R.id.editText3);
alpha=(Button)findViewById(R.id.button1);
alpha.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
n[0]=t1.getText().toString();
n[1]=t2.getText().toString();
n[2]=t3.getText().toString();
for(int j=0;j<2;j++)
{
for(int k=j+1;k<3;k++)
{
if(n[j].compareToIgnoreCase(n[k])>0)
{
String temp=n[j];
n[j]=n[k];
n[k]=temp;
}
}
}
a1=n[0];
a2=n[1];
a3=n[2];
Intent i=new Intent(Input.this,Output.class);
i.putExtra("x", a1+a2+a3);
Toast.makeText(Input.this, "Method OnClick has been invoked",Toast.LENGTH_SHORT).show();
startActivity(i);
}
});}}
Your Output Activity is fine it is working no changes in that activity.

How retrieve selected from preferenceActivity

(Android 2.2)
I'm trying to retrieve a value set in a PreferenceActivity. Although the value is correctly stored/recalled by the activity itself, I cannot figure out how to retrieve the set value by myself (to show it in a textView for instance). This code doesn't give an error however the value 0 is shown in textView1 in Spel.java instead of the actual / current value in PrefsManager prefs (in NumberPickerPreferenceActivity).
How to make the correct value show in textView1 there?
Also: onSharedPreferenceChanged (in NumberPickerPreferenceActivity) doesn't show a toast messsage (nothing pops up, nothing is logged). What is wrong, how to solve?
Here's class NumberPickerPreferenceActivity:
package mypackage.tabnavui;
import mypackage.R;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;
public class NumberPickerPreferenceActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {
private static final String TAG = NumberPickerPreferenceActivity.class.getSimpleName();
private SharedPreferences prefs;
public static int aantalBanen;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PreferenceManager preferenceManager = getPreferenceManager();
preferenceManager.setSharedPreferencesMode(MODE_PRIVATE);
preferenceManager.setSharedPreferencesName("numberPicker.preferences");
addPreferencesFromResource(R.xml.preferences);
prefs = PreferenceManager.getDefaultSharedPreferences(this);
}
public int prefsAantalBanen() {
prefs = PreferenceManager.getDefaultSharedPreferences(this);
return prefs.getInt( "AANTAL_BANEN", 0 );
}
public int getAantalBanen() {
aantalBanen = prefsAantalBanen();
return aantalBanen;
}
#Override
protected void onResume() {
super.onResume();
prefs.registerOnSharedPreferenceChangeListener(this);
}
#Override
protected void onPause() {
super.onPause();
prefs.unregisterOnSharedPreferenceChangeListener(this);
}
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
int selectedNumber = prefsAantalBanen();
Log.d(TAG, "Number selected: " + selectedNumber);
Toast t = Toast.makeText(this, "Number selected: " + selectedNumber, Toast.LENGTH_SHORT);
t.show();
}
}
Here's Spel.java where I want to show the value as selected / stored in the ReferenceActivity, see method toonClickHandler:
package mypackage.tabnavui;
import mypackage.tabnavui.R;
import mypackage.Controls2.MyGestureDetector;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.AbsListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class Spel extends Activity implements NumberPickerDialog.OnNumberSetListener {
private static final String TAG = Spel.class.getSimpleName();
private GestureDetector gestureDetector;
private TextView textView1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gestureDetector = new GestureDetector(new MyGestureDetector());
// Use a custom layout file
setContentView(R.layout.spel);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_sample_picker, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.menu_dialog_item) {
NumberPickerDialog dialog = new NumberPickerDialog(this, -1, 2);
dialog.setTitle(getString(R.string.dialog_picker_title));
dialog.setOnNumberSetListener(this);
dialog.show();
return true;
} else if (item.getItemId() == R.id.menu_preferences_item) {
startActivity(new Intent(this, NumberPickerPreferenceActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
public void toonClickHandler(View v) {
textView1 = (TextView) this.findViewById(R.id.textView1);
int aantalBanen = NumberPickerPreferenceActivity.aantalBanen;
textView1.setText(String.valueOf(aantalBanen));
}
public void onNumberSet(int number) {
Log.d(TAG, "Number selected: " + number);
Toast t = Toast.makeText(this, "Number selected: " + number, Toast.LENGTH_SHORT);
t.show();
}
#Override
public boolean onTouchEvent(MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
}
class MyGestureDetector extends SimpleOnGestureListener {
private static final int SWIPE_MAX_OFF_PATH = 200;
private static final int SWIPE_MIN_DISTANCE = 50;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
#Override
public boolean onDown (MotionEvent e) {
return true;
}
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
// left to right swipe and right to left swipe
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
// left swipe
Toast t = Toast.makeText(Spel.this, "Left swipe", Toast.LENGTH_LONG);
t.show();
// startActivity(Tabs3.tab3);
startActivityForResult(Tabs3.tab3, 500);
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
return true;
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
// right swipe
Toast t = Toast.makeText(Spel.this, "Right swipe", Toast.LENGTH_LONG);
t.show();
// startActivity(Tabs3.tab1);
startActivityForResult(Tabs3.tab1, 500);
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
return true;
}
return false;
}
}
}
and preferences.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:picker="http://schemas.android.com/apk/res/mypackage.tabnavui"
xmlns:android="http://schemas.android.com/apk/res/android">
<nl.computerhuys.tabnavui.NumberPickerPreference
android:key="AANTAL_BANEN"
android:title="Aantal banen"
android:summary="Number picker as a preference"
picker:defaultValue="2"
picker:startRange="0"
picker:endRange="12" />
</PreferenceScreen>
Found it here: How do I get the SharedPreferences from a PreferenceActivity in Android?
in NumberPickerReferenceActivity: replace
preferenceManager.setSharedPreferencesMode(MODE_PRIVATE);
by
preferenceManager.setSharedPreferencesMode(MODE_WORLD_READABLE);
and replace
prefs = PreferenceManager.getDefaultSharedPreferences(this);
by
prefs = getSharedPreferences("numberPicker.preferences", MODE_WORLD_READABLE);
to make the toast and log work
and in Spel:
int aantalBanen = getSharedPreferences("numberPicker.preferences", MODE_WORLD_READABLE).getInt( "AANTAL_BANEN", 0 );
textView1.setText(String.valueOf(aantalBanen));
to show stored value in textView1