Friday, August 24, 2012

Re: [android-developers] More Than One Spinners

okay  Thank you  for reply i tell u in more details...
 
 Actually i have created one  spinner with different entries and different icons  in my app but when i create second 'spinner' with different entries and different icons in my app, so entries overwrite on one another due to the initialization of second 'spinner's' entries..any help please, i m new in android ...please reply me as soon as possible...Many Thanks!
 
here is my **main.xml**
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Spinner
    android:id="@+id/spinner1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:drawSelectorOnTop="true"
    android:prompt="@string/prompt"/>
    <Spinner
    android:id="@+id/spinner2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:drawSelectorOnTop="true"
    android:prompt="@string/prompt"/>
    </LinearLayout>
 
my **row.xml**
   
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="3dip">
    <ImageView
    android:id="@+id/image1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher"/>
    <TextView
    android:layout_toRightOf="@+id/image1"
    android:padding="3dip"
    android:layout_marginTop="2dip"
    android:textColor="@drawable/red"
    android:textStyle="bold"
    android:id="@+id/company1"
    android:text="Sandils"
    android:layout_marginLeft="5dip"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
    <TextView
    android:layout_toRightOf="@+id/image1"
    android:padding="2dip"
    android:textColor="@drawable/darkgrey"
    android:layout_marginLeft="5dip"
    android:id="@+id/sub1"
    android:layout_below="@+id/company1"
    android:text="Sandils"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
   
    <ImageView
    android:id="@+id/image2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher"/>
    <TextView
    android:layout_toRightOf="@+id/image2"
    android:padding="3dip"
    android:layout_marginTop="2dip"
    android:textColor="@drawable/red"
    android:textStyle="bold"
    android:id="@+id/company2"
    android:layout_marginLeft="5dip"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
    <TextView
    android:layout_toRightOf="@+id/image2"
    android:padding="2dip"
    android:textColor="@drawable/darkgrey"
    android:layout_marginLeft="5dip"
    android:id="@+id/sub2"
    android:layout_below="@+id/company2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
    </RelativeLayout>
 
my **SpinnersDemo.java** activity
   
    Package com.SpinnersDemo;
    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.ImageView;
    import android.widget.Spinner;
    import android.widget.TextView;
    import android.widget.AdapterView.OnItemSelectedListener;
    public class SpinnersDemo extends Activity {
    //strings for first spinner
    String[] strings1 = {"Gold","Silver",
    "Antique", "Diamond", "Silver","White Gold"};
    
    //sub strings for first spinner
    String[] subs1 = {"Gold Sub ","Silver sub",
    "Antique sub", "Diamond sub", "SIlver sub","White Gold sub"};
    
    //images for first spinner
    int arr_images[] = { R.drawable.d0,
    R.drawable.d1, R.drawable.d2,
    R.drawable.d3, R.drawable.d4, R.drawable.d5};
 
    //strings for second spinner

    String[] strings2 = {"Flat Shoes","Heel Shoes",
    "Baby Shoes"};
    //sub strings for second spinner
    String[] subs2 = {"Flat Shoes sub","Heel Shoes sub",
    "Baby Shoes sub"};
    //images for second spinner
    int arr_images2[] = { R.drawable.s0,
    R.drawable.s1, R.drawable.s2};
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Spinner mySpinner = (Spinner)findViewById(R.id.spinner1);
    mySpinner.setAdapter(new MyAdapter(SpinnersDemo.this, R.layout.row, strings1));
    Spinner mySpinner2 = (Spinner)findViewById(R.id.spinner2);
    mySpinner2.setAdapter(new MyAdapter(SpinnersDemo.this, R.layout.row, strings2));
    }
    public class MyAdapter extends ArrayAdapter<String>{
    public MyAdapter(Context context, int row, String[] objects) {
    super(context, row, objects);
    }
    @Override
    public View getDropDownView(int position, View convertView,ViewGroup parent) {
    return getCustomView(position, convertView, parent);
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    return getCustomView(position, convertView, parent);
    }
    public View getCustomView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater=getLayoutInflater();
    View row=inflater.inflate(R.layout.row, parent, false);
    TextView label=(TextView)row.findViewById(R.id.company1);
    label.setText(strings1[position]);
    TextView sub=(TextView)row.findViewById(R.id.sub1);
    sub.setText(subs1[position]);
    ImageView icon=(ImageView)row.findViewById(R.id.image1);
    icon.setImageResource(arr_images[position]);
    //spinner2
    TextView label2=(TextView)row.findViewById(R.id.company2);
    label2.setText(strings2[position]);
    TextView subb2=(TextView)row.findViewById(R.id.sub2);
    subb2.setText(subs2[position]);
    ImageView icon2=(ImageView)row.findViewById(R.id.image2);
    icon2.setImageResource(arr_images2[position]);
    return row;}
    }}
 
my **strings.xml**
 
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    <string name="hello">Spinners Demo !</string>
    <string name="prompt">Select your Favourite</string>
    <string name="app_name">Spinners Demo</string>
    <drawable name="white">#ffffff</drawable>
    <drawable name="black">#000000</drawable>
    <drawable name="green">#347C2C</drawable>
    <drawable name="pink">#FF00FF</drawable>
    <drawable name="violet">#a020f0</drawable>
    <drawable name="grey">#778899</drawable>
    <drawable name="red">#C11B17</drawable>
    <drawable name="yellow">#FFFF8C</drawable>
    <drawable name="PowderBlue">#b0e0e6</drawable>
    <drawable name="brown">#2F1700</drawable>
    <drawable name="Hotpink">#7D2252</drawable>
    <string name="select_Category">Select Category</string>
    <drawable name="darkgrey">#606060</drawable>
    </resources>
 
 
I have also attached the  screen shots of given code 's output...
 
 
 


 
On Fri, Aug 24, 2012 at 11:34 AM, Narendra Singh Rathore <nsr.curious@gmail.com> wrote:


On Sat, Aug 18, 2012 at 2:32 AM, S Awan <juwerian.sobia@gmail.com> wrote:
Hi,
    i wanna set more than one spinner with different strings and different icons ..i hve done this all in one spinner correctly  but when i set the second spinner with different strings and icons it loads the same strings as first spinner even i defined different strings and icons... i hve uploaded the sample image for easily understand ...i am new in android ..any help plzzzzzzzz, many Thnx...



Please mention clearly, how you are doing that, so that we can help you.

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en



--
Sobia Awan
  Bs(cs)
 
 

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment