Friends, I am using AutoCompleteTextView. The Suggestion :
String[] recipes={ "Fish", "Chicken", "Mutton"};
How do I do this:
When I select one of the item from the dropdown list, it will go to another event?
For example, I type Fi, it will come out Fish from the dropdown list and then I select Fish, it will go to another Activity.
package net.learn2develop.Activities;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.AdapterView;
import android.view.View;
import android.content.Intent;
public class AutoCompleteTextActivity extends Activity {
String[] recipes ={
"Nasi Lemak With Ikan Bilis",
"Steamed Cod Fish"
};
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, recipes);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.txtRecipes);
textView.setThreshold(3);
textView.setAdapter(adapter);
textView.setOnItemSelectedListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> av, View view, int index, long id){
Intent i=new Intent(this,Activity2.class);
i.putExtra("item",recipes[index]);
StartActivity(i);
}
});
}
}
You can use a class that implements TextWatcher and override following methods:
#Override
public void afterTextChanged(final Editable editable) {
// check if entered text is "fish" and if yes then start the new activity.
}
#Override
public void beforeTextChanged(final CharSequence string,
final int start, final int count, final int after) {
}
#Override
public void onTextChanged(final CharSequence string, final int start,
final int before, final int count) {
}
}
receipesBelow Snippet will help you.
autoCompleteTextView.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> av, View view,
int index, long id)
{
//index will give you item which you selected
Now start another activity here
Intent i=new Intent(context,SecondActivity.class);
i.putExtra("item",recipes[index]);
StartActivity(i);
}
#Override
public void onNothingSelected(AdapterView<?> arg0)
{
// TODO Auto-generated method stub
}
})
SecondActivity.java
You can retrieve that value is onCreate like below
String selectedItem=getIntent().getStringExtra("item");
You should have used setOnItemClickListener instead of setOnItemSelectedListener.
Related
This code is working properly by showing all the data in database but i have a problem in filtering. I try many code but nothing works , can someone help me out ? thanks
package com.example.dictionary;
import java.util.List;
import com.example.dictionary.R;
import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.support.v4.widget.SimpleCursorAdapter;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
public class MainActivity extends Activity{
Cursor cursor;
ListView listView;
SimpleCursorAdapter adapter;
Button back, clear;
List<String> items;
String get;
EditText et;
int textlength = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText) findViewById(R.id.editText1);
Opening my database and getting all data
Historydb db = new Historydb(this);
db.open();
cursor = db.getword();
startManagingCursor(cursor);
cursor.moveToFirst();
adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, cursor,
new String[] { "word" }, new int[] { android.R.id.text1 });
listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(adapter);
I set addtextchangelistener in my edittext.
et.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
// Abstract Method of TextWatcher Interface.
}
public void beforeTextChanged(CharSequence s, int start, int count,int after) {
// Abstract Method of TextWatcher Interface.
}
public void onTextChanged(CharSequence s, int start, int before,int count) {
adapter.getFilter().filter(s);
}});
}
}
it is not working because adapter.getfilter().filter(cs); does not work directly for SimpleCursorAdapter.
you have to use adapter.setFilterQueryProvider first for SimpleCursorAdapter.
here is the complete description: ListView, SimpleCursorAdapter, an an EditText filter -- why won't it do anything?
and this: Using an EditText to filter a SimpleCursorAdapter-backed ListView
Try this....
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
((SimpleAdapter) YourActivity.this.adapter).getFilter().filter(cs);
}
I have a listview in my activity. I want to show the data of it when clicked on it on second activity. But is not able to do so. Help. This is the code. What to do so that my data is fetched from listview and is shown in next activity?
ListActivity.java
package com.example.task;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class ListActivity extends Activity implements OnItemClickListener{
ListView list1;
LoginDataBaseAdapter loginDataBaseAdapter;
ArrayList<HashMap<String, String>> datalist;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
loginDataBaseAdapter=new LoginDataBaseAdapter(this);
loginDataBaseAdapter=loginDataBaseAdapter.open();
list1=(ListView)findViewById(R.id.list);
datalist=loginDataBaseAdapter.getAllAnimals();
ArrayAdapter<HashMap<String, String>> adapter=new ArrayAdapter<HashMap<String,String>>(getApplicationContext(), android.R.layout.simple_dropdown_item_1line,datalist);
list1.setAdapter(adapter);
list1.setOnItemClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.list, menu);
return true;
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
}
}
you can simple use this:
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
String mydata= datalist.get(arg2).toString();
// pass this data to your second activity
Intent n = new Intent(YourActivityName.this ,SecondActivity.class);
n.putExtra("key", mydata);
startActivity(intent);
}
Now retrieve in your SecondActivity on oncreate() method:
Intent n= getIntent();
String data = intent.getStringExtras("key");
String yourdata;
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
yourdata= list1.getItemAtPosition(arg2).toString();
// pass this data to your second activity
}
I think you are trying to achieve like this..
String DATA;
#Override
public void onItemClick(AdapterView<?> arg0, View view, int pos,
long arg3) {
DATA= YOUR_LIST.getItemAtPosition(pos).toString();
Intent intent = new Intent(getApplicationContext(),SECOND_ACTIVITY.class);
intent.putExtra("DATA",DATA);
startActivity(intent);
}
Hope it will help you..!!
Well apart from passing data by intents, you can also use Global Variables.
You can check out an example here.
You can set your global variable/s to the value that is applicable when list item is selected (using your onItemClick listner) and then access it in other activity.
Eg:
CLASS MyApp
class MyApp extends Application
{ public String whatever;}
CODE: ACTIVITY 1
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// TODO Auto-generated method stub
MyApp as = ((MyApp)getApplicationContext());
as.whatever = position + "Clicked";
}
CODE: ACTIVITY 2
class Blah extends Activity
{
#Override
public void onCreate(Bundle b){
...
MyApp as = ((MyApp)getApplicationContext());
Log.d(as.whatever);
...}
}
MainActivity
String data;
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// TODO Auto-generated method stub
data = adapter.getItem(position);
Intent intent = new Intnet(MainActivity.this, NextActivity.class);
i.putExtra("data", data);
startActivity(intent);
}
to open your data on NextActivity
Intent intent = getIntent();
String passData= intent.getExtras().getString("data");
i want to add two numbers using spinner view. here in my code two spinners .After i run the emulator it displays straight result only. it does not display spinner control and i'm not able to select the two numbers. Pls give one solution. Thanks in advance. Here code
package com.kk;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.TextView;
import android.R.id;
public class TrckActivity extends Activity {
/** Called when the activity is first created. */
String[] a={"-select-","1","2"};
String[] b={"-select-","2","4"};
int first,second,f,s,c;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayAdapter<String> a1= new ArrayAdapter<String> (this,android.R.layout.simple_dropdown_item_1line,a);
final Spinner sp1=(Spinner)findViewById(R.id.spinner1);
sp1.setAdapter(a1);
sp1.setOnItemSelectedListener(new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
first=sp1.getSelectedItemPosition();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
ArrayAdapter<String> a2= new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,b);
final Spinner sp2=(Spinner)findViewById(R.id.spinner1);
sp2.setAdapter(a2);
sp2.setOnItemSelectedListener(new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
second=sp2.getSelectedItemPosition();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
if(first==1)
{
f=1;
}
else if(first==2)
{
f=2;
}
if(second==1)
{
s=2;
}
else if(second==2)
{
s=3;
}
c=f+s;
TextView tv=new TextView(this);
tv.setText(""+c);
setContentView(tv);
}
}
This might be because the spinner's "onItemSelected" method gets called initially as soon as your code enters the onCreate method. Maybe you have to maintain flag values to do this.
These links might help you get started with it,
Spinner onItemSelected called erroneously (without user action)
Spinner onItemSelected() executes when it is not suppose to
Try to exchange
android.R.layout.simple_dropdown_item_1line
to
android.R.layout.simple_spinner_item
Basically, I want to create a ListView with a bunch of items on it, and whenever the user click any item, it will open a new Activity according to which item he clicks. PLease, help me I would really appreciate it a lot.
Here is my my code:
package com.hipeople;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
public class char1 extends Activity {
/** Called when the activity is first created. */
private ListView lv1;
private EditText ed;
private String lv_arr[]={"Android","Cupcake","Donut","Eclairs","AndroidPeople","Froyo",};
private ArrayList<String> arr_sort= new ArrayList<String>();
int textlength=0;
#Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
lv1=(ListView)findViewById(R.id.ListView01);
ed=(EditText)findViewById(R.id.EditText01);
lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr));
ed.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
textlength=ed.getText().length();
arr_sort.clear();
for(int i=0;i<lv_arr.length;i++)
{
if(textlength<=lv_arr[i].length())
{
if(ed.getText().toString().equalsIgnoreCase((String) lv_arr[i].subSequence(0, textlength)))
{
arr_sort.add(lv_arr[i]);
}
}
}
}
});
}
}
Try this,
lv1.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3)
{
String str = ((TextView) arg1).getText().toString();
Toast.makeText(getBaseContext(),str, Toast.LENGTH_LONG).show();
Intent intent = new Intent(getBaseContext(),your_new_Intent.class);
intent.putExtra("list_view_value", str);
startActivity(intent);
}
});
**I am trying to retrieve the 2nd value from a Arraylist/ArrayAdapter that I have populated. I am new to Array so please correct me if I am wrong
Q1. I created the Array Favorite. What I think what I created is an Array with two set of value call Detail | Value. example Detail="Yasmin",Value="8". Is this correct?
Q2. I have assign the Favorite Array to the mFavlist listview. During the OnItemClick I can return the label "Yasmin" by the position of the listview. What I would like to do is return the value of "8". What would be the best way to do this?
Please let me know if I am on the right track via the array and adapter**
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class lister extends Activity {
/** Called when the activity is first created. */
TextView txHomeTeam;
protected ListView mFavlist;
protected ArrayList<Favorite> fakeFavs = new ArrayList<Favorite>();
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
this.setContentView(R.layout.main);
this.txHomeTeam = (TextView)this.findViewById(R.id.title);
fakeFavs.add(new Favorite("John", "1"));
fakeFavs.add(new Favorite("Yasmin", "8"));
fakeFavs.add(new Favorite("Jack", "10"));
//this.mFavlist = (ListView) this.findViewById(R.id.list_favorites);
this.mFavlist = (ListView) this.findViewById(R.id.list_favorites);
initListView();
mFavlist.setTextFilterEnabled(true);
mFavlist.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView arg0, View view,
int position, long id) {
// user clicked a list item,
//and read the value from <Favorite>.value
//txHomeTeam.setText=????
}
});
}
public void refreshFavListItems() {
mFavlist.setAdapter(new ArrayAdapter<Favorite>(this,
android.R.layout.simple_list_item_1, fakeFavs));
}
public void initListView() {
/* Loads the items to the ListView. */
refreshFavListItems();
}
protected class Favorite {
protected String Detail;
protected String value;
protected Favorite(String Detail, String value) {
this.Detail = Detail;
this.value = value;
}
public String toString() {
return Detail;
}
}
}
Use the position parameter to get to the correct position in your adapter
public void onItemClick(AdapterView arg0, View view,
int position, long id) {
Favorite selectedFav=lister.this.fakeFavs.getItem(position)
}