I reckon I would subclass View and create a Product_View class.


Product_View would override the onDraw method and draw a product thumbnail 
next to its name.



On Thursday, November 1, 2012 10:14:30 AM UTC-5, S Awan wrote:
>
> Hi,
> I have taken spinner and have database in Wamp Server.. I want to get data 
> like Product Name and Product Image from database on spinner in android.I 
> have done from different ways but not working..can any one give me the 
> sample code or any instructions about this??please reply..Thank you
>  
> This is my CutomSpinnerDemo.java
>  
>  package com.CustomDemo;
>  import java.net.MalformedURLException;
>  import java.net.URL;
>  import java.util.ArrayList;
>  import android.app.Activity;
>  import android.content.Context;
>  import android.content.Intent;
>  import android.graphics.Bitmap;
>  import android.os.Bundle;
>  import android.util.Log;
>  import android.view.LayoutInflater;
>  import android.view.View;
>  import android.view.ViewGroup;
>  import android.widget.AdapterView;
>  import android.widget.AdapterView.OnItemSelectedListener;
>  import android.widget.RelativeLayout.LayoutParams;
>  import android.widget.ArrayAdapter;
>  import android.widget.BaseAdapter;
>  import android.widget.ImageView;
>  import android.widget.ListView;
>  import android.widget.RelativeLayout;
>  import android.widget.Spinner;
>  import android.widget.TextView;
>  import android.widget.Toast;
>  public class CustomSpinnerDemo extends Activity {
>   ArrayAdapter list1;
>   
>  /*String[] strings1 = { "Jewellery", "Ear Rings", "Rings", 
> "Neckless","Bracelets","Nose Pins" };
>   String[] subs1 = { "sobia ", "Silver sub", "Antique sub", "Diamond sub",
>     "SIlver sub", "White Gold sub" };
>   int arr_images[] = { R.drawable.jj22, R.drawable.e1, R.drawable.r13,
>     R.drawable.jj11, R.drawable.b1, R.drawable.ns1 };*/
>  
>    int mGalleryInitializedCount = 0;
>    int mGalleryCount = 0;
>    
>   @Override
>   public void onCreate(Bundle savedInstanceState) {
>    try {
>    super.onCreate(savedInstanceState);
>    Helper objHelper = new Helper();
>    
>    } catch (Throwable t) {
>     Toast.makeText(this, "Request failed: " + t.toString(),
>       Toast.LENGTH_LONG).show();}
>   int count = Helper.vctrMainCategoriesTitle.size();
>    
>    for(int i =0;i<count;i++)
>    {  
>    list1.add(Helper.vctrMainCategoriesTitle.elementAt(i));
>    }
>    setAdapter(new MyAdapter(getBaseContext()));
>    setAdapter(new ArrayAdapter<String>(this,
>       R.layout.spin,R.id.spinner
>       )); 
>    }
>    
>    private void setAdapter(ArrayAdapter<String> arrayAdapter) {
>    // TODO Auto-generated method stub
>    
>   }
>    private void setAdapter(MyAdapter myAdapter) {
>    // TODO Auto-generated method stub
>    
>   }
>   public class MyAdapter extends BaseAdapter {
>    
>    int counter = 0;
>    Context mContext;
>    Activity a;
>    //String[] subTitle;
>    
>    public MyAdapter(Context context) {
>     this.mContext = context;
>     this.a = a;
>     //this.subTitle=sub;
>     
>    }
>    
>    public int getCount() {
>     // TODO Auto-generated method stub
>     return Helper.vctrMainCategoriesTitle.size();
>    }
>   
>    public Object getItem(int position) {
>     return position;
>    }
>    public long getItemId(int position) {
>     return position;
>    }
>
>   
>    public View getView(int position, View convertView, ViewGroup parent) {
>     ImageView text = new ImageView(mContext);
>     Helper helper = new Helper();
>     
>     String strUrl = 
> Helper.vctrMainCategoriesImageUrls.elementAt(counter).toString();// getting 
> url of the images
>     System.out.println("Urls...." + strUrl);
>     URL url =null;
>     try {
>      url = new URL(strUrl);
>     } catch (MalformedURLException e) {
>      // TODO Auto-generated catch block
>      e.printStackTrace();
>     }
>     Bitmap bitmap = helper.loadPhotoBitmap(url);
>     text.setImageBitmap(bitmap);
>     RelativeLayout layout = new RelativeLayout(mContext);
>     //RelativeLayout.LayoutParams params = new 
> RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
>     //layout.setLayoutParams(params);
>     TextView text1 = new TextView(mContext);
>     text1.setTextSize(30);
>
>     
> text1.setText(Helper.vctrMainCategoriesTitle.elementAt(counter).toString());
>     LayoutParams params1 = new  
> LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
>     params1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
>     text1.setLayoutParams(params1);
>     layout.addView(text1);
>     ImageView image = new ImageView(mContext);
>     image.setImageBitmap(bitmap);
>     layout.addView(image);
>     counter++;
>     return layout;
>    }
>    
>   }
>   
>   public void addListenerOnSpinnerItemSelection() {
>    Spinner mySpinner = (Spinner) findViewById(R.id.spinner);
>    mySpinner.setOnItemSelectedListener(new CustomOnItemSelectedListener());
>    
>    mGalleryCount = 2;
>    }
>    public class CustomOnItemSelectedListener implements 
> OnItemSelectedListener {
>    public void onItemSelected(AdapterView<?> parent,Spinner s, View view,  
> int position,
>    long id) {
>    // Display Selected option
>     System.out.println("Clicked");
>     System.out.println(s.getItemAtPosition(position).toString()); 
>     
>      System.out.println(Helper.vctrMainCategoriesImageUrls.get(position));
>     TextView tv=(TextView) parent.findViewById(R.id.company);
>     if (mGalleryInitializedCount <= mGalleryCount) {
>     mGalleryInitializedCount++;
>     /*Log.d("onItemSelcted", " "
>     + mGalleryInitializedCount);*/
>     }
>     /*Log.d("onItemSelcted", " "
>       + parent.getItemAtPosition(pos).toString());*/
>      if (tv.getText().toString().equals("Rings")) {
>   
>     Intent i = new Intent(CustomSpinnerDemo.this,
>     MessageActivity.class);
>     startActivity(i);
>     } else if (tv.getText().toString()
>     .equals("Ear Rings")) {
>     Intent i = new Intent(CustomSpinnerDemo.this,
>       MessageActivity.class);
>     startActivity(i);}
>     else if (tv.getText().toString()
>       .equals("Silver")) {
>       Intent i = new Intent(CustomSpinnerDemo.this,
>         MessageActivity.class);
>       startActivity(i);}
>     else if (tv.getText().toString()
>       .equals("Women Perfume")) {
>       Intent i = new Intent(CustomSpinnerDemo.this,
>         MessageActivity.class);
>       startActivity(i);
>       }
>     }
>     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
>      
>     }
>     }
>     }
>  
> Helper.java class
>   package com.CustomDemo;
>   import java.io.BufferedInputStream;
>   import java.io.BufferedOutputStream;
>   import java.io.BufferedReader;
>   import java.io.ByteArrayOutputStream;
>   import java.io.Closeable;
>   import java.io.FileOutputStream;
>   import java.io.IOException;
>   import java.io.InputStream;
>   import java.io.InputStreamReader;
>   import java.io.OutputStream;
>   import java.net.MalformedURLException;
>   import java.net.URL;
>   import java.util.ArrayList;
>   import java.util.StringTokenizer;
>   import java.util.Vector;
>   import org.apache.http.HttpEntity;
>   import org.apache.http.HttpResponse;
>   import org.apache.http.client.HttpClient;
>   import org.apache.http.client.methods.HttpPost;
>   import org.apache.http.entity.ByteArrayEntity;
>   import org.apache.http.impl.client.DefaultHttpClient;
>   import org.apache.http.params.BasicHttpParams;
>   import org.apache.http.params.HttpConnectionParams;
>   import org.apache.http.params.HttpParams;
>   import org.json.JSONObject;
>   import android.graphics.Bitmap;
>   import android.graphics.BitmapFactory;
>   import android.graphics.Typeface;
>   import android.util.Log;
>   import android.widget.ArrayAdapter;
>   import android.widget.Spinner;
>   import android.widget.TextView;
>   import android.widget.Toast;
>   public class Helper {
>   public  static Vector vctrMainCategories = null;
>   public  static Vector vctrMainCategoriesImageUrls = null;
>   public  static Vector vctrMainCategoriesTitle = null;
>   public  static Vector vctrMainCategoriesIds= null;
>
>   //public  static Vector vctrMainCategoriesImageUrls = null;
>   public int iLanguage = 0;
>   TextView lbl;
>   Typeface arabicFont = null;
>   int TIMEOUT_MILLISEC = 10000; // = 10 seconds
>   Spinner list1;
>   String result =null;
>    
>    public Helper() {
>    
>     // TODO Auto-generated constructor stub
>     vctrMainCategories =  new Vector();
>     vctrMainCategoriesImageUrls =  new Vector();
>     vctrMainCategoriesTitle =  new Vector();
>     vctrMainCategoriesIds = new Vector();
>     
>     String record= getProductList();
>     showMessage("Actual Records-----------: "+record);
>     record = record.substring(0, record.length()-4);
>     showMessage("Actual Record: "+record); // will write message in console
>     createRecordInVectors(record);// this function will devide the whole 
> record into hierarchi
>     
>     String strUrl = "
> http://i.i.com.com/cnwk.1d/i/tim/2012/10/02/CarParis_(16)_60x60.jpg";
>     URL url =null;
>     try {
>      url = new URL(strUrl);
>     } catch (MalformedURLException e) {
>      // TODO Auto-generated catch block
>      e.printStackTrace();
>     }
>     
>     loadPhotoBitmap(url);
>    }
>    
>    
>    private void createRecordInVectors(String record) {
>     // TODO Auto-generated method stub
>     
>     StringTokenizer st = new StringTokenizer(record,"~"); // divide record 
> into small pieces  like single record 
>     int totalNoOfCounts = st.countTokens();
>     for(int i=0;i<totalNoOfCounts;i++){
>      
>      Helper.vctrMainCategories.addElement(st.nextToken());
>      showMessage("Vector 
> Value"+Helper.vctrMainCategories.get(i).toString()); // see output of the 
> current set value
>      
>     }
>     
>     // now dividing the individual record into small pieces
>     
>     int sizeOfRecord = Helper.vctrMainCategories.size(); // return the 
> size of the all records that will be used in loop
>     System.out.println("size of Records"+ sizeOfRecord);
>     
>     for(int i=0;i<sizeOfRecord;i++){
>      
>      StringTokenizer stRecord = new 
> StringTokenizer(Helper.vctrMainCategories.get(i).toString(),"_");
>      Helper.vctrMainCategoriesIds.add(stRecord.nextToken());
>      showMessage("Titles"+Helper.vctrMainCategoriesIds.get(i).toString());
>      Helper.vctrMainCategoriesTitle.add(stRecord.nextToken());// title
>      
>
>      showMessage("Titles"+Helper.vctrMainCategoriesTitle.get(i).toString());
>      
>      Helper.vctrMainCategoriesImageUrls.add(stRecord.nextToken());
>      showMessage("Images: 
> "+Helper.vctrMainCategoriesImageUrls.get(i).toString());
>      
>      
>     // showMessage("Indvidual Record: 
> "+Helper.vctrMainCategories.get(i).toString());
>      
>     }
>     
>     
>     
>     
>    }
>    
>    public String getProductList(){
>     try {
>      JSONObject json = new JSONObject();
>      json.put("shopId", "1");
>      json.put("productId", "123");
>      HttpParams httpParams = new BasicHttpParams();
>      HttpConnectionParams.setConnectionTimeout(httpParams,
>        TIMEOUT_MILLISEC);
>      HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
>      HttpClient client = new DefaultHttpClient(httpParams);
>      
>     
>      String url = "
> http://119.154.253.169:8088/phpWebservice/webservice2.php";;
>     
>      HttpPost request = new HttpPost(url);
>      request.setEntity(new ByteArrayEntity(json.toString().getBytes(
>        "UTF8")));
>      request.setHeader("json", json.toString());
>      HttpResponse response = client.execute(request);
>      HttpEntity entity = response.getEntity();
>      // If the response does not enclose an entity, there is no need
>      if (entity != null) {
>       InputStream instream = entity.getContent();
>       
>       String result = convertStreamToString(instream);
>      // list.add(result);
>      // list.add("faraz");
>       //Log.i("Read from server", result);
>   //    Toast.makeText(this,  result,
>   //      Toast.LENGTH_LONG).show();
>       
>       return result;
>      }
>     } catch (Throwable t) {
>   //   Toast.makeText(this, "Request failed: " + t.toString(),
>   //     Toast.LENGTH_LONG).show();
>     }
>     
>     return "Record not Found";
>    }
>    
>    public static String convertStreamToString(InputStream is) {
>     /*
>      * To convert the InputStream to String we use the 
> BufferedReader.readLine()
>      * method. We iterate until the BufferedReader return null which means
>      * there's no more data to read. Each line will appended to a 
> StringBuilder
>      * and returned as String.
>      */
>     BufferedReader reader = new BufferedReader(new InputStreamReader(is));
>     StringBuilder sb = new StringBuilder();
>     String line = null;
>     try {
>      while ((line = reader.readLine()) != null) {
>       sb.append(line+ "\n");
>      }
>     } catch (IOException e) {
>      e.printStackTrace();
>     } finally {
>      try {
>       is.close();
>      } catch (IOException e) {
>       e.printStackTrace();
>      }
>     }
>     return sb.toString();
>    }
>    
>    
>    private void showMessage(String message) {
>     // TODO Auto-generated method stub
>     System.out.println("Message: "+message);
>     
>    }
>    
>    
>    
>    public Bitmap loadPhotoBitmap(URL url) {
>    Bitmap bitmap = null;
>     InputStream in = null;
>     BufferedOutputStream out = null;
>     BufferedOutputStream bfs = null;
>     try {
>      FileOutputStream fos = new FileOutputStream("/sdcard/photo-tmp.jpg");
>      bfs = new BufferedOutputStream(fos);
>      
>      in = new BufferedInputStream(url.openStream(),8192);
>      final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
>      out = new BufferedOutputStream(dataStream, 8192);
>      copy(in, out);                    
>      out.flush();
>      final byte[] data = dataStream.toByteArray();
>      bfs.write(data, 0, data.length);
>      bfs.flush();
>      BitmapFactory.Options opt = new BitmapFactory.Options();
>      bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opt);
>     } catch (IOException e) {
>      android.util.Log.e("message", "Could not load photo: " + this, e);
>     } finally {
>      closeStream(in);
>      closeStream(out);
>      closeStream(bfs);
>     }
>     return bitmap;
>    }
>   private static void copy(InputStream in, OutputStream out) throws 
> IOException {
>    
>    byte[] b = new byte[8192];
>    int read;
>    while ((read = in.read(b)) != -1) {
>     out.write(b, 0, read);
>    }
>   }
>   private static void closeStream(Closeable stream) {
>    if (stream != null) {
>     try {
>      stream.close();
>     } catch (IOException e) {
>      android.util.Log.e("Message", "Could not close stream", e);
>     }}}}}
>
> spin.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"
>  android:id="@+id/background"
>  android:background="@drawable/ba">
>  
>    
>    
>    <!--  Header Ends --> 
>  <Spinner
>   android:id="@+id/spinner"
>   android:layout_width="fill_parent"
>   android:layout_height="75dp"
>   android:drawSelectorOnTop="true"
>   android:prompt="@string/prompt" />
>
>    
>     <TextView android:id="@+id/abc" 
>      android:layout_height="wrap_content" 
>      android:layout_width="fill_parent" 
> android:paddingBottom="5px"></TextView>
>  
>  </LinearLayout>
> 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/image"
>  android:layout_width="wrap_content"
>  android:layout_height="wrap_content" 
>  android:src="@drawable/ic_launcher" />
>
> <TextView android:layout_toRightOf="@id/image"
>  android:padding="3dip" 
>  android:layout_marginTop="2dip"
>  android:textColor="@drawable/black" 
>  android:textStyle="bold" 
>  android:id="@+id/company"
>  android:text="" 
>  android:layout_marginLeft="5dip"
>  android:layout_width="wrap_content" 
>  android:layout_height="wrap_content" />
>
>  <TextView 
> android:layout_toRightOf="@id/image"
>  android:padding="2dip" 
>  android:textColor="@drawable/darkgrey"
>  android:layout_marginLeft="5dip" 
>  android:id="@+id/sub2"
>  android:layout_below="@+id/company" 
>  android:text=""
>  android:layout_width="wrap_content" 
>  android:layout_height="wrap_content" />
> </RelativeLayout>
>  
> I created maincategories table with the given column names: 
> MainCatId,Name,Imgurl in wampserver ...
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>
>  
>  
>
>
> -- 
> *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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to