How to get the value from onItemClick ListView in Android - android

I have a global variable myval which is of String type.
String[] Category={"----SELECT----","BEVERAGES","BREAKFAST","LUNCH","DINNER","DESSERTS","APPETIZERS & SIDES"};
String[] Beverage={"PEPSI","COKE","LASSI","FALOODA","BUTTER MILK","GREEN TEA","BADAM MILK","MASALA CHAI" };
String[] Breakfast={"SIRLOIN & EGG","CFS STEAK & EGG","T-BONE & EGG","TWO EGGS BREAKFAST","2EGG W/MEAT","BEL WAFFLE","BEL WAFFLE W/MEAT","BLUEBERRY CAKE","CAKES","FABULOUS FRENCH TST","MOONS","FRENCH TST"};
String[] Lunch={" CRAB CAKE SANDWICH","TUSCAN GRILLED CHICKEN PANINI","SOUTHWEST TURKEY CLUB"," LOBSTER ROLL","TUNA MELT"," FRENCH DIP","YOUR OWN SANDWICH"};
String[] Dinner={"DUBLIN BAY PRAWN","CRAB FROM BRITTANY","WHITE ASPARAGUS","BLEWIT MUSHROOM","JOHN DORY FISH","MONKFISH","VEAL SWEET BREAD","LAMB","PIGEON FRY"};
String[] Desserts={"ECHOURGNAC CHEESE","HAZELNUT","GARIGUETTE STRAWBERRY","MOUSSE & ZEST","APRICOT","CHOCOLATE"};
String[] Appetizers={"Greek Artichoke Spinach","Seafood Appetizers","Tapenade Flatbread","Cranberry Blue Cheese","Fig and Blue Cheese","Sun-Dried Tomato","Tropical Crab Rangoon"};
class GlobalClass extends Application {
public static String[] myval;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
spinner = (Spinner) findViewById(R.id.spinnerCategory);
l1=(ListView) findViewById (R.id.list);
b1=(Button)findViewById(R.id.button3);
b2=(Button)findViewById(R.id.button2);
b3=(Button)findViewById(R.id.button1);
e1=(EditText)findViewById(R.id.editText1);
e2=(EditText)findViewById(R.id.editText2);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, Category);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
final ArrayAdapter<String> bever = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Beverage);
// Assign adapter to ListView
final ArrayAdapter<String> Breakf = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, Breakfast);
final ArrayAdapter<String> lunc= new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, Lunch);
final ArrayAdapter<String> Dinn= new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, Dinner);
final ArrayAdapter<String> Dessert = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, Desserts);
final ArrayAdapter<String> Appet= new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, Appetizers);
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> parent, View arg1, int pos,long id){
String Text = parent.getSelectedItem().toString();
if(Text.equals("----SELECT----")) {
}
else if(Text.equals("BEVERAGES")){
l1.setAdapter(bever);
return;
}
else if(Text.equals("BREAKFAST")){
l1.setAdapter(Breakf);
return;
}
else if(Text.equals("LUNCH")){
l1.setAdapter(lunc);
return;
}
else if(Text.equals("DINNER")){
l1.setAdapter(Dinn);
return;
}
else if(Text.equals("DESSERTS")){
l1.setAdapter(Dessert);
return;
}
else if(Text.equals("APPETIZERS & SIDES")){
l1.setAdapter(Appet);
return;
}
}
public void onNothingSelected(AdapterView<?> arg0){
}
});
l1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), "SELECTED :: " +((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
long ii=(long) l1.getItemIdAtPosition((int) id);
//Get the value from listView click item
String[] S5=new String[]{a5};
//String[] S1=new String[]{s2};
GlobalClass.myval=S5;
});
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String tno = e1.getText().toString();
int tn = Integer.parseInt(tno);
Intent i=new Intent(getApplicationContext(),TicketActivity.class);
Bundle b=new Bundle();
b.putInt("Table No:", tn);
i.putExtras(b);
String et= e2.getText().toString();
int et1 = Integer.parseInt(et);
Bundle be=new Bundle();
be.putInt("Guest:", et1);
i.putExtras(be);
startActivity(i);
}
});
b3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent=new Intent(HomeActivity.this,PaynowActivity.class);
startActivity(intent);
}
});
b2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent m=new Intent(HomeActivity.this,OrderinfoActivity.class);
startActivity(m);
}
});
}
}
How do I get the value from onItemClick in the ListView?

