2011-03-17 74 views
1

我從數據庫中檢索圖像+文本並將其顯示在gridview.i.e中。每個gridview項目包含一個圖像+一個文本。它工作正常。但我只是想知道如何設置每個gridview項目的邊界在android.How我可以做到這一點?android中的gridview項的邊框

我的代碼..

public class HomePage extends Activity { 
    private ArrayList<SingleElementDetails> allElementDetails=new ArrayList<SingleElementDetails>(); 
    DBAdapter db=new DBAdapter(this); 
    String category, description; 
    String data; 
    String data1; 
    GridView gridview; 
    Button menu; 

     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.homepage); 

     menu=(Button)findViewById(R.id.menus); 

     menu.setOnClickListener(new OnClickListener(){ 
      public void onClick(View v) 
      { 
       gridview=(GridView)findViewById(R.id.gridview); 
       TypedArray a = obtainStyledAttributes(R.styleable.Gallery); 
       int mGalleryItemBackground = a.getResourceId(
         R.styleable.Gallery1_android_galleryItemBackground, 0); 
       a.recycle(); 


       allElementDetails.clear(); 
       db.open(); 
       long id; 
       //id=db1.insertTitle1(category, description,r_photo); 
       Cursor cursor = db.getAllTitles1(); 
       while (cursor.moveToNext()) 
       { 
        SingleElementDetails single=new SingleElementDetails(); 
        single.setDishName(cursor.getString(1)); 
        single.setCateogry(cursor.getString(2)); 
        single.setDescription(cursor.getString(3)); 
        single.setImage(cursor.getBlob(4)); 
        allElementDetails.add(single); 

       } 
       db.close(); 
      CustomListAdapter adapter=new CustomListAdapter(HomePage.this,allElementDetails); 
      gridview.setAdapter(adapter); 
      gridview.setBackgroundResource(mGalleryItemBackground); 



      } 
     }); 
     } 

} 

我用CustomListAdapter這裏從數據庫檢索數據的存儲。 我CustomListAdapter代碼...

public class CustomListAdapter extends BaseAdapter { 
    private ArrayList<SingleElementDetails> allElementDetails; 

    private LayoutInflater mInflater; 

    public CustomListAdapter(Context context, ArrayList<SingleElementDetails> results) { 
     allElementDetails = results; 
     mInflater = LayoutInflater.from(context); 
    } 

    public int getCount() { 
     return allElementDetails.size();   
    } 

    public Object getItem(int position) { 
     return allElementDetails.get(position); 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) 
    { 
     convertView = mInflater.inflate(R.layout.listview1, null); 
     ImageView imageview = (ImageView) convertView.findViewById(R.id.image); 

     TextView textview1= (TextView) convertView.findViewById(R.id.dishname_entry); 
     TextView textview2 = (TextView) convertView.findViewById(R.id.category_entry); 
     TextView textview3=(TextView)convertView.findViewById(R.id.description_entry); 
     textview1.setText(allElementDetails.get(position).getDishName()); 
     textview2.setText(allElementDetails.get(position).getCategory()); 

     if(allElementDetails.get(position).getDescription().length()>8) 
      textview3.setText(allElementDetails.get(position).getDescription().substring(0,8)+"..."); 
     else 
      textview3.setText(allElementDetails.get(position).getDescription());  

     byte[] byteimage=allElementDetails.get(position).getImage(); 
     ByteArrayInputStream imageStream = new ByteArrayInputStream(byteimage); 
     BitmapFactory.Options op=new BitmapFactory.Options(); 
     op.inSampleSize=12; 
     Bitmap theImage= BitmapFactory.decodeStream(imageStream,null,op); 
     imageview.setImageBitmap(theImage); 
     return convertView; 
    }  

} 
+0

我最近幾乎解決了類似的問題。你在看什麼樣的邊界?每個網格周圍都有一個簡單的矩形/正方形? – Abhijit 2012-04-11 15:01:51

+0

我有同樣的問題.....如何把一個簡單的正方形圍繞每個網格? – thej 2012-09-07 07:56:27

+0

你解決了這個問題嗎?你可以分享嗎? – user1781367 2013-05-22 12:38:22

回答

1

也許有更簡單的方法,但有可能創建一個XML定義裏面像這樣與層可繪製的可繪製:

每個繪製由內部的元素表示一個單一的元素。

文件位置:

res/drawable/filename.xml 

的文件名被用作資源ID。

<?xml version="1.0" encoding="utf-8"?> 
<layer-list 
    xmlns:android="http://schemas.android.com/apk/res/android" > 
<item> 
    <bitmap 
     android:src="@drawable/rectangle" /> 
</item> 
<item> 
    <bitmap 
     android:src="@drawable/griditem" /> 
</item> 
<item> 
    <nine-patch> other etc... 
</item> 
    <item> other drawable etc.</item> 
</layer-list> 

可以使用它作爲一個可繪製或位圖如下所示:

Bitmap bitmap=BitmapFactory.decodeResource(getApplicationContext.getResources(),R.drawable.filename); 

有些文檔here