Hi there I am need asistance with my android java code. The user is supposed to
ID: 3553819 • Letter: H
Question
Hi there I am need asistance with my android java code. The user is supposed to enter a surname into the surnameEditText field then click the calculate button to recieve the soundex code in the soundexTextView field. I feel like my approach is correct but I cannot find it to work properly.
package com.example.soundexcalculator;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
private EditText surnameEditText;
private TextView soundexTextView;
String surname;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
surnameEditText = (EditText) findViewById(R.id.surnameEditText);
soundexTextView = (TextView) findViewById(R.id.SoundexResultTextView);
surname = surnameEditText.getEditableText().toString();
Button calculateButton = (Button) findViewById(R.id.calcBtn);
calculateButton.setOnCickListener(startButtonListener);
}
public OnClickListener startButtonListener = new OnClickListener()
{
public static String soundex(String s) {
char[] x = s.toUpperCase().toCharArray();
char firstLetter = x[0];
// convert letters to numeric code
for (int i = 0; i < x.length; i++) {
switch (x[i]) {
case 'B':
case 'F':
case 'P':
case 'V': { x[i] = '1'; break; }
case 'C':
case 'G':
case 'J':
case 'K':
case 'Q':
case 'S':
case 'X':
case 'Z': { x[i] = '2'; break; }
case 'D':
case 'T': { x[i] = '3'; break; }
case 'L': { x[i] = '4'; break; }
case 'M':
case 'N': { x[i] = '5'; break; }
case 'R': { x[i] = '6'; break; }
default: { x[i] = '0'; break; }
}
}
// remove duplicates
String output = "" + firstLetter;
for (int i = 1; i < x.length; i++)
if (x[i] != x[i-1] && x[i] != '0')
output += x[i];
// pad with 0's or truncate
output = output + "0000";
return output.substring(0, 4);
String name1 = surname;
String code1 = soundex(name1);
soundexTextView.setText(code1 + ": " + name1);
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Explanation / Answer
// code at: https://gist.github.com/brijeshb42/9971151
// Try to implement this code: it has fixed issues with onclicklistener
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
private EditText surnameEditText;
private TextView soundexTextView;
static String surname;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
surnameEditText = (EditText) findViewById(R.id.surnameEditText);
soundexTextView = (TextView) findViewById(R.id.SoundexResultTextView);
surname = surnameEditText.getEditableText().toString();
Button calculateButton = (Button) findViewById(R.id.calcBtn);
calculateButton.setOnClickListener(startButtonListener);
}
public OnClickListener startButtonListener = new OnClickListener()
{
public String soundex(String s) {
char[] x = s.toUpperCase().toCharArray();
char firstLetter = x[0];
// convert letters to numeric code
for (int i = 0; i < x.length; i++) {
switch (x[i]) {
case 'B':
case 'F':
case 'P':
case 'V': { x[i] = '1'; break; }
case 'C':
case 'G':
case 'J':
case 'K':
case 'Q':
case 'S':
case 'X':
case 'Z': { x[i] = '2'; break; }
case 'D':
case 'T': { x[i] = '3'; break; }
case 'L': { x[i] = '4'; break; }
case 'M':
case 'N': { x[i] = '5'; break; }
case 'R': { x[i] = '6'; break; }
default: { x[i] = '0'; break; }
}
}
// remove duplicates
String output = "" + firstLetter;
for (int i = 1; i < x.length; i++)
if (x[i] != x[i-1] && x[i] != '0')
output += x[i];
// pad with 0's or truncate
output = output + "0000";
return output.substring(0, 4);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String name1 = surname;
String code1 = soundex(name1);
soundexTextView.setText(code1 + ": " + name1);
};
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.