ArrayAdapter<String> adapter=new ArrayAdapter<String>(HotelActivity.this, android.R.layout.simple_list_item_1,Category);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
if(position>0){
Intent intent=new Intent(HotelActivity.this,NewList.class);
intent.putExtra("position", position);
startActivity(intent);
}
}
});
Create new Activity and declare listview in its xml file
NewList Activity:
public class NewList extends Activity{
ListView listView;
int pos=0;
ArrayAdapter<String> adapter;
String[] Beverage={"PEPSI","COKE","LASSI","FALOODA","BUTTER MILK","GREEN TEA","BADAM MILK","MASALA CHAI" };
String[] Breakfast={"SIRLOIN & EGG","CFS STEAK & EGG","T-BONE & EGG","TWO EGGS BREAKFAST","2EGG W/MEAT","BEL WAFFLE","BEL WAFFLE W/MEAT","BLUEBERRY CAKE","CAKES","FABULOUS FRENCH TST","MOONS","FRENCH TST"};
String[] Lunch={" CRAB CAKE SANDWICH","TUSCAN GRILLED CHICKEN PANINI","SOUTHWEST TURKEY CLUB"," LOBSTER ROLL","TUNA MELT"," FRENCH DIP","YOUR OWN SANDWICH"};
String[] Dinner={"DUBLIN BAY PRAWN","CRAB FROM BRITTANY","WHITE ASPARAGUS","BLEWIT MUSHROOM","JOHN DORY FISH","MONKFISH","VEAL SWEET BREAD","LAMB","PIGEON FRY"};
String[] Desserts={"ECHOURGNAC CHEESE","HAZELNUT","GARIGUETTE STRAWBERRY","MOUSSE & ZEST","APRICOT","CHOCOLATE"};
String[] Appetizers={"Greek Artichoke Spinach","Seafood Appetizers","Tapenade Flatbread","Cranberry Blue Cheese","Fig and Blue Cheese","Sun-Dried Tomato","Tropical Crab Rangoon"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylist);
listView=(ListView) findViewById(R.id.newlist);
Bundle bundle=getIntent().getExtras();
if(bundle!=null){
pos=bundle.getInt("position");
}
if(pos==1){
adapter=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,Beverage);
listView.setAdapter(adapter);
}
else if(pos==2){
adapter=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,Breakfast);
listView.setAdapter(adapter);
}
else if(pos==3){
adapter=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,Lunch);
listView.setAdapter(adapter);
}
else if(pos==4){
adapter=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,Dinner);
listView.setAdapter(adapter);
}
else if(pos==5){
adapter=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,Desserts);
listView.setAdapter(adapter);
}
else if(pos==6){
adapter=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,Appetizers);
listView.setAdapter(adapter);
}
}
}
Hope this will help you.

Related

Spinner selecting step by step depend on selected value

