As I am working on getting a custom spinner and displaying the value in toast. The values are setting to the spinner correctly but I am unable to display them in toast. My code is
Main.java
public class Main extends Activity {
ArrayList<String> ast = new ArrayList<String>();
Spinner customspinner;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
customspinner = (Spinner)findViewById(R.id.spinner1);
ast.add("111111");
ast.add("222222");
ast.add("333333");
ast.add("444444");
MyAdapter adp = new MyAdapter();
customspinner.setAdapter(adp);
System.out.println(ast);
customspinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapter, View v,
int position, long val) {
// TODO Auto-generated method stub
//here when spinner was selected I need to save those two custom textviews in two string and need to display them in toast....
//String getspineer = customspinner.getSelectedItem().toString();
//Toast.makeText(getApplicationContext(), "spinner --- " + getspineer , Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
public class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
// TODO Auto-generated method stub
return ast.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = null;
LayoutInflater inflater = getLayoutInflater();
v = inflater.inflate(R.layout.customspinner, null);
TextView tv = (TextView)v.findViewById(R.id.textView1);
TextView tv1 = (TextView)v.findViewById(R.id.textView2);
String arlstdate[] = ast.get(position).split("~");
for (int i = 0; i < arlstdate.length; i++) {
tv.setText(arlstdate[i]);
tv1.setText(arlstdate[i]);
}
return v;
}
}
}
Here what I am looking for is, when I select spinner, I need to save the following two textview values in two string and need to display them in Toast mess seperately.
Can anyone help me with this. After a lot of search I found this code but unable to save to text values in string & display..
I don’t really get exactly what you mean sorry, but as a pointer, the content of the view at that position on the spinner is the same as the string at same position on the ArrayList you used to populate the adapter.
So you can take the value from the ArrayList not from the Spinner View.