I'm pretty new to this, and I have a question that is probably pretty simple.
I have 3 spinners, and I want to populate each spinner from an array based on the choice the user made on the previous spinners.
Right now i have it set up to display a toast with the selected data, but I want to set up an activity to open up, but that's later. Right now I need to know how to populate the spinner from an array from the strings.xml.
.Java
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
/**
* #author madonk
*
*/
public class Region extends Activity {
Spinner sp1,sp2,sp3;
ArrayAdapter<String> reg_adp,sw_city_adp,sw_lake_charles_adp;
List<String> regions,sw_cities,sw_lake_charles;
int pos;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_region);
regions=new ArrayList<String>();
regions.add("Select a Region");
regions.add("Southwest");
sp1= (Spinner) findViewById(R.id.regions_spinner);
sp2= (Spinner) findViewById(R.id.sw_city_spinner);
sp3= (Spinner) findViewById(R.id.sw_lake_charles_spinner);
reg_adp=new ArrayAdapter<String> (this,android.R.layout.simple_dropdown_item_1line,regions);
reg_adp.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
sp1.setAdapter(reg_adp);
sp1.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
pos=arg2;
add();
}
private void add() {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), ""+pos, Toast.LENGTH_SHORT).show();
switch(pos)
{
case 0:
sw_cities= new ArrayList<String>();
sw_cities.add("Select a City");
sw_city_adp=new ArrayAdapter<String>(Region.this,
android.R.layout.simple_dropdown_item_1line,sw_cities);
sw_city_adp.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
sp2.setAdapter(sw_city_adp);
select();
break;
case 1:
sw_cities= new ArrayList<String>();
sw_cities.add("Select a City");
sw_cities.add("Lake Charles");
sw_cities.add("Iowa");
sw_cities.add("Lake Arthur");
sw_city_adp=new ArrayAdapter<String>(Region.this,
android.R.layout.simple_dropdown_item_1line,sw_cities);
sw_city_adp.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
sp2.setAdapter(sw_city_adp);
select();
break;
}
}
private void select() {
// TODO Auto-generated method stub
sp2.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelectedregions(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
}
public void onNothingSelectedregions(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
public void onNothingSelectedcities(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
stings.xml
<resources>
<string name="app_name">Louisiana Festivals</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_region">Select a Region</string>
<string-array name="regions_array">
<item >Southwest</item>
</string-array>
<string name="select_a_region">Select a Region</string>
<string-array name="southwest_cities">
<item >Lake Charles</item>
<item >Iowa</item>
</string-array>
<string name="sw_select_a_city">Select a City</string>
<string-array name="lake_charles">
<item>Contraband days Pirate Festival</item>
<item>Other festival to be determined</item>
</string-array>
</resources>
You can get a string array from strings.xml using getStringArray(). So the code snippet you want is:
String[] regionsArray = getResources().getStringArray(R.array.regions_array);
regions = new ArrayList<String>(Arrays.asList(regionsArray));
Now whatever you enter into regions_array will end up in the regions ArrayList.
Related
I have one spinner which has some font size 14dp,16dp etc. when i select value from spinner
I want to change font size in text view for all activity of application at run time.
IS this possible?
I refer several answer but it doesn't work for me..
android dynamically change style at runtime
Try this code:
package com.packegename;
import android.app.Activity;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
public class AndroidTextSize extends Activity {
String[] optUnit = {"COMPLEX_UNIT_DIP",
"COMPLEX_UNIT_IN",
"COMPLEX_UNIT_MM",
"COMPLEX_UNIT_PT",
"COMPLEX_UNIT_PX",
"COMPLEX_UNIT_SP"};
int[] valueUnit = { TypedValue.COMPLEX_UNIT_DIP,
TypedValue.COMPLEX_UNIT_IN,
TypedValue.COMPLEX_UNIT_MM,
TypedValue.COMPLEX_UNIT_PT,
TypedValue.COMPLEX_UNIT_PX,
TypedValue.COMPLEX_UNIT_SP};
String[] optSize ={"0.05", "0.1", "0.25", "0.5", "1", "4", "8", "10", "14", "16", "20", "30"};
float[] valueSize = {0.05f, 0.1f, 0.25f, 0.5f, 1f, 4f, 8f, 10f, 14f, 16f, 20f, 30f};
Spinner selUnit, selSize;
TextView textOut;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
selUnit = (Spinner)findViewById(R.id.selunit);
selSize = (Spinner)findViewById(R.id.selsize);
textOut = (TextView)findViewById(R.id.textout);
ArrayAdapter<String> adapterUnit = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, optUnit);
adapterUnit.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
selUnit.setAdapter(adapterUnit);
selUnit.setOnItemSelectedListener(selUnitOnItemSelectedListener);
ArrayAdapter<String> adapterSize = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, optSize);
adapterSize.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
selSize.setAdapter(adapterSize);
selSize.setOnItemSelectedListener(selSizeOnItemSelectedListener);
}
private Spinner.OnItemSelectedListener selUnitOnItemSelectedListener
= new Spinner.OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
updateTextSize();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}};
private Spinner.OnItemSelectedListener selSizeOnItemSelectedListener
= new Spinner.OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
updateTextSize();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}};
private void updateTextSize(){
int unit = valueUnit[selUnit.getSelectedItemPosition()];
String strUnit = optUnit[selUnit.getSelectedItemPosition()];
float size = valueSize[selSize.getSelectedItemPosition()];
String strSize = optSize[selSize.getSelectedItemPosition()];
textOut.setTextSize(unit, size);
textOut.setText(strUnit + " : " + strSize);
}
}
above code is working for one activity and for single textview or other view ..
use this code globally for whole application activity modify
create one class. TextFontSize.class implement class with serializable
change updateTextSize() to.
public static void updateTextView(TextView theTextView, float theSize) {
theTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, theSize);
}
public static void updateEditText(EditText theEditText, float theSize) {
theEditText.setTextSize(TypedValue.COMPLEX_UNIT_DIP, theSize);
}
and any other Layout view just create method as above.
and use this function any activity of your application like
private void setAllFontSizeOf(float theSize) {
TextVFontsize.updateTextView(textview, theSize);
TextFontSize.updateEditText(Edittext,theSize);
}
and finally declare this method to particular activity.
I'm working on an order management system for android.
On the order page where the user submits an order, i want it to be able to allow the user to submit multiple orders, then take the user to a page where it displays all the orders made.
What method can i use to accomplish that?
This is the order activity page which is made up of 3 spinners and one textfield. This page takes the user to a ViewOrder page which displays what the user has selected. Instead of that i would like the user to submit multiple orders first, then see the display of all orders submitted. Any help is much appreciated.
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import com.example.androidhive.R;
public class OrderActivity extends Activity {
public Button btnOrder;
public EditText txtCrates;
public Spinner sp1, sp2, sp3;
public List<String> list;
public static String crates;
public static final String EXTRA_MESSAGE = "com.example.myspinner.msg1";
public static final String EXTRA_MESSAGE2 = "com.example.myspinner.msg2";
public static final String EXTRA_MESSAGE3 = "com.example.myspinner.msg3";
public static final String EXTRA_MESSAGE4 = "com.example.myspinner.MESSAGE";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.order);
sp1 = (Spinner) findViewById(R.id.spinner1);
sp2 = (Spinner) findViewById(R.id.spinner2);
sp3 = (Spinner) findViewById(R.id.spinner3);
btnOrder = (Button) findViewById(R.id.btnOrder);
txtCrates =(EditText) findViewById(R.id.crates);
list = new ArrayList<String> ();
list.add("CocaCola");
list.add("Sprite");
list.add("Fanta Orange");
list.add("Fanta Pineapple");
list.add("Fanta Blackcurrant");
list.add("Fanta Passion");
list.add("Krest");
list.add("Stoney");
list.add("Dasani");
list.add("Minute Maid Apple");
list.add("Minute Maid Mango");
list.add("Minute Maid Orange");
final String[] str1={"300ml","500ml","1 Litre","1.25 Litres","2 Litres"};
ArrayAdapter<String> adp1 = new ArrayAdapter<String>
(this, android.R.layout.simple_dropdown_item_1line, list);
ArrayAdapter<String> adp2 = new ArrayAdapter<String>
(this, android.R.layout.simple_dropdown_item_1line, str1);
ArrayAdapter<CharSequence> adp3 = ArrayAdapter.createFromResource
(this, R.array.str2, android.R.layout.simple_dropdown_item_1line);
sp1.setAdapter(adp1);
sp2.setAdapter(adp2);
sp3.setAdapter(adp3);
btnOrder.setOnClickListener(new View.OnClickListener() {
/** Called when the user clicks the Submit Order button */
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(),ViewOrder.class);
String msg1=sp1.getSelectedItem().toString();
String msg2=sp2.getSelectedItem().toString();
String msg3=sp3.getSelectedItem().toString();
String message = txtCrates.getText().toString();
intent.putExtra(EXTRA_MESSAGE, msg1);
intent.putExtra(EXTRA_MESSAGE2, msg2);
intent.putExtra(EXTRA_MESSAGE3, msg3);
intent.putExtra(EXTRA_MESSAGE4, message);
startActivity(intent);
}
});
sp1.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), list.get(arg2),
Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
sp2.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), str1[arg2],
Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
sp3.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String item = sp3.getSelectedItem().toString();
Toast.makeText(getBaseContext(), item, Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
}
Once try as follows take an List for multiple orders
ArrayList<String> orders=new ArrayList<String>();
as class variable in activity
then store a complete order as string with some special character as divider like
String msg1=sp1.getSelectedItem().toString();
String msg2=sp2.getSelectedItem().toString();
String msg3=sp3.getSelectedItem().toString();
String message = txtCrates.getText().toString();
orders.add(msg1+","+msg2+","+msg3+","+message);
then for intent add as follows
intent.putStringArrayListExtra("orders", orders);
Hope this will helps you.
I have a problem that i can't figure out how to solve. My code is the following:
package com.app.BoomBase;
import android.app.Activity;
import android.app.LauncherActivity.ListItem;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class Distance extends Activity implements OnItemSelectedListener {
Button Calc;
Spinner spType, spForce, spWeight;
double FarstCatch, TrickCatch, MTA, LongDistance ;
String[] types = { "Farst Catch", "Trick Catch", "MTA" , "Long Distance" };
EditText etWeight;
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.distance);
super.onCreate(savedInstanceState);
FarstCatch = 100;
TrickCatch = 43.56;
MTA = 32.60 ;
LongDistance = 25;
ArrayAdapter<String> AdapterT = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, types);
spType = (Spinner) findViewById(R.id.spType);
AdapterT.setDropDownViewResource(android.R.layout.simple_spinner_item);
spType.setAdapter(AdapterT);
spType.setPrompt("Select Type");
spType.setOnItemSelectedListener(this);
Button Calc = (Button) findViewById(R.id.button1);
Calc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String selected = spType.getSelectedItem().toString();
Toast.makeText(getApplicationContext(), "Your Boomerang will apoximately fly this far:" + selected, Toast.LENGTH_LONG).show();
}
});
}
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
int position = spType.getSelectedItemPosition();
switch (position){
case 0:
double FarstCatch;
break;
case 1:
double TrickCatch;
break;
case 2:
double MTA;
break;
case 3:
double LongDistance;
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
I want to take the variable that correspond to the seleceted case from the user input (spType), and then use that for a calculation. Exmple: If the user selectes FarstCatch i want to take the number 100 and use it in this calculation: R = m / FarstCatch.
How can i do that ?
Edit:
The new code:
package com.app.BoomBase;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class Distance extends Activity implements OnItemSelectedListener {
Button Calc;
Spinner spType, spForce, spWeight;
double FarstCatch, TrickCatch, MTA, LongDistance, r ;
String[] types = { "Farst Catch", "Trick Catch", "MTA" , "Long Distance" };
double[] values = {100, 43.56, 32.6, 25};
EditText etWeight;
int etNum;
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.distance);
super.onCreate(savedInstanceState);
FarstCatch = 100;
TrickCatch = 43.56;
MTA = 32.60 ;
LongDistance = 25;
ArrayAdapter<String> AdapterT = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, types);
spType = (Spinner) findViewById(R.id.spType);
AdapterT.setDropDownViewResource(android.R.layout.simple_spinner_item);
spType.setAdapter(AdapterT);
spType.setPrompt("Select Type");
spType.setOnItemSelectedListener(this);
etWeight = (EditText) findViewById(R.id.etWeight);
Button Calc = (Button) findViewById(R.id.button1);
Calc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String etValue = etWeight.getText().toString();
etNum = Integer.parseInt(etValue);
Toast.makeText(getApplicationContext(), "Your Boomerang will apoximately fly this far:" + r , Toast.LENGTH_LONG).show();
}
});
}
public void onItemSelected1(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// value corresponds to the selected spType
double value = values[arg2];
r = etNum/value;
}
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
int position = spType.getSelectedItemPosition();
switch (position){
case 0:
double FarstCatch;
break;
case 1:
double TrickCatch;
break;
case 2:
double MTA;
break;
case 3:
double LongDistance;
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
But it still don't work.. How can this be ?
Create another array for values corresponding to types array.
double[] values = {100, 43.56, 32.6, 25};
Next, implement onItemSelected() as follows -
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// value corresponds to the selected spType
double value = values[arg2];
// Do your calculation
//double R = m/value;
}
Note: arg2 is the selected item position so need to use getSelectedItem()
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
Sample Code:
public class Distance extends Activity implements OnItemSelectedListener {
Button Calc;
Spinner spType, spForce, spWeight;
double selectedValue, r;
String[] types = { "Farst Catch", "Trick Catch", "MTA" , "Long Distance" };
double[] values = {100, 43.56, 32.6, 25};
EditText etWeight;
int etNum;
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.distance);
super.onCreate(savedInstanceState);
ArrayAdapter<String> AdapterT = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, types);
spType = (Spinner) findViewById(R.id.spType);
AdapterT.setDropDownViewResource(android.R.layout.simple_spinner_item);
spType.setAdapter(AdapterT);
spType.setPrompt("Select Type");
spType.setOnItemSelectedListener(this);
etWeight = (EditText) findViewById(R.id.etWeight);
Button Calc = (Button) findViewById(R.id.button1);
Calc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String etValue = etWeight.getText().toString();
etNum = Integer.parseInt(etValue);
r = etNum/selectedValue;
Toast.makeText(getApplicationContext(), "Your Boomerang will apoximately fly this far:" + r , Toast.LENGTH_LONG).show();
}
});
}
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// value corresponds to the selected spType
selectedValue = values[arg2];
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
}
I am developing a Keyboard. When i am inflating a ListView in onCreateInputView() and returning same View(or parent View) to it. I have implemented setOnItemClickListener for the ListView i am not getting callback to it.
What might be the issue?
android framework doesn't work for listview for keyboard(InputMethodService) ?
FY Reference i am attaching the code please have look at it.
NOTE: i am able to get ontouchlistner of it.
package com.listkeyboard;
import java.util.ArrayList;
import android.content.Context;
import android.content.res.Configuration;
import android.inputmethodservice.InputMethodService;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class ListKeyBoardIME extends InputMethodService {
private LinearLayout mainLayout;
#Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
}
#Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
System.out.println("onCreate");
}
#Override
public View onCreateCandidatesView() {
// TODO Auto-generated method stub
System.out.println("onCreateCandidatesView");
return super.onCreateCandidatesView();
}
#Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
System.out.println("onDestroy");
}
#Override
public void onFinishInput() {
// TODO Auto-generated method stub
super.onFinishInput();
System.out.println("onFinishInput");
}
#Override
public void onFinishInputView(boolean finishingInput) {
// TODO Auto-generated method stub
super.onFinishInputView(finishingInput);
System.out.println("onFinishInputView");
}
#Override
public View onCreateInputView() {
// TODO Auto-generated method stub
System.out.println(" onCreateInputView");
LayoutInflater layoutInflater = (LayoutInflater) ListKeyBoardIME.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View mMainKeyboardLayout = layoutInflater.inflate(R.layout.keyboard_list_view, null);
mainLayout = (LinearLayout)mMainKeyboardLayout.findViewById(R.id.main_layout);
ListView listView = (ListView)mainLayout.findViewById(R.id.list);
ArrayList<Character> alphabets = new ArrayList<Character>();
for (char ch = 'a'; ch <= 'z'; ch++) {
alphabets.add(ch);
if (ch == 'n') {
break;
}
}
ArrayAdapter<Character> adapter = new ArrayAdapter<Character>(ListKeyBoardIME.this, android.R.layout.simple_list_item_1,alphabets);
listView.setAdapter(adapter);
listView.setItemsCanFocus(false);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(getApplicationContext(), "on itemclicked clicked" , Toast.LENGTH_SHORT).show();
}
});
setInputView(mainLayout);
return mainLayout;
}
#Override
public void onStartCandidatesView(EditorInfo info, boolean restarting) {
// TODO Auto-generated method stub
super.onStartCandidatesView(info, restarting);
System.out.println("onStartCandidatesView ");
}
#Override
public void onStartInput(EditorInfo attribute, boolean restarting) {
// TODO Auto-generated method stub
super.onStartInput(attribute, restarting);
System.out.println("onStartInput ");
}
#Override
public void onStartInputView(EditorInfo info, boolean restarting) {
// TODO Auto-generated method stub
super.onStartInputView(info, restarting);
System.out.println("onStartInputView");
}
}
I am also face the same problem & after lot of searching when no answer work for me, i implement below code in my list adapter & its work for me. If anyone find any other better answer please let us know.
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
I'm using an AutoCompleteTextView to suggest user some words from my sqlite db as they type the input string to search.
I try to make the suggestion look friendly by using simple_list_item_2, here's my code:
package com.suit.kamus;
import android.app.Activity;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class SuitAuto extends Activity implements TextWatcher{
AutoCompleteTextView auto; TextView result;
Button search; Button add; Spinner chooser;
String input; static String selection; String main;
String[] options = {"en to ina", "ina to en"};
SimpleCursorAdapter simple;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
auto = (AutoCompleteTextView)findViewById(R.id.auto);
auto.addTextChangedListener(this);
result = (TextView)findViewById(R.id.result);
chooser = (Spinner)findViewById(R.id.chooser);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, options);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
chooser.setAdapter(adapter);
KamusDbAdapter dbHelper = new KamusDbAdapter(getApplicationContext());
dbHelper.open();
String status = dbHelper.getstatedb();
selection = status;
dbHelper.close();
if (selection.equalsIgnoreCase("en")){
chooser.setSelection(0);
} else {chooser.setSelection(1);}
Log.d("statelang", selection);
add = (Button)findViewById(R.id.add);
add.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startAdding(main);
}
});
chooser.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
if (chooser.getSelectedItemId() == 0){
selection = "en";
select();
updateDb();
}else{
selection = "ina";
select();
updateDb();
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
auto.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
main = auto.getText().toString();
//Log.v("CURSOR",finish);
result.setText("");
auto.setText("");
}
});
}
public void startAdding(String dWord) {
// TODO Auto-generated method stub
KamusDbAdapter adding = new KamusDbAdapter(getApplicationContext());
adding.open();
adding.Favorite(dWord);
adding.close();
}
protected void updateDb() {
// TODO Auto-generated method stub
KamusDbAdapter save = new KamusDbAdapter(getApplicationContext());
save.open();
save.updatestatedb(selection);
save.close();
}
public static String select() {
// TODO Auto-generated method stub
Log.v("STRING",selection);
return selection;
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
input = auto.getText().toString();
KamusDbAdapter x = new KamusDbAdapter(getApplicationContext());
x.open();
Cursor cur = x.getCall(input, selection);
//getCall is in KamusDbAdapter class, it used to return result cursor from sqlite db
//i use rawQuery "SELECT * FROM en_to_ina WHERE word LIKE 'input%'"
x.close();
String[] displayFields = new String[] {"word", "meaning"};
int[] displayViews = new int[] { android.R.id.text1,android.R.id.text2 };
simple = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, cur,displayFields, displayViews);
auto.setAdapter(simple);
}
}
I got a problem with retrieving the string from clicked item. It is in:
auto.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
main = auto.getText().toString();
//Log.v("CURSOR",finish);
result.setText("");
auto.setText("");
}
});
I need both strings from word and meaning fields.
Any response would be great...
You need to use http://developer.android.com/reference/android/widget/CursorAdapter.html#getItem(int).
Cursor cursor = (Cursor) simple.getItem(position);
// retrieve the data from the cursor
This seems to be a problem with SimpleCursorAdapter on 2.1, on 2.2 the cursor is positioned on the requested item and you can retrieve the column data but in 2.1 the cursor position is 0 and cursor.move(itemIndex) and cursor.moveToFirst() both return false.
I plan to do a RYO database adapter.
Getting string from SimpleCurstor Adapter using cursor
#Override
public boolean onSuggestionClick(int position) {
//First get cursor from your Adapter,
Cursor cursor = (Cursor) mAdapter.getItem(position);
/* Then get string data from cursor's column, I am getting it from column 0 in this case. You can use your own column index. */
String s=cursor.getString(0);
//Then set the searchview or autotextview with that string
mSearchView.setQuery(s, true); //true will submit the query
}