2011-03-16 130 views
7

即時通訊從數據庫中獲取數據並顯示在gridview罰款。但我需要在每個文本顯示下面放置一個單獨的按鈕。當我點擊按鈕。,我要做一些東西。我使用customList適配器從DB中檢索數據。我可以這樣做嗎?自定義適配器的gridview在android

我的代碼..

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); 
      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.setCateogry(cursor.getString(1)); 
       single.setDescription(cursor.getString(2)); 
       single.setImage(cursor.getBlob(3)); 
       allElementDetails.add(single); 

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

     } 
    }); 
    } 

}

我customListAdapter ..

import java.io.ByteArrayInputStream; 
import java.util.ArrayList; 

import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 

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 textview = (TextView) convertView.findViewById(R.id.category_entry); 
    TextView textview1 = (TextView) convertView.findViewById(R.id.description_entry); 
    textview.setText(allElementDetails.get(position).getCategory()); 
    textview1.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

請你介意加入R.layout.listview1'的'xml文件,所以我能理解你的榜樣,我也有類似的情況。 – 2014-06-17 22:27:30

回答

8

而不是使用CustomListAdapter的,你必須創建自己的適配器擴展BaseAdapter,爲每個網格項目(擴展LinearLayout)創建一個佈局然後你的textview就是一個按鈕。

尼斯嘖:

Custom GridView

+0

感謝您的答覆..我用我的CustomListAdapter代碼更新我的問題..也擴展了BaseAdapter ..在哪裏可以添加這些按鈕? – sanjay 2011-03-16 12:08:14

+0

太棒了!我已經完成了..工作正常..謝謝很多.. – sanjay 2011-03-16 12:37:54

+1

鏈接中的教程非常過時 - 對於那些沒有注意到像我這樣的2011年答案的人;) – Srneczek 2015-10-08 12:57:23