I have 3 spinners and depend on each other like that;
Category1
Category2
Category3
When user select Category1, 2.spinner should show that;
Category1-1
Category1-2
Category1-3
And user select Category1-2, 3.spinner should show that;
Category1-1-1
Category1-1-2
Category1-1-3
I implement this way, but it is too long
so i want to any suggestion for more efficient code
public class BookActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
Spinner sp1,sp2,sp3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_book);
sp1 = (Spinner) findViewById(R.id.sp1);
sp2 = (Spinner) findViewById(R.id.sp2);
sp3 = (Spinner) findViewById(R.id.sp3);
List<String> list = new ArrayList<String>();
list.add("Category1");
list.add("Category2");
list.add("Category3");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp1.setAdapter(adapter);
sp1.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
Toast.makeText(this, "YOUR SELECTION IS : " + parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();
fillSpinnerList(parent.getItemAtPosition(position).toString());
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
public void fillSpinnerList(String selectedSpinnerName){
if(selectedSpinnerName =="Category1" ){
List<String> list = new ArrayList<String>();
list.add("Category1-1");
list.add("Category1-2");
list.add("Category1-3");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp2.setAdapter(adapter);
sp2.setOnItemSelectedListener(this);
}
if(selectedSpinnerName =="Category2" ){
List<String> list = new ArrayList<String>();
list.add("Category2-1");
list.add("Category2-2");
list.add("Category2-3");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp2.setAdapter(adapter);
sp2.setOnItemSelectedListener(this);
}
if(selectedSpinnerName =="Category3" ){
List<String> list = new ArrayList<String>();
list.add("Category3-1");
list.add("Category3-2");
list.add("Category3-3");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp2.setAdapter(adapter);
sp2.setOnItemSelectedListener(this);
}
if(selectedSpinnerName =="Category1-1" ){
List<String> list = new ArrayList<String>();
list.add("Category1-1-1");
list.add("Category1-1-2");
list.add("Category1-1-3");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp3.setAdapter(adapter);
sp3.setOnItemSelectedListener(this);
}
if(selectedSpinnerName =="Category1-2" ){
List<String> list = new ArrayList<String>();
list.add("Category1-2-1");
list.add("Category1-2-2");
list.add("Category1-2-3");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp3.setAdapter(adapter);
sp3.setOnItemSelectedListener(this);
}
if(selectedSpinnerName =="Category1-3" ){
List<String> list = new ArrayList<String>();
list.add("Category1-3-1");
list.add("Category1-3-2");
list.add("Category1-3-3");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp3.setAdapter(adapter);
sp3.setOnItemSelectedListener(this);
}
}
}
You can refactor it like that. It's still long as a part of it can be generalised but it's more readable.
BookActivity
public class BookActivity extends AppCompatActivity {
Spinner sp1, sp2, sp3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_book);
sp1 = (Spinner) findViewById(R.id.sp1);
sp2 = (Spinner) findViewById(R.id.sp2);
sp3 = (Spinner) findViewById(R.id.sp3);
initSpinner1();
}
private void initSpinner1() {
final List<String> list = new ArrayList<>();
list.add("Category1");
list.add("Category2");
list.add("Category3");
initSpinner(sp1, list, new SpinnerListener.Callback() {
#Override
public void onResult(String itemName) {
initSpinner2(itemName);
}
});
}
private void initSpinner2(String selectedCategory) {
final List<String> list = new ArrayList<>();
switch (selectedCategory) {
case "Category1":
list.add("Category1-1");
list.add("Category1-2");
list.add("Category1-3");
break;
case "Category2":
list.add("Category2-1");
list.add("Category2-2");
list.add("Category2-3");
break;
case "Category3":
list.add("Category3-1");
list.add("Category3-2");
list.add("Category3-3");
break;
default:
break;
}
initSpinner(sp2, list, new SpinnerListener.Callback() {
#Override
public void onResult(String itemName) {
initSpinner3(itemName);
}
});
}
public void initSpinner3(String selectedCategory) {
final List<String> list = new ArrayList<>();
switch (selectedCategory) {
case "Category1-1":
list.add("Category1-1-1");
list.add("Category1-1-2");
list.add("Category1-1-3");
break;
case "Category1-2":
list.add("Category1-2-1");
list.add("Category1-2-2");
list.add("Category1-2-3");
break;
case "Category1-3":
list.add("Category1-3-1");
list.add("Category1-3-2");
list.add("Category1-3-3");
break;
default:
break;
}
initSpinner(sp3, list, new SpinnerListener.Callback() {
#Override
public void onResult(String itemName) {
// TODO
}
});
}
private void initSpinner(Spinner spinner, List<String> list, SpinnerListener.Callback callback) {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
final AdapterView.OnItemSelectedListener listener = new SpinnerListener(this, callback);
spinner.setOnItemSelectedListener(listener);
}
}
The implementation of the OnItemSelectedListener
public class SpinnerListener implements AdapterView.OnItemSelectedListener {
private final Context mContext;
private final Callback mCallback;
public SpinnerListener(Context context, Callback callback) {
mContext = context;
mCallback = callback;
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
final String itemName = parent.getItemAtPosition(position).toString();
Toast.makeText(mContext, "YOUR SELECTION IS : " + itemName, Toast.LENGTH_SHORT).show();
mCallback.onResult(itemName);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// Do nothing
}
public interface Callback {
void onResult(String itemName);
}
}
The code can be shorten a little bit if you use lambda function (Java 8). For instance:
initSpinner(sp2, list, new SpinnerListener.Callback() {
#Override
public void onResult(String itemName) {
initSpinner3(itemName);
}
});
can be written:
initSpinner(sp2, list, itemName -> initSpinner3(itemName));
or even shorter:
initSpinner(sp2, list, this::initSpinner3);
EDIT
Or you can do something like that by using String arrays instead of lists. You can export the methods to init the array into a separated file so it makes the code in the Activity lighter:
public class BookActivity extends AppCompatActivity {
Spinner sp1, sp2, sp3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_book);
sp1 = (Spinner) findViewById(R.id.sp1);
sp2 = (Spinner) findViewById(R.id.sp2);
sp3 = (Spinner) findViewById(R.id.sp3);
initSpinner1();
}
private void initSpinner1() {
final String[] list = SpinnerArrayInitializer.getSpinner1Array();
initSpinner(sp1, list, itemName -> initSpinner2(itemName));
}
private void initSpinner2(String selectedCategory) {
final String[] list = SpinnerArrayInitializer.getSpinner2Array(selectedCategory);
initSpinner(sp2, list, itemName -> initSpinner3(itemName));
}
public void initSpinner3(String selectedCategory) {
final String[] list = SpinnerArrayInitializer.getSpinner3Array(selectedCategory);
initSpinner(sp3, list, itemName -> {
// TODO
});
}
private void initSpinner(Spinner spinner, String[] list, SpinnerListener.Callback
callback) {
final ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
final AdapterView.OnItemSelectedListener listener = new SpinnerListener(this, callback);
spinner.setOnItemSelectedListener(listener);
}
}
And the to initialise the arrays:
public interface SpinnerArrayInitializer {
static String[] getSpinner1Array() {
return new String[]{"Category1", "Category2", "Category3"};
}
static String[] getSpinner2Array(String selectedCategory) {
switch (selectedCategory) {
case "Category1":
return new String[]{"Category1-1", "Category1-2", "Category1-3"};
case "Category2":
return new String[]{"Category2-1", "Category2-2", "Category2-3"};
case "Category3":
return new String[]{"Category3-1", "Category3-2", "Category3-3"};
default:
return new String[0];
}
}
static String[] getSpinner3Array(String selectedCategory) {
switch (selectedCategory) {
case "Category1-1":
return new String[]{"Category1-1-1", "Category1-1-2", "Category1-1-3"};
case "Category1-2":
return new String[]{"Category1-2-1", "Category1-2-2", "Category1-2-3"};
case "Category1-3":
return new String[]{"Category1-3-1", "Category1-3-2", "Category1-3-3"};
default:
return new String[0];
}
}
}
Try this:
public class BookActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
private final static int SP1_NUM_OF_OPTIONS = 3;
private final static int SP2_NUM_OF_OPTIONS = 3;
private final static int SP3_NUM_OF_OPTIONS = 3;
private final static String SP3_HINT = "<SELECT>";
Spinner sp1, sp2, sp3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_book);
sp1 = (Spinner) findViewById(R.id.sp1);
sp2 = (Spinner) findViewById(R.id.sp2);
sp3 = (Spinner) findViewById(R.id.sp3);
// Setup data list and adapter for sp1.
ArrayList<String> list = new ArrayList<>();
for(int i=1; i<=SP1_NUM_OF_OPTIONS; i++){
list.add("Category" + i);
}
ArrayAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp1.setAdapter(adapter);
// Set listeners for all spinners.
sp1.setOnItemSelectedListener(this);
sp2.setOnItemSelectedListener(this);
sp3.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(this, "YOUR SELECTION IS : " + parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();
fillSpinnerList(parent.getId(), parent.getItemAtPosition(position).toString());
// Automatically do something different after last step is selected.
if(parent.getId() == R.id.sp3){
if(!parent.getItemAtPosition(position).toString().equals(SP3_HINT)){
// Do something
}
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {}
public void fillSpinnerList(int spinnerId, String selectedSpinnerName){
ArrayAdapter adapter;
if(spinnerId == R.id.sp1){
String[] list1;
list1 = getResources().getStringArray(getResources().getIdentifier(selectedSpinnerName, "array", getPackageName()));
adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, list1);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp2.setAdapter(adapter);
}else if(spinnerId == R.id.sp2){
ArrayList<String> list = new ArrayList<>();
list.add(SP3_HINT);
String[] list1;
list1 = getResources().getStringArray(getResources().getIdentifier(selectedSpinnerName, "array", getPackageName()));
list.addAll(Arrays.asList(list1));
adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp3.setAdapter(adapter);
}
}
}
Add your Category in strings.xml:
<array name="Category1">
<item>Category1_1</item>
<item>Two</item>
<item>Three</item>
</array>
<array name="Category1_1">
<item>Category1_1_1</item>
<item>Category1_1_2</item>
<item>Category1_1_3</item>
</array>
<array name="Two">
<item>Category1_2_1</item>
<item>Category1_2_2</item>
<item>Category1_2_3</item>
</array>
<array name="Three">
<item>Category1_3_1</item>
<item>Category1_3_2</item>
<item>Category1_3_3</item>
</array>
<array name="Category2">
<item>Category2_1</item>
<item>Four</item>
<item>Five</item>
</array>
<array name="Category2_1">
<item>Category2_1_1</item>
<item>Category2_1_2</item>
<item>Category2_1_3</item>
</array>
<array name="Four">
<item>Category2_2_1</item>
<item>Category2_2_2</item>
<item>Category2_2_3</item>
<item>Category2_2_4</item>
</array>
<array name="Five">
<item>Category2_3_1</item>
<item>Category2_3_2</item>
<item>Category2_3_3</item>
<item>Category2_3_4</item>
<item>Category2_3_5</item>
</array>
<array name="Category3">
<item>Category3_1</item>
<item>Six</item>
<item>Seven</item>
</array>
<array name="Category3_1">
<item>Category3_1_1</item>
<item>Category3_1_2</item>
<item>Category3_1_3</item>
</array>
<array name="Six">
<item>Category3_2_1</item>
<item>Category3_2_2</item>
<item>Category3_2_3</item>
<item>Category3_2_4</item>
<item>Category3_2_5</item>
<item>Category3_2_6</item>
</array>
<array name="Seven">
<item>Category3_3_1</item>
<item>Category3_3_2</item>
<item>Category3_3_3</item>
<item>Category3_3_4</item>
<item>Category3_3_5</item>
<item>Category3_3_6</item>
<item>Category3_3_7</item>
</array>
Hope it helps!!

