I am new to android studio.
I am making an app that lists names of teachers and the subjects thought and then every person has a different class. The teacher names and subjects though are stored in a TreeMap.
I am having difficulty as I want my app to feature sorts, so when outputted the positions of teachers are changed according to the sort. At the moment the items are sorted via hard code alphabetically and a switch case is used and the onclick integer position is obtained. However, I want to make the switch case make a decision according to the string of the itemClicked so whenever a sort is executed when the user clicks the correct class is displayed. Kindly see my code below:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView resultsListView = (ListView) findViewById(R.id.results_listview);
TreeMap<String, String> nameSubject = new TreeMap<>();
nameSubject.put("Richard Bonnici", "Maths"); //1
nameSubject.put("Natasha Mifsud", "English"); //2
nameSubject.put("Victor Valdes", "Computing"); //3
nameSubject.put("Jonathan Galea", "Computing"); //4
nameSubject.put("Jerome Micallef", "History"); //5
nameSubject.put("Tarah Galea", "Marketing"); //6
nameSubject.put("Micheal Gonzalez", "Maths"); //7
nameSubject.put("Jason Bartolo", "Chemistry"); //8
nameSubject.put("Jack Grech", "Chemistry"); //9
nameSubject.put("Anna Carbonaro", "Maltese"); //10
nameSubject.put("Kay Azzopardi", "Biology"); //11
nameSubject.put("George Micallef", "Physics"); //12
nameSubject.put("Gabriella Bonnici", "Maths"); //13
Map sortedMap = valueSort(nameSubject);
// Get a set of the entries on the sorted map
Set set = sortedMap.entrySet();
// Get an iterator
Iterator i = set.iterator();
List<HashMap<String, String>> listItems = new ArrayList<>();
SimpleAdapter adapter = new SimpleAdapter(this, listItems, R.layout.list_item,
new String[]{"First Line", "Second Line"},
new int[]{R.id.text1, R.id.text2});
Iterator it = nameSubject.entrySet().iterator();
while (it.hasNext()) {
HashMap<String, String> resultsMap = new HashMap<>();
Map.Entry pair = (Map.Entry) it.next();
resultsMap.put("First Line", pair.getKey().toString());
resultsMap.put("Second Line", pair.getValue().toString());
listItems.add(resultsMap);
}
resultsListView.setAdapter(adapter);
resultsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(android.widget.AdapterView<?> adapterView, View view, int i, long l) {
//
switch (i) {
case 0:
startActivity(new Intent(TeacherList.this, AnnaCarbonaro.class));
break;
case 1:
startActivity(new Intent(TeacherList.this, GabriellaBonnici.class));
break;
case 2:
startActivity(new Intent(TeacherList.this, GeorgeMicallef.class));
break;
case 3:
startActivity(new Intent(TeacherList.this, JackGrech.class));
break;
case 4:
startActivity(new Intent(TeacherList.this, JasonBartolo.class));
break;
case 5:
startActivity(new Intent(TeacherList.this, JeromeMicallef.class));
break;
case 6:
startActivity(new Intent(TeacherList.this, JonathanGalea.class));
break;
case 7:
startActivity(new Intent(TeacherList.this, KayAzzopardi.class));
break;
case 8:
startActivity(new Intent(TeacherList.this, MichealGonzalez.class));
break;
case 9:
startActivity(new Intent(TeacherList.this, NatashaMifsud.class));
break;
case 10:
startActivity(new Intent(TeacherList.this, RichardBonnici.class));
break;
case 11:
startActivity(new Intent(TeacherList.this, TarahGalea.class));
break;
case 12:
startActivity(new Intent(TeacherList.this, VictorValdes.class));
break;
}
}
});
}
}
i have had this problem for the last two to three days....i am trying to set specific intents to each item clicked in the list view but the issue is that iam using the position parameter to get the position but is redirecting me according to the current filtered position yet i want the original position on the list view... i tried to use the getItem method within the switch case but but am confused which values i have to pass in the case: to match the position.any help is highly appreciated
below is the sample of the codes
customList
public class CustomList extends BaseAdapter implements Filterable {
Context context;
ArrayList<Countries> countrylist;
ArrayList<Countries> stringFilterList;
ValueFilter valueFilter;
CustomList(Context context, ArrayList<Countries> countrylist){
this.context = context;
this.countrylist = countrylist;
stringFilterList = countrylist;
}
#Override
public int getCount() {
return countrylist.size();
}
#Override
public Object getItem(int position) {
return countrylist.get(position);
}
#Override
public long getItemId(int position) {
return countrylist.indexOf(getItem(position));
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = null;
if(convertView == null){
convertView = inflater.inflate(R.layout.menu_eng_content, null);
TextView countryName = (TextView) convertView.findViewById(R.id.country_names);
ImageView countryImages = (ImageView) convertView.findViewById(R.id.country_imgs);
Countries countries = countrylist.get(position);
countryName.setText(countries.getNames());
countryImages.setImageResource(countries.getFlags());
}
return convertView;
}
#Override
public Filter getFilter() {
if(valueFilter == null){
valueFilter = new ValueFilter();
}
return valueFilter;
}
private class ValueFilter extends Filter{
#Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint != null && constraint.length()>0){
ArrayList<Countries> filterList = new ArrayList<Countries>();
for (int i = 0; i<stringFilterList.size(); i++){
if ((stringFilterList.get(i).getNames().toUpperCase()).contains(constraint.toString()
.toUpperCase())){
Countries countries = new Countries(stringFilterList.get(i)
.getNames(), stringFilterList.get(i).getFlags());
filterList.add(countries);
}
}
results.count = filterList.size();
results.values =filterList;
}
else {
results.count = stringFilterList.size();
results.values = stringFilterList;
}
return results;
}
#Override
protected void publishResults(CharSequence constraint, FilterResults results) {
countrylist = (ArrayList<Countries>)results.values;
notifyDataSetChanged();
}
}
FrenchMenu class
public class FrenchMenu extends ActionBarActivity implements SearchView.OnQueryTextListener{
ListView listView;
private SearchView searchView;
ArrayList<Countries> countrylist;
CustomList adapter;
String[] countryNames = {"Algerie", "Botswana", "Burkina Faso", "Burundi", "Cameroon", "Cape Verde",
"Congo Brazavile", "Côte d'Ivoire", "Djibouti", "RDC", "Egypte", "Eritrea", "Ethiopie", "Gabon","Ghana","Guinea Bissau"
, "Guinea Conakry", "Kenya", "Lesotho", "Madagascar", "Malawi", "Mali", "Mauritania", "Maroque", "Namibie",
"Niger", "Nigeria", "Rwanda", "Senegal", "Seychelles", "Afrique du Sud", "Sudan", "Tanzanie", "Tchad", "Togo"
, "Tunisie", "Uganda", "Zambie", "Zimbabwe"};
Integer imgs[] = {R.drawable.algeria_flag,
R.drawable.botswana,
R.drawable.burkina_faso,
R.drawable.burundi_flag,
R.drawable.cameroon_flag,
R.drawable.cape_verde,
R.drawable.congo_braza,
R.drawable.cote_divoire,
R.drawable.djibouti,
R.drawable.drc_flag,
R.drawable.egypt,
R.drawable.eritrea_flag,
R.drawable.ethiopia,
R.drawable.gabon_flag,
R.drawable.ghana_flag,
R.drawable.guinea_bissau,
R.drawable.guinea_conakry,
R.drawable.kenya,
R.drawable.lesotho,
R.drawable.madagascar,
R.drawable.malawi, R.drawable.mali, R.drawable.mauritania, R.drawable.morocco,
R.drawable.namibia,R.drawable.niger,R.drawable.nigeria,R.drawable.rwandan_flag,
R.drawable.senegal_flag,R.drawable.seychelles,R.drawable.south_african_flag,
R.drawable.sudan,R.drawable.tanzania,R.drawable.tchad,R.drawable.togo,
R.drawable.tunisia,R.drawable.uganda_flag,R.drawable.zambia,R.drawable.zimbabwe_flag
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_eng_countries);
listView = (ListView) findViewById(R.id.listView_eng);
searchView = (SearchView) findViewById(R.id.search_view);
/*
CustomList adapter = new CustomList(FrenchMenu.this, names, imgs);
listView = (ListView) findViewById(R.id.listView_eng);
listView.setAdapter(adapter);*/
countrylist = new ArrayList<Countries>();
for (int i= 0;i<countryNames.length;i++){
Countries countries = new Countries(countryNames[i], imgs[i]);
countrylist.add(countries);
}
adapter = new CustomList(getApplicationContext(),countrylist);
listView.setAdapter(adapter);
listView.setTextFilterEnabled(true);
searchView.setOnQueryTextListener(this);
//when the items in the list view are clicked
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//String itemPosition = adapter.getItem(position).toString();
switch (position){
case 0:
Intent algeria = new Intent(FrenchMenu.this, WebDirection.class);
algeria.putExtra("url","http://fr.napteker.com/products/article/algeria");
algeria.putExtra("title", "Medicament en Algerie");
startActivity(algeria);
break;
case 1:
Intent botswana = new Intent(FrenchMenu.this, WebDirection.class);
botswana.putExtra("url", "http://fr.napteker.com/products/article/botswana");
botswana.putExtra("title", "Medicament au Botswana");
startActivity(botswana);
break;
case 2:
Intent burkina_faso = new Intent(FrenchMenu.this, WebDirection.class);
burkina_faso.putExtra("url","http://fr.napteker.com/products/article/burkina-faso");
burkina_faso.putExtra("title","Medicament au Burkina Faso");
startActivity(burkina_faso);
break;
case 3:
Intent burundi = new Intent(FrenchMenu.this, WebDirection.class);
burundi.putExtra("url","http://fr.napteker.com/products/article/burundi");
burundi.putExtra("title","Medicament au Burundi");
startActivity(burundi);
break;
case 4:
Intent cameroon = new Intent(FrenchMenu.this, WebDirection.class);
cameroon.putExtra("url", "http://fr.napteker.com/products/article/cameroon");
cameroon.putExtra("title", "Medicament en Cameroun");
startActivity(cameroon);
break;
case 5:
Intent cape_verde = new Intent(FrenchMenu.this, WebDirection.class);
cape_verde.putExtra("url", "http://fr.napteker.com/products/article/cape-verde");
cape_verde.putExtra("title", "Medicament au Cape Vert");
startActivity(cape_verde);
break;
case 6:
Intent congo_braza = new Intent(FrenchMenu.this, WebDirection.class);
congo_braza.putExtra("url", "http://fr.napteker.com/products/article/congo-braza");
congo_braza.putExtra("title","Medicament au Congo ");
startActivity(congo_braza);
break;
case 7:
Intent ivory_coast = new Intent(FrenchMenu.this, WebDirection.class);
ivory_coast.putExtra("url", "http://fr.napteker.com/products/article/ivory-coast");
ivory_coast.putExtra("title", "Medicament au Côte d'Ivoire");
startActivity(ivory_coast);
break;
case 8:
Intent djibouti = new Intent(FrenchMenu.this, WebDirection.class);
djibouti.putExtra("url", "http://fr.napteker.com/products/article/djibouti");
djibouti.putExtra("title", "Medicament au Djibouti");
startActivity(djibouti);
break;
case 9:
Intent drc = new Intent(FrenchMenu.this, WebDirection.class);
drc.putExtra("url", "http://fr.napteker.com/products/article/drc");
drc.putExtra("title", "Medicament au RDC");
startActivity(drc);
break;
case 10:
Intent egypt = new Intent(FrenchMenu.this, WebDirection.class);
egypt.putExtra("url", "http://fr.napteker.com/products/article/egypt");
egypt.putExtra("title", "Medicament en Egypte");
startActivity(egypt);
break;
case 11:
Intent eritrea = new Intent(FrenchMenu.this, WebDirection.class);
eritrea.putExtra("url", "http://fr.napteker.com/products/article/eritrea");
eritrea.putExtra("title", "Medicament en Eritrea");
startActivity(eritrea);
break;
case 12:
Intent ethiopia = new Intent(FrenchMenu.this, WebDirection.class);
ethiopia.putExtra("url", "http://fr.napteker.com/products/article/burkina-faso");
ethiopia.putExtra("title", "Medicament en Ethiopie");
startActivity(ethiopia);
break;
case 13:
Intent gabon = new Intent(FrenchMenu.this, WebDirection.class);
gabon.putExtra("url", "http://fr.napteker.com/products/article/gabon");
gabon.putExtra("title", "Medicament au Gabon");
startActivity(gabon);
break;
case 14:
Intent ghana = new Intent(FrenchMenu.this, WebDirection.class);
ghana.putExtra("url", "http://fr.napteker.com/products/article/ghana");
ghana.putExtra("title", "Medicament au Ghana");
startActivity(ghana);
break;
case 15:
Intent guinea_bissau = new Intent(FrenchMenu.this, WebDirection.class);
guinea_bissau.putExtra("url", "http://fr.napteker.com/products/article/guinea-bissau");
guinea_bissau.putExtra("title", "Medicament au Guinea Bissau");
startActivity(guinea_bissau);
break;
case 16:
Intent guinea_conakry = new Intent(FrenchMenu.this, WebDirection.class);
guinea_conakry.putExtra("url", "http://fr.napteker.com/products/article/guinea-conakry");
guinea_conakry.putExtra("title", "Medicament au Guinea Conakry");
startActivity(guinea_conakry);
break;
case 17:
Intent kenya = new Intent(FrenchMenu.this, WebDirection.class);
kenya.putExtra("url", "http://fr.napteker.com/products/article/kenya");
kenya.putExtra("title", "Drugs in Kenya");
startActivity(kenya);
break;
case 18:
Intent lesotho = new Intent(FrenchMenu.this, WebDirection.class);
lesotho.putExtra("url", "http://fr.napteker.com/products/article/lesotho");
lesotho.putExtra("title", "Medicament au Lesotho");
startActivity(lesotho);
break;
case 19:
Intent madagascar = new Intent(FrenchMenu.this, WebDirection.class);
madagascar.putExtra("url", "http://fr.napteker.com/products/article/madagascar");
madagascar.putExtra("title", "Medicament au Madagascar");
startActivity(madagascar);
break;
case 20:
Intent malawi = new Intent(FrenchMenu.this, WebDirection.class);
malawi.putExtra("url", "http://fr.napteker.com/products/article/malawi");
malawi.putExtra("title", "Medicament au Malawi");
startActivity(malawi);
break;
case 21:
Intent mali = new Intent(FrenchMenu.this, WebDirection.class);
mali.putExtra("url", "http://fr.napteker.com/products/article/mali");
mali.putExtra("title", "Medicament au Mali");
startActivity(mali);
break;
case 22:
Intent mauritania = new Intent(FrenchMenu.this, WebDirection.class);
mauritania.putExtra("url", "http://fr.napteker.com/products/article/mauritania");
mauritania.putExtra("title", "Medicament au Mauritanie");
startActivity(mauritania);
break;
case 23:
Intent morocco = new Intent(FrenchMenu.this, WebDirection.class);
morocco.putExtra("url", "http://fr.napteker.com/products/article/morocco");
morocco.putExtra("title", "Medicament au Maroc");
startActivity(morocco);
break;
case 24:
Intent namibia = new Intent(FrenchMenu.this, WebDirection.class);
namibia.putExtra("url", "http://fr.napteker.com/products/article/namibia");
namibia.putExtra("title", "Drugs in Namibia");
startActivity(namibia);
break;
case 25:
Intent niger = new Intent(FrenchMenu.this, WebDirection.class);
niger.putExtra("url", "http://fr.napteker.com/products/article/niger");
niger.putExtra("title", "Medicament au Niger");
startActivity(niger);
break;
case 26:
Intent nigeria = new Intent(FrenchMenu.this, WebDirection.class);
nigeria.putExtra("url", "http://fr.napteker.com/products/article/nigeria");
nigeria.putExtra("title", "Medicament au Nigeria");
startActivity(nigeria);
break;
case 27:
Intent rwanda = new Intent(FrenchMenu.this, WebDirection.class);
rwanda.putExtra("url", "http://fr.napteker.com/products/article/rwanda");
rwanda.putExtra("title", "Medicament au Rwanda");
startActivity(rwanda);
break;
case 28:
Intent senegal = new Intent(FrenchMenu.this, WebDirection.class);
senegal.putExtra("url", "http://fr.napteker.com/products/article/senegal");
senegal.putExtra("title", "Medicament au Senegal");
startActivity(senegal);
break;
case 29:
Intent seychelles = new Intent(FrenchMenu.this, WebDirection.class);
seychelles.putExtra("url", "http://fr.napteker.com/products/article/seychelles");
seychelles.putExtra("title", "Medicament au Seychelles");
startActivity(seychelles);
break;
case 30:
Intent south_africa = new Intent(FrenchMenu.this, WebDirection.class);
south_africa.putExtra("url", "http://fr.napteker.com/products/article/south-africa");
south_africa.putExtra("title", "Medicament en Afrique du Sud");
startActivity(south_africa);
break;
case 31:
Intent sudan = new Intent(FrenchMenu.this, WebDirection.class);
sudan.putExtra("url", "http://fr.napteker.com/products/article/sudan");
sudan.putExtra("title", "Medicament au Sudan");
startActivity(sudan);
break;
case 32:
Intent tanzania = new Intent(FrenchMenu.this, WebDirection.class);
tanzania.putExtra("url", "http://fr.napteker.com/products/article/tanzania");
tanzania.putExtra("title", "Medicament au Tanzanie");
startActivity(tanzania);
break;
case 33:
Intent tchad = new Intent(FrenchMenu.this, WebDirection.class);
tchad.putExtra("url", "http://fr.napteker.com/products/article/Tchad");
tchad.putExtra("title", "Medicament au Tchad");
startActivity(tchad);
break;
case 34:
Intent togo = new Intent(FrenchMenu.this, WebDirection.class);
togo.putExtra("url", "http://fr.napteker.com/products/article/togo");
togo.putExtra("title", "Medicament au Togo");
startActivity(togo);
break;
case 35:
Intent tunisia = new Intent(FrenchMenu.this, WebDirection.class);
tunisia.putExtra("url", "http://fr.napteker.com/products/article/tunisia");
tunisia.putExtra("title", "Drugs in Tunisia");
startActivity(tunisia);
break;
case 36:
Intent uganda = new Intent(FrenchMenu.this, WebDirection.class);
uganda.putExtra("url", "http://fr.napteker.com/products/article/uganda");
uganda.putExtra("title", "Medicament en Uganda");
startActivity(uganda);
break;
case 37:
Intent zambia = new Intent(FrenchMenu.this, WebDirection.class);
zambia.putExtra("url", "http://fr.napteker.com/products/article/burkina-faso");
zambia.putExtra("title", "Medicament au Zambie");
startActivity(zambia);
break;
case 38:
Intent zimbabwe = new Intent(FrenchMenu.this, WebDirection.class);
zimbabwe.putExtra("url", "http://fr.napteker.com/products/article/zimbabwe");
zimbabwe.putExtra("title", "Medicament au Zimbabwe");
startActivity(zimbabwe);
break;
}
}
});
//instantiating the action bar
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#436487")));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main,menu);
return true;
}
public boolean onQueryTextChange(String newText){
adapter.getFilter().filter(newText);
return false;
}
public boolean onQueryTextSubmit(String query){
return false;
}
}
If you have access the countries class, try adding the link & title to the country itself. It will allow the listview to just retreive the country and get the link & title from the object.
Like so:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Countries country= (Countries) adapter.getItemAtPosition(position);
Intent country_intent = new Intent(FrenchMenu.this, WebDirection.class);
country_intent ("url", country.getURL());
country_intent .putExtra("title", country.getTitle());
startActivity(country_intent);
}
}
});
Hope this helps
Edit:
So, in Countries : Create 2 variables for the link and title (String link, title). Edit the constructor and create getters and setters for those variables.
In the FrenchMenu class:
Add an array String[] links = { "The","bunch","Of","Links" }
And an array String[] titles = { "the","Same","here"}
Change your loop
for (int i= 0;i<countryNames.length;i++){
Countries countries = new Countries(countryNames[i], imgs[i]);
countrylist.add(countries);
}
To
for (int i= 0;i<countryNames.length;i++){
Countries countries = new Countries(countryNames[i], imgs[i],links[i],titles[i]);
countrylist.add(countries);
}
Then you can use the function in your onItemClickListener as I showed you above.
When I delete text inside EditText with clrFunc() I lost cursor (I see it flashing, but nothing happens if I type). I can't type back in. I have to click back onEditText and then I can type in.
/* WORKS PERFECT */
private void delFunc(){
String str = display.getText().toString();
if(str.length() > 0){
String strStart = str.substring(0, SELECTOR_POSITION-1);
String strEnd = str.substring(SELECTOR_POSITION);
display.setText(strStart + strEnd);
display.requestFocus();
display.setSelection(--SELECTOR_POSITION);
}
}
/* NOT FULLY WORKING */
private void clrFunc(){
display.setText(""); //text is set to ""
display.requestFocus(); //not working
display.setSelection(display.getText().length()); //not working
}
EDIT: Added more code, I'm building simple calculator.
private EditText display;
private Button b0, b1, b2, b3, b4, b5, b6, b7, b8, b9;
private Button bDec, bEquals, bAdd, bSub, bMultiply, bDivide, bClear, bBracket, bBackBracket, bDel, bClrH;
private Button bSin, bAsin, bCos, bAcos, bTan, bAtan, bLn, bLog, bPow, bPow2, bSqrt, bPi, bE, bToRad, bToDeg;
private TextView history;
private int SELECTOR_POSITION;
private void implementGUI(){
/* EditText */
display = (EditText) findViewById(R.id.etDisplay);
display.setOnTouchListener(this);
/* ... */
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.bZero: insert("0"); break;
case R.id.bOne: insert("1"); break;
case R.id.bTwo: insert("2"); break;
case R.id.bThree: insert("3"); break;
case R.id.bFour: insert("4"); break;
case R.id.bFive: insert("5"); break;
case R.id.bSix: insert("6"); break;
case R.id.bSeven: insert("7"); break;
case R.id.bEight: insert("8"); break;
case R.id.bNine: insert("9"); break;
case R.id.bDecPoint: insert("."); break;
case R.id.bAdd: insert("+"); break;
case R.id.bSub: insert("-"); break;
case R.id.bMultiply: insert("*"); break;
case R.id.bDivide: insert("/"); break;
case R.id.bBracket: insert("("); break;
case R.id.bBackBracket: insert(")"); break;
case R.id.bDel: delFunc(); break;
case R.id.bC: clrFunc(); break;
case R.id.bEquals: calcFunc(); break;
case R.id.bClrH: clrHistory(); break;
case R.id.bSin: insert("sin("); break;
case R.id.bAsin: insert("asin("); break;
case R.id.bCos: insert("cos("); break;
case R.id.bAcos: insert("acos("); break;
case R.id.bTan: insert("tan("); break;
case R.id.bAtan: insert("atan("); break;
case R.id.bLn: insert("ln("); break;
case R.id.bLog: insert("log("); break;
case R.id.bPow: insert("^"); break;
case R.id.bPow2: insert("^2"); break;
case R.id.bSqrt: insert("sqrt("); break;
case R.id.bPi: insert("(PI)"); break;
case R.id.bE: insert("(E)"); break;
case R.id.bToRad: insert("toRadians("); break;
case R.id.bToDeg: insert("toDegrees("); break;
}
}
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
v.onTouchEvent(event);
InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
SELECTOR_POSITION = display.getSelectionStart();
return true;
}
private void delFunc(){
String str = display.getText().toString();
if(str.length() > 0){
String strStart = str.substring(0, SELECTOR_POSITION-1);
String strEnd = str.substring(SELECTOR_POSITION);
display.setText(strStart + strEnd);
display.requestFocus();
display.setSelection(--SELECTOR_POSITION);
}
}
/* NOT FULLY WORKING */
private void clrFunc(){
display.setText("");
display.requestFocus();
display.setSelection(display.getText().length());
}
private void clrHistory(){
history.setText("");
}
private void calcFunc(){
try{
MathEval math = new MathEval();
String input = display.getText().toString();
history.setText(String.format("%s = %s%n%s", input, math.evaluate(input), history.getText()));
}catch(Exception e){
e.printStackTrace();
history.setText(String.format("%s%n%s", "ERROR", history.getText()));
}
clrFunc();
}
private void insert(String midStr){
try{
String input = display.getText().toString();
String startStr = input.substring(0,SELECTOR_POSITION);
String endStr = input.substring(SELECTOR_POSITION);
String retStr = startStr + midStr + endStr;
SELECTOR_POSITION += midStr.length();
display.setText(retStr);
display.setSelection(SELECTOR_POSITION);
}catch(Exception e){
e.printStackTrace();
}
}
P.S.: Sorry for my bad language.
If you want to directly type into the EditText you are missing the following in your method:
display.requestFocus();
EDIT
This is the method I am using to show the keyboard with focus, the different focus call could do the trick.
protected void showKeyboard(EditText edit) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(edit, 0);
edit.requestFocusFromTouch();
}