use intent and the putextra method to pass the pos in the following code?

Mainactivity.java
this is the mainactivity which calls the three methods
public class MainActivity extends Activity {
Button b = (Button) findViewById(R.id.button);
public TextView t = (TextView) findViewById(R.id.postp);
public Spinner catspinner,planspinner1,planspinner2;
protected void onCreate(Bundle mbfSplash) {
super.onCreate(mbfSplash);
setContentView(R.layout.activity_main);
chooseCategory();
chooseone();
choosetwo();
}
public void chooseCategory() {
catspinner = (Spinner) findViewById(R.id.catspinner);
ArrayAdapter<CharSequence> catAdapter = ArrayAdapter.createFromResource(this, R.array.states_array, android.R.layout.simple_spinner_item);
catAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
catspinner.setAdapter(catAdapter);
catspinner.setOnItemSelectedListener(new planOnClickListener());
}
private void chooseone(){
planspinner1 = (Spinner) findViewById(R.id.planspinner);
List<String> planlist = new ArrayList<String>();
planlist.add("Mandu");
planlist.add("Chanderi");
planlist.add("Jabalpur");
planlist.add("Orccha");
planlist.add("Bhopal");
ArrayAdapter<String> planAdapter1 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, planlist);
planAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
planspinner1.setAdapter(planAdapter1);
}
private void choosetwo(){
planspinner2 = (Spinner) findViewById(R.id.planspinner);
List<String> planlist1 = new ArrayList<String>();
planlist1.add("Premier Plan");
planlist1.add("The Executive Plan");
planlist1.add("Business Circle Plan");
planlist1.add("Business Diamond Plan");
ArrayAdapter<String> planAdapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, planlist1);
planAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
planspinner2.setAdapter(planAdapter2);
}
public class planOnClickListener implements OnItemSelectedListener {
#Override
public void onItemSelected(AdapterView<?> parent, View v, int pos,
long id) {
final int p = pos;
parent.getItemAtPosition(pos);
if (pos==0){
choosePersonalPlan();
}else if (pos==1){
chooseLargeBusPlan();
} else if (pos==2){
chooseSmallMedPlan();
}
/*Intent intent = new Intent(new Intent("com.example.android.testing.SecondActivity"));
intent.putExtra("value",str);
//---set the data to pass back---
//data.setData(Uri.parse(str));
//setResult(RESULT_OK, data);
//---closes the activity---
//finish();
startActivity(intent);*/
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
the choosecategory() method gives a selected item from spinner
the next method are choosed on the basis of the first spinner item(i.e. second spinner is dependent on the spinner no. one)
second method maybe choose 1 or choose 2
the method is choosen by the help of the onItemSelected method in the planOnClickListener
thanks for the help...
Just pass your extra to your intent
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("key","value");
startActivity(intent);

String Resources

i'm practising how to add an string to the addapter of the spinner. If i declare the string in the java activity, works perfectly, but if i do in the string.xml:
<string-array name="tabs">
<item>tab</item>
<item>tab1</item>
<item>tab2</item>
<item>tab3</item>
<item>tab4</item>
<item>tab5</item>
</string-array>
and in the java
ArrayAdapter<String> adapterr1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, R.array.tabs);
dont work.
what's the problem? Thanks
This is the full code, because i have problems whit the app (crash)
public class MainActivity extends AppCompatActivity {
Spinner OptionSpinner;
TextView textview;
String[] stabs= getResources().getStringArray(R.array.tabs);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OptionSpinner = (Spinner) findViewById(R.id.OptionSpinner);
textview = (TextView) findViewById(R.id.textview);
ArrayAdapter adapter1 = new ArrayAdapter(this, android.R.layout.simple_spinner_item, stabs);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
OptionSpinner.setAdapter(adapter1);
OptionSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
textview.setText("seleccionado: " + parent.getItemAtPosition(position));
if(position==1){
Intent IntentActT1 = new Intent(MainActivity.this, ActTab1.class);
startActivity(IntentActT1);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
String[] stabs= getResources().getStringArray(R.array.tabs);
ArrayAdapter adapterr1 = new ArrayAdapter(this, android.R.layout.simple_spinner_item, stabs);
Edited 1
public class MainActivity extends AppCompatActivity {
Spinner OptionSpinner;
TextView textview;
String[] stabs;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OptionSpinner = (Spinner) findViewById(R.id.OptionSpinner);
textview = (TextView) findViewById(R.id.textview);
stabs= getResources().getStringArray(R.array.tabs);
ArrayAdapter adapter1 = new ArrayAdapter(this, android.R.layout.simple_spinner_item, stabs);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
OptionSpinner.setAdapter(adapter1);
OptionSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
textview.setText("seleccionado: " + parent.getItemAtPosition(position));
if(position==1){
Intent IntentActT1 = new Intent(MainActivity.this, ActTab1.class);
startActivity(IntentActT1);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
The constructor ArrayAdapter<String>(Context, int, int) doesn't use the third argument as an array resource id; it uses it as the id of the TextView to use to display each item in the layout. Instead, you can pass getStringArray(R.array.tabs) as the third argument (or getContext().getStringArray(R.array.tabs), if this code is not in an Activity).

How to update listview when an item is click from the listview itself

How to update listview when an item is click from the listview itself. I know there is notifydatachanged but i dont know how to implement it in my code to make it work. Please help.
private ListView listView;
private ArrayList<String> friends;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//initialize button ass and textview
Button btnSearch = (Button)findViewById(R.id.btnSearch);
final EditText ED = (EditText)findViewById(R.id.editText1);
final TextView TV_word = (TextView)findViewById(R.id.TV_word);
btnSearch.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0) {
String word = ED.getText().toString();
fillFreinds(word);
setUpList();
TV_word.setText(word); //set the word into the textview which the user has entered into the search field
}
});
//��� �������� ������
ExternalDbOpenHelper dbOpenHelper = new ExternalDbOpenHelper(this, DB_NAME);
database = dbOpenHelper.openDataBase();
//���, ���� �������!
fillWords();
setUpList();
}
private void setUpList()
{
//��������� ����������� ������� � layout �������� ��� ���������
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, friends));
listView = getListView();
//������� ���� ����, ��� ����
listView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position,long id)
{
Toast.makeText(getApplicationContext(),((TextView) view).getText().toString(), Toast.LENGTH_SHORT).show();
String wordClicked = ((TextView) view).getText().toString();
fillFreinds(wordClicked);
}
});
}
//���������� �������� �� ���� ������
private void fillFreinds(String word) {
final TextView partOfspeech = (TextView)findViewById(R.id.tv_PartOfSpeech);
friends = new ArrayList<String>();
Cursor friendCursor = database.rawQuery("SELECT * FROM friends where name='"+ word + "'", null);
friendCursor.moveToFirst();
if(!friendCursor.isAfterLast())
{
do
{
String name = friendCursor.getString(2);
partOfspeech.setText(friendCursor.getString(3));
friends.add(name);
} while (friendCursor.moveToNext());
}
friendCursor.close();
}
//On Start Activity
private void fillWords() {
friends = new ArrayList<String>();
Cursor friendCursor = database.rawQuery("SELECT * FROM friends", null);
friendCursor.moveToFirst();
if(!friendCursor.isAfterLast())
{
do
{
String name = friendCursor.getString(1);
//partOfspeech.setText(friendCursor.getString(3));
friends.add(name);
} while (friendCursor.moveToNext());
}
friendCursor.close();
}
I google for an hours but I couldnt not figure it out
change setUpList to following code:
private void setUpList()
{
//��������� ����������� ������� � layout �������� ��� ���������
//// change to this
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, friends);
setListAdapter(adapter);
listView = getListView();
//������� ���� ����, ��� ����
listView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position,long id)
{
Toast.makeText(getApplicationContext(),((TextView) view).getText().toString(), Toast.LENGTH_SHORT).show();
String wordClicked = ((TextView) view).getText().toString();
fillFreinds(wordClicked);
///// add following line
adapter.notifyDatasetChanged();
/////
}
});
}
you need define following line top of your class too:
ArrayAdapter<String> adapter;
for refreshing data you can use notifyDatasetChanged();
Add this line in your class as
ArrayAdapter<String> adapter;
Change this line
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, friends));
to
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, friends)
setListAdapter(adapter);
And notify adapter as
adapter.notifyDataSetChanged();
i.e. change your listView Item click function as
listView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position,long id)
{
Toast.makeText(getApplicationContext(),((TextView) view).getText().toString(), Toast.LENGTH_SHORT).show();
String wordClicked = ((TextView) view).getText().toString();
fillFreinds(wordClicked);
adapter.notifyDataSetChanged();
}
});

Selecting an item in AutocompleteTextview should load a list in android?

I have one autocompleteTextview, where i am loading a string array in to it.. so that i can search the data by typing the firstletter .Now i need to load a list on selecting each item in autocomplete textview .. How is it possble?
this is my autocompletetextview
autocomplete = (AutoCompleteTextView) customView.findViewById(R.id.myautocomplete);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_dropdown_item_1line, ENTON_WEB);
autocomplete.setAdapter(adapter);
Thanks in advance
String s1="xcv";
String s2;
String [] search=new String[20];<---- this is your search list
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ac=(EditText)findViewById(R.id.editText1);
ListView lv=(ListView)findViewById(R.id.listView1);
//ac.setThreshold(3);
//l.add("pp");
s2=ac.getText().toString();
adapter=new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
l);
//adapter.add("pp");
lv.setAdapter(adapter);
lv.setOnItemClickListener(mMessageClickedHandler);
new Thread(new Runnable() {
public void run() {
while(infra){
s2=ac.getText().toString();
if(s1!=s2 && s2.length()>=1){
s1=s2;
for(int=0;i<20;i++)
{
int index=search[i].index(s1);
if(index >-1)
{
lst.add(search[i])<----- lst is the arraylist of strings you should create
}
}
}
}
runOnUiThread(new Runnable() {
public void run() {
adapter.clear();
for(int z=0;z<k;z++){
adapter.add(s[z]);
}
adapter.notifyDataSetChanged();
}
});
}
}
}}).start();
}
private OnItemClickListener mMessageClickedHandler = new
OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id){
infra=false;
String s=d[position];
finish();
Intent k=new Intent(InfrapagesActivity.this,InfrapagesActivity.class);
startActivity(k);
Intent i=new Intent(this,nextactivity.class);
i.putExtra("hash",s);
startActivity(i);
}
};
}
next activity
public class nextactivity extends ListActivity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
type = getIntent().getExtras().get("hash").toString();....
you can search using the string "type" and populate the list...

Categories

